Question: Using Java8 to code game. Develop an app based on the code below that adds the following features: Players take it in turns to place
Using Java8 to code game.
Develop an app based on the code below that adds the following features:
-
Players take it in turns to place 3 units at a time on a territory that they control and then one unit for each neutral. The user types in the name of the territories. The user should be allowed to enter a shortened version of the name, so long as it is unambiguous. After each selection, update the map.
-
The user should receive appropriate error messages if their commands are invalid.
public class Board { private boolean[] occupied = new boolean [GameData.NUM_COUNTRIES]; private int[] occupier = new int [GameData.NUM_COUNTRIES]; private int[] numUnits = new int [GameData.NUM_COUNTRIES]; Board() { for (int i=0; i public class CommandPanel extends JPanel { private static final long serialVersionUID = 1L; private static final int FONT_SIZE = 14; private JTextField commandField = new JTextField(); private LinkedList commandBuffer = new LinkedList(); CommandPanel () { class AddActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { synchronized (commandBuffer) { commandBuffer.add(commandField.getText()); commandField.setText(""); commandBuffer.notify(); } return; } } ActionListener listener = new AddActionListener(); commandField.addActionListener(listener); commandField.setFont(new Font("Times New Roman", Font.PLAIN, FONT_SIZE)); setLayout(new BorderLayout()); add(commandField, BorderLayout.CENTER); return; } public String getCommand() { String command; synchronized (commandBuffer) { while (commandBuffer.isEmpty()) { try { commandBuffer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } command = commandBuffer.pop(); } return command; } } public final class GameData { public static final int NUM_PLAYERS = 2; public static final int NUM_NEUTRALS = 4; public static final int NUM_PLAYERS_PLUS_NEUTRALS = NUM_PLAYERS + NUM_NEUTRALS; public static final int NUM_COUNTRIES = 42; public static final int INIT_COUNTRIES_PLAYER = 9; public static final int INIT_COUNTRIES_NEUTRAL = 6; public static final String[] COUNTRY_NAMES = { "Ontario","Quebec","NW Territory","Alberta","Greenland","E United States","W United States","Central America","Alaska", "Great Britain","W Europe","S Europe","Ukraine","N Europe","Iceland","Scandinavia", "Afghanistan","India","Middle East","Japan","Ural","Yakutsk","Kamchatka","Siam","Irkutsk","Siberia","Mongolia","China", "E Australia","New Guinea","W Australia","Indonesia", "Venezuela","Peru","Brazil","Argentina", "Congo","N Africa","S Africa","Egypt","E Africa","Madagascar"}; // for reference public static final int[][] ADJACENT = { {4,1,5,6,3,2}, // 0 {4,5,0}, {4,0,3,8}, {2,0,6,8}, {14,1,0,2}, {0,1,7,6}, {3,0,5,7}, {6,5,32}, {2,3,22}, {14,15,13,10}, {9,13,11,37}, // 10 {13,12,18,39,10}, {20,16,18,11,13,15}, {15,12,11,10,9}, {15,9,4}, {12,13,14}, {20,27,17,18,12}, {16,27,23,18}, {12,16,17,40,39,11}, {26,22}, {25,27,16,12}, // 20 {22,24,25}, {8,19,26,24,21}, {27,31,17}, {21,22,26,25}, {21,24,26,27,20}, {24,22,19,27,25}, {26,23,17,16,20,25}, {29,30}, {28,30,31}, {29,28,31}, // 30 {23,29,30}, {7,34,33}, {32,34,35}, {32,37,35,33}, {33,34}, {37,40,38}, {10,11,39,40,36,34}, {36,40,41}, {11,18,40,37}, {39,18,41,38,36,37}, //40 {38,40} }; public static final int NUM_CONTINENTS = 6; public static final String[] CONTINENT_NAMES = {"N America","Europe","Asia","Australia","S America","Africa"}; // for reference public static final int[] CONTINENTS = {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,5,5}; private GameData() { //this prevents even the native class from calling this constructor throw new AssertionError(); } } public class InfoPanel extends JPanel { private static final long serialVersionUID = 1L; private static final int TEXT_AREA_HEIGHT = 10; private static final int CHARACTER_WIDTH = 30; private static final int FONT_SIZE = 14; JTextArea textArea = new JTextArea(TEXT_AREA_HEIGHT, CHARACTER_WIDTH); JScrollPane scrollPane = new JScrollPane(textArea); DefaultCaret caret = (DefaultCaret)textArea.getCaret(); InfoPanel () { textArea.setEditable(false); textArea.setFont(new Font("Times New Roman", Font.PLAIN, FONT_SIZE)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); setLayout(new BorderLayout()); add(scrollPane, BorderLayout.CENTER); return; } public void addText (String text) { textArea.setText(textArea.getText()+" "+text); } } public class Sprint1 { public static void main (String args[]) { Board board = new Board(); UI ui = new UI(board); int playerId, countryId; String name; // display blank board ui.displayMap(); // get player names for (playerId=0; playerId " + name); } // add units countryId = 0; for (playerId=0; playerId
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
