Question: This question relates to changing Chance-It to a multi-player game (for 2 or more players). a) Instead of player1 and player2, we are going to
This question relates to changing Chance-It to a multi-player game (for 2 or more players).
a) Instead of player1 and player2, we are going to have a field called players which is an ArrayList of Player objects. Write the appropriate line of Java here: (1 mark)
b) We are now going to store the dice in a field, so that they can be accessed by all the players: private Dice dice; Rewrite the ChanceItGame constructor given these two new fields (dice and players). The constructor now takes just one argument, the number of rounds. (3 marks)
c) Building on parts a) and b) we are now going to add the players to the game. This requires a new method called addPlayer that takes one argument, a String representing the players name. This method creates a new Player and adds it to the ArrayList of Players. Write the addPlayer method here. (3 marks)
d) Now you are going to rewrite method playGame. This method will just output a message if there are fewer than 2 players when it is invoked. Otherwise it will set all the players total scores to 0, and then, just as before, repeatedly invoke playOneRound to play the appropriate number of rounds and then invoke announceResults to print the results. (Do not change playOneRound or announceResults here, they will be changed later, see below.) (5 marks)
e) Now you are going to rewrite method playOneRound so that it works with multiple players. Each player has access only to the score of the person who preceded him, not to all the players scores. Apart from that, the logic is unchanged from the two-person game. (5 marks)
f) The only thing left to do is to update announceResults for the new multi-player game. The logic is a bit more complicated than for two players, as we could have a multi-way tie. You are to implement this method by creating another ArrayList of Players that will contain the player(s) with the highest score. Once you have created this list, print it out. (5 marks)
Copies of the java files relating to this game and API documentation for ArrayList is also provided







