Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question relates to the strategies used in method stopRolling() in class Player. The two parts of the question are independent (they do not depend

This question relates to the strategies used in method stopRolling() in class Player. The two parts of the question are independent (they do not depend on each other).

a) Rewrite stopRolling() to implement the following strategy: A player will roll until he/she gets a roll that is an even number, at least 8, and higher than their opponents most recent score.

b) This time our players have decided to go with a simple strategy: Roll 3 times. While this is simple to write, it is a little trickier to program, as the number of rolls is not currently tracked, and this strategy will require changes in method takeTurn() as well as in stopRolling(). Make the changes required in takeTurn() (by crossing out and/or adding to whats given here), and then rewrite stopRolling() below to implement this new strategy. (5 marks)

image text in transcribed

Copies of the java files relating to this game and API documentation for ArrayList is also provided

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

public int takeTurn (int otherTurnScore) opponentsTurnScore-otherTurnScore; currentRoll-pairofDice.roll ) firstRollcurrentRoll; turnScorecurrentRoll; System.out.printin("ttFirst roll: "+ firstRol1); or if (stopRolling)) totalScore-totalScore turnScore; System.out.printin("ItltStopping with turn score"+ turnScore+ ", total scoretotalScore); return turnScore; currentRo 11 = parofDice . roll(); System.out.printin("tItNext roll: "currentRoll); if (currentRol1firstRoll) turnScore0; System.out.println("ttoops! Rolled a match!") System . out.println("\t\tStopping with turn score = " + turnscore + ", total score = " + tota!Score); return turnScore; // The current roll is not the same as the first roll if (currentRol > turnScore) turnScorecurrentRoll; 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 API Summary - Class ArrayList /1 Constructs an empty list with an initial capacity of ten. ArrayListO // Appends o (of type E) to the end of this list. Returns true if // successful, otherwise returns false. boolean add (E o) // Returns the number of objects stored in this 1ist. int size ) // Returns the object (of type E) at the specified position (index) // in the list. ndex must be >= 0 and = 0 and turnScore) turnScorecurrentRoll; 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 API Summary - Class ArrayList /1 Constructs an empty list with an initial capacity of ten. ArrayListO // Appends o (of type E) to the end of this list. Returns true if // successful, otherwise returns false. boolean add (E o) // Returns the number of objects stored in this 1ist. int size ) // Returns the object (of type E) at the specified position (index) // in the list. ndex must be >= 0 and = 0 and

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

Step: 3

blur-text-image

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

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

Global happiness polls.

Answered: 1 week ago