The Token Pass game is represented by the TokenPass class. public class TokenPass { private int[] board;
Question:
The Token Pass game is represented by the TokenPass class.
public class TokenPass
{
private int[] board;
private int currentPlayer;
/** Creates the board array to be of size playerCount and fills it with
* random integer values from 1 to 10, inclusive. Initializes currentPlayer to a
* random integer value in the range between 0 and playerCount-1, inclusive.
* @param playerCount the number of players
*/
public TokenPass(int playerCount)
{ /* to be implemented in part (a) */ }
/** Distributes the tokens from the current player's position one at a time to each player in
* the game. Distribution begins with the next position and continues until all the tokens
* have been distributed. If there are still tokens to distribute when the player at the
* highest position is reached, the next token will be distributed to the player at position 0.
* Precondition: the current player has at least one token.
* Postcondition: the current player has not changed.
*/
public void distributeCurrentPlayerTokens()
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}
(a) Write the constructor for the TokenPass class. The parameter playerCount represents
the number of players in the game. The constructor should create the board array to contain
playerCount elements and fill the array with random numbers between 1 and 10, inclusive.
The constructor should also initialize the instance variable currentPlayer to a random number
between 0 and playerCount-1, inclusive.
Complete the TokenPass constructor below.
/** Creates the board array to be of size playerCount and fills it with
* random integer values from 1 to 10, inclusive. Initializes currentPlayer to a
* random integer value in the range between 0 and playerCount-1, inclusive.
* @param playerCount the number of players
(b) Write the distributeCurrentPlayerTokens method.
The tokens are collected and removed from the game board at the current player's position. These tokens are
distributed, one at a time, to each player, beginning with the next higher position, until there are no more
tokens to distribute.
Class information repeated from the beginning of the question
public class TokenPass
private int[] board
private int currentPlayer
public TokenPass(int playerCount)
public void distributeCurrentPlayerTokens()
Complete method distributeCurrentPlayerTokens below.
/** Distributes the tokens from the current player's position one at a time to each player in
* the game. Distribution begins with the next position and continues until all the tokens
* have been distributed. If there are still tokens to distribute when the player at the
* highest position is reached, the next token will be distributed to the player at position 0.
* Precondition: the current player has at least one token.
* Postcondition: the current player has not changed.
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates