Question: Using Java to program card game 'Risk' Achieving following functionality based on the Java code given. If the other human players armies are eliminated, the
Using Java to program card game 'Risk'
Achieving following functionality based on the Java code given.
-
If the other human players armies are eliminated, the app should display the name of the winner
and a game over message.
-
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
occupied[i] = false ;
numUnits[i] = 0;
}
return;
}
public void addUnits (int countryId, int player, int addNumUnits) {
// prerequisite: country must be unoccupied or already occupied by this player
if (!occupied[countryId]) {
occupied[countryId] = true;
occupier[countryId] = player;
}
numUnits[countryId] = numUnits[countryId] + addNumUnits;
return;
}
public void addUnits (Card card, Player player, int addNumUnits) {
addUnits(card.getCountryId(), player.getId(), addNumUnits);
return;
}
public void addUnits (int countryId, Player player, int addNumUnits) {
addUnits(countryId, player.getId(), addNumUnits);
return;
}
public boolean checkOccupier (Player player, int countryId) {
return (occupier[countryId] == player.getId());
}
public boolean isOccupied(int country) {
return occupied[country];
}
public int getOccupier (int country) {
return occupier[country];
}
public int getNumUnits (int country) {
return numUnits[country];
}
}
import java.awt.event.ActionEvent; import java.util.*; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.*; import javax.swing.JTextField;
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; } }
import javax.swing.*; import javax.swing.text.*; import java.awt.*;
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 Parse {
private String[] countryCodes = new String[GameData.NUM_COUNTRIES];
private boolean isError = false;
private int countryId = 0;
private boolean isDone = false;
private int numUnits;
Parse () {
String name;
for (int i=0; i
name = GameData.COUNTRY_NAMES[i];
name = name.toLowerCase();
name = name.replaceAll("\\s","");
name = name.substring(0,4);
countryCodes[i] = name;
}
return;
}
public void countryId (String string) {
boolean found = false;
string = string.replaceAll("\\s", "");
if (string.length() >= 4) {
string = string.toLowerCase();
string = string.substring(0,4);
for (int i=0; (i
if (string.equals(countryCodes[i])) {
found = true;
countryId = i;
}
}
}
isError = !found;
return;
}
public boolean isError () {
return isError;
}
public boolean isDone () {
return isDone;
}
public int getCountryId () {
return countryId;
}
public int getNumUnits () {
return numUnits;
}
}
import java.util.ArrayList;
public class Player {
private int id;
private String name;
private int numUnits;
private ArrayList dice = new ArrayList();
Player (int inId, String inName, int inNumUnits) {
id = inId;
name = inName;
numUnits = inNumUnits;
return;
}
public void rollDice (int numDice) {
dice.clear();
for (int j=0; j
dice.add(1 + (int)(Math.random() * 6));
}
return;
}
public void addUnits (int inNum) {
numUnits = numUnits + inNum;
return;
}
public void subtractUnits (int inNum) {
numUnits = numUnits - inNum;
return;
}
public int getId () {
return id;
}
public String getName () {
return name;
}
public int getNumUnits () {
return numUnits;
}
public ArrayList getDice () {
return dice;
}
public int getDie (int dieId) {
return dice.get(dieId);
}
}
public class Sprint2 {
public static void main (String args[]) {
Board board = new Board();
UI ui = new UI(board);
Player[] players = new Player[GameData.NUM_PLAYERS_PLUS_NEUTRALS];
Player currPlayer;
Card card;
int playerId, countryId, numUnits, numCards;
String name;
ui.displayString("ENTER PLAYER NAMES");
ui.displayMap();
for (playerId=0; playerId
if (playerId < GameData.NUM_PLAYERS) {
name = ui.inputName(playerId);
numUnits = GameData.INIT_UNITS_PLAYER;
} else {
name = "Neutral " + (playerId - GameData.NUM_PLAYERS + 1);
ui.displayName(playerId,name);
numUnits = GameData.INIT_UNITS_NEUTRAL;
}
players[playerId] = new Player (playerId, name, numUnits);
}
ui.displayString(" DRAW TERRITORY CARDS FOR STARTING COUNTRIES");
Deck deck = new Deck();
for (playerId=0; playerId
currPlayer = players[playerId];
if (playerId < GameData.NUM_PLAYERS) {
numCards = GameData.INIT_COUNTRIES_PLAYER;
} else {
numCards = GameData.INIT_COUNTRIES_NEUTRAL;
}
for (int i=0; i
card = deck.getCard();
ui.displayCardDraw(currPlayer, card);
currPlayer.subtractUnits(1);
board.addUnits(card, currPlayer, 1);
}
}
ui.displayMap();
ui.displayString(" ROLL DICE TO SEE WHO REINFORCES THEIR COUNTRIES FIRST");
do {
for (int i=0; i
players[i].rollDice(1);
ui.displayDice(players[i]);
}
} while (players[0].getDie(0) == players[1].getDie(0));
if (players[0].getDie(0) > players[1].getDie(0)) {
playerId = 0;
} else {
playerId = 1;
}
currPlayer = players[playerId];
ui.displayRollWinner(currPlayer);
ui.displayString(" REINFORCE INITIAL COUNTRIES");
while (currPlayer.getNumUnits() > 0) {
ui.inputPlacement(currPlayer, currPlayer);
countryId = ui.getCountryId();
currPlayer.subtractUnits(3);
board.addUnits(countryId, currPlayer, 3);
ui.displayMap();
for (int i=GameData.NUM_PLAYERS; i
ui.inputPlacement(currPlayer, players[i]);
countryId = ui.getCountryId();
currPlayer.subtractUnits(1);
board.addUnits(countryId, currPlayer, 1);
ui.displayMap();
}
playerId = (++playerId)%GameData.NUM_PLAYERS;
currPlayer = players[playerId];
}
return;
}
}
Game Data
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 int INIT_UNITS_PLAYER = 36;
public static final int INIT_UNITS_NEUTRAL = 24;
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();
}
}
-
If the other human players armies are eliminated, the app should display the name of the winner
and a game over message.
-
The user should receive appropriate error messages if their commands are invalid.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