ChanceltGame.java ChanceItGame plays the game of Chance-It, the simple dice game *introduced by Joel Adams at Calvin College in the late 1990's *The only change from the version discussed in class is that there is now just one Player class (playerl and player2 are both instances of Player) @author D.L. Bailey&L.S. Marshall, SCE, Carleton University @version 1.04 September 23, 2007 public class ChanceItGame /**The number of rounds in a game.*/ private int numberOfRounds /**The player who takes the first turn in each round. */ private Player playerl; /**The player who takes the second turn in each round. */ private Player player2; The score obtained by the player who most recently took a turn. private int turnScore; Constructs a new Chance-It game with the specified number of rounds. @param rounds The number of rounds in a game. @param playerlName The name of the player who takes the first turn in each round @param player2Name The name of the player who takes the second turn in each round public ChanceItGame (int rounds, String playerlName, String player2Name) numberofROunds rounds turnScore0 Dice dice new Dice ; /Note that it is important that both players share the same * dice playerl-new Player (dice, playerlName) player2-new Player (dice, player2Name) Plays one game of Chance-It. public void playGame () /Reset both players' total scores. Doing this here allows us to play multiple games without having to create a new ChanceItGame object (and therefore new player objects) each * time playerl.resetTotalScore ) player2.resetTotalScore ) for (int round1 roundnumberOfRounds; round+) [ playOneRound (round) announceResults Play one round of the game @param round the round number private void playOneRound (int round) System.out.println ("Round "+round":") // The first player takes a turn. System.out.println("t" + playerl.name ) +":") turnScore-playerl.takeTurn (turnScore) // The second player takes a turn System.out.println("t" + player2.name ) +":") turnScore-player2.takeTurn (turnScore) Summarize the results of this round System.out.println("After round "+round "n"+ playerl.name )+"'s score:" + playerl.score+"In"+ player2.name )+"'s score: "+player2.score O) System.out.println ); Determines which player won the game or if there is a tie, *and displays the results private void announceResults () if (playerl.score) >player2.score )) System.out.println (playerl.name )+"wins the game."); System.out.println (player2.name )+"wins the game."); System.out.println("We have a tie.") else if (playerl.score) turnScore) turnScorecurrentRoll; Encapsulates the strategy used by this player to determine when to stop rolling the dice during each turn. As currently implemented, the player will always decide to stop when the current roll is 7 or higher. @return true when the player decides to end their current turn; otherwise returns false. private boolean stopRolling () return (currentRoll7) Dice.java import java.util.Random; Dice models a pair of 6-sided dice. @author D.L. Bailey, SCE, Carleton University eversion 1.01 January 22, 2006 public class Dice private Die diel; private Die die2; Constructor for objects of class Dice public Dice Random r new Random(); diel new Die (r) ; die2 new Die (r) ; Returns the result of rolling a pair of dice. The value returned is a pseudorandom integer between 2 and 12, inclusive. public int roll ) return diel.roll die2.rol1 ); Die.java import java.util.Random; * Die models a 6-sided die. @author D.L. Bailey, SCE, Carleton University eversion 1.01 January 22, 2006 public class Die private Random generator; Constructs a new Die object that uses the specified * random-number generator. @param r the random number generator used by this Die. public Die (Random r) generator = r; Returns the result of rolling a die. The value returned "is a pseudorandom integer between 1 and 6, inclusive public int roll ) /* The value returned by nextInt (6) will be a pseudorandom *integer value between 0 and 5, inclusive. Map this to a *value between 1 to 6, inclusive. return generator.nextInt (6)+ 1 ChanceltGame.java ChanceItGame plays the game of Chance-It, the simple dice game *introduced by Joel Adams at Calvin College in the late 1990's *The only change from the version discussed in class is that there is now just one Player class (playerl and player2 are both instances of Player) @author D.L. Bailey&L.S. Marshall, SCE, Carleton University @version 1.04 September 23, 2007 public class ChanceItGame /**The number of rounds in a game.*/ private int numberOfRounds /**The player who takes the first turn in each round. */ private Player playerl; /**The player who takes the second turn in each round. */ private Player player2; The score obtained by the player who most recently took a turn. private int turnScore; Constructs a new Chance-It game with the specified number of rounds. @param rounds The number of rounds in a game. @param playerlName The name of the player who takes the first turn in each round @param player2Name The name of the player who takes the second turn in each round public ChanceItGame (int rounds, String playerlName, String player2Name) numberofROunds rounds turnScore0 Dice dice new Dice ; /Note that it is important that both players share the same * dice playerl-new Player (dice, playerlName) player2-new Player (dice, player2Name) Plays one game of Chance-It. public void playGame () /Reset both players' total scores. Doing this here allows us to play multiple games without having to create a new ChanceItGame object (and therefore new player objects) each * time playerl.resetTotalScore ) player2.resetTotalScore ) for (int round1 roundnumberOfRounds; round+) [ playOneRound (round) announceResults Play one round of the game @param round the round number private void playOneRound (int round) System.out.println ("Round "+round":") // The first player takes a turn. System.out.println("t" + playerl.name ) +":") turnScore-playerl.takeTurn (turnScore) // The second player takes a turn System.out.println("t" + player2.name ) +":") turnScore-player2.takeTurn (turnScore) Summarize the results of this round System.out.println("After round "+round "n"+ playerl.name )+"'s score:" + playerl.score+"In"+ player2.name )+"'s score: "+player2.score O) System.out.println ); Determines which player won the game or if there is a tie, *and displays the results private void announceResults () if (playerl.score) >player2.score )) System.out.println (playerl.name )+"wins the game."); System.out.println (player2.name )+"wins the game."); System.out.println("We have a tie.") else if (playerl.score) turnScore) turnScorecurrentRoll; Encapsulates the strategy used by this player to determine when to stop rolling the dice during each turn. As currently implemented, the player will always decide to stop when the current roll is 7 or higher. @return true when the player decides to end their current turn; otherwise returns false. private boolean stopRolling () return (currentRoll7) Dice.java import java.util.Random; Dice models a pair of 6-sided dice. @author D.L. Bailey, SCE, Carleton University eversion 1.01 January 22, 2006 public class Dice private Die diel; private Die die2; Constructor for objects of class Dice public Dice Random r new Random(); diel new Die (r) ; die2 new Die (r) ; Returns the result of rolling a pair of dice. The value returned is a pseudorandom integer between 2 and 12, inclusive. public int roll ) return diel.roll die2.rol1 ); Die.java import java.util.Random; * Die models a 6-sided die. @author D.L. Bailey, SCE, Carleton University eversion 1.01 January 22, 2006 public class Die private Random generator; Constructs a new Die object that uses the specified * random-number generator. @param r the random number generator used by this Die. public Die (Random r) generator = r; Returns the result of rolling a die. The value returned "is a pseudorandom integer between 1 and 6, inclusive public int roll ) /* The value returned by nextInt (6) will be a pseudorandom *integer value between 0 and 5, inclusive. Map this to a *value between 1 to 6, inclusive. return generator.nextInt (6)+ 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
