Status: Started
  • TokenPass.java
Font Size
14
Parentheses autocomplete
Wrap lines
Editor
Theme
Code with blocks by default
Console Font Size
12
Console Theme
Display Server Graphics Screen
Show File Tab Bar
Debug Mode
2 TokenPass Part a (text only)
TokenPass.java
/** 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
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Output
Docs
Exercise
More
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
This student is viewing this assignment in English. View this page in English?

5 points

A multiplayer game called Token Pass has the following rules.

Each player begins with a random number of tokens (at least 1, but no more than 10) that are placed on a linear game board. There is one position on the game board for each player. After the game board has been filled, a player is randomly chosen to begin the game. Each position on the board is numbered, starting with 0.


The following rules apply for a player’s turn. 

  1. The tokens are collected and removed from the game board at that player's position.
  2. The collected tokens are distributed one at a time, to each player, beginning with the next player in order of increasing position.
  3. If there are still tokens to distribute after the player at the highest position gets a token, the next token will be distributed to the player at position 0.
  4. The distribution of tokens continues until there are no more tokens to distribute. 


The Token Pass game board is represented by an array of integers. The indexes of the array represent the player positions on the game board, and the corresponding values in the array represent the number of tokens that each player has. The following example illustrates one player's turn. 


Example

The following represents a game with 4 players. The player at position 2 was chosen to go first.


The tokens at position 2 are collected and distributed as follows.

1st token - to position 3 (The highest position is reached, so the next token goes to position 0.) 2nd token - to position 0

3rd token - to position 1

4th token - to position 2

5th token - to position 3 (The highest position is reached, so the next token goes to position 0.) 6th token - to position 0 


After player 2’s turn, the values in the array will be as follows. 


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.

}



Part 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.


Reset Code

Slides and Notes

No slides available for this video

About

Java (main)
Java Version 1.8.0_222