Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA JUNIT HELP - Looking to write some basic Unit Tests on this piece of Code. What could I write? The Code is just a

JAVA JUNIT HELP - Looking to write some basic Unit Tests on this piece of Code. What could I write? The Code is just a small piece of a basic game of RISK. **

public static void main (String [] args) { Board board = new Board(); UI ui = new UI(board); int playerId, countryId; // Array to hold the players dice roll int[] dice = new int[GameData.NUM_PLAYERS]; // if player 2 throws a higher dice, OneGoesFirst is set to false boolean OneGoesFirst = true; // Displays the background as a image try { MapPanel.boardImage = ImageIO.read(Sprint1.class.getResource("map_background.jpg")); } catch (IOException ex) { System.out.println("Could not find the image file " + ex.toString()); } // display blank board ui.displayMap(); // Initialize the players and neutrals Player p1 = new Player(null,MapPanel.PLAYER_COLORS[0],GameData.PLAYERS_NUM_ARMIES); Player p2 = new Player(null,MapPanel.PLAYER_COLORS[1],GameData.PLAYERS_NUM_ARMIES); Player n1 = new Player("NeutralOne",MapPanel.PLAYER_COLORS[2],GameData.NEUTRAL_NUM_ARMIES); Player n2 = new Player("NeutralTwo",MapPanel.PLAYER_COLORS[3],GameData.NEUTRAL_NUM_ARMIES); Player n3 = new Player("NeutralThree",MapPanel.PLAYER_COLORS[4],GameData.NEUTRAL_NUM_ARMIES); Player n4 = new Player("NeutralFour",MapPanel.PLAYER_COLORS[5],GameData.NEUTRAL_NUM_ARMIES); Player[] players = {p1, p2, n1, n2, n3, n4}; // get player names for (playerId=0; playerId " + players[playerId].getName()); } } // add units countryId = 0; for (playerId=0; playerId " + dice[playerId]); //When player 2 rolls the dice, compare both players rolls if(playerId == 1) { // Player 2 has a higher roll if (dice[playerId] > dice[playerId - 1]) { ui.displayString("\t --" + players[playerId].getName() + " goes first--"); OneGoesFirst = false; } // Player 1 has a higher roll else if (dice[playerId] < dice[playerId - 1]) { ui.displayString("\t --" + players[playerId - 1].getName() + " goes first--"); } // If its a draw, reset the loop else { ui.displayString("\t --Draw: Both players re-roll--"); playerId--; playerId--; } } } // Repeats loop until 'roll' is entered else{playerId--;} } // Switches players names around so that player 2 becomes player 1 if(!OneGoesFirst){ String temp = players[0].getName(); players[0].setName(players[1].getName()); players[1].setName(temp); } ui.displayString("Both player have 36 units to reinforce their territories. "); // Show the abbreviated versions of the countries for(int i = 0; i < GameData.NUM_COUNTRIES; i++){ ui.displayString(GameData.COUNTRY_NAMES[i] + " = " + GamePlay.ABBREV_COUNTRY_NAMES[i]); } // Displays the players colour for(playerId=0; playerId" + players[playerId].getName() + "'s colour: " + players[playerId].getColorName()); } playerId = 0; // Players choose territories they occupy to reinforce while(players[playerId].getNumArmies() > 0) { for (playerId = 0; playerId < GameData.NUM_PLAYERS_PLUS_NEUTRALS; playerId++) { // Human players go first if(playerId < GameData.NUM_PLAYERS){ // On screen prompt ui.displayString(players[playerId].getName() + ": Choose a territory you control"); // Reads the users input and converts it to a string String command = ui.getCommand(); // If the string iss valid, convert it to it's integer position in the array if(Arrays.asList(GamePlay.ABBREV_COUNTRY_NAMES).contains(command)) { int unit = Arrays.asList(GamePlay.ABBREV_COUNTRY_NAMES).lastIndexOf(command); // Checks whether the player occupies the enter command if (playerId == board.getOccupier(unit)) { board.addUnits(unit, playerId, GameData.PLAYER_UNITS); ui.displayMap(); // Update player details, show user players[playerId].removeUnits(GameData.PLAYER_UNITS); ui.displayString("> " + GameData.COUNTRY_NAMES[unit] + " :" + board.getNumUnits(unit) + "\t[" + players[playerId].getNumArmies() + " remaining units]"); } else{ // Resets the loop, the player doesn't occupy that territory ui.displayString(players[playerId].getName() + " does not occupy " + GameData.COUNTRY_NAMES[unit]); playerId--; } } else { // Resets the loop, the player entered an incorrect command ui.displayString(players[playerId].getName() + ": " + command + " is not a valid input"); playerId--; } }else{ // Create an array of all possible countries int[] occupied = new int[GameData.INIT_COUNTRIES_NEUTRAL]; int j = 0; for (int i=0; i                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

=+How should it be delivered?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago