Due by the end of Tuesday, November 28
Create a new project L11 for the program ChutesAndLadders.
Run the program. There's no input except for pressing the enter key when prompted. You should see output that looks something like this:
The actual numbers it rolls will differ from one run to the next, but eventually the program should roll a six and then end. Any other roll has the player "slide down a chute" back to the start.
Note the class constants for the Scanner and the Random number generator. Most programs will need only one of each, and so we can create static constants for each, and use them in whichever method needs them. That saves having to pass them around from one method to another.
makeBoard()
method
and examine the code.
Right now the board is mostly made up of zeroes, except for space 6, which is a six:
0 0 0 0 0 0 6 0 0 0 0 0
The numbers in the array show where there are chutes (or ladders) by giving the number of the space that this space leads to. So spaces 1, 2, 3, 4 and 5 all have chutes down to 0 (as do spaces 7, 8, 9, 10 and 11). If you land on (say) space 4, you slide down the chute to space 0. Space 6, on the other hand, has no chute or ladder. The array has value 6 there, indicating that if you get to space 6, you stay at space 6.
OK, so let's get rid of all the chutes. Set each space in the array to its own value.
0 1 2 3 4 5 6 7 8 9 10 11
In case you're wondering, six is the winning space. Anyone who gets to six (or past) wins the game. The array continues on past six because someone on space five might roll a six and need to move to space eleven.
Next, change NUM_SPACES to 10, so the game will last a bit longer.
Run the program again and make sure that the player never slides down a chute.
Find the place where we advance to the new position (in main). Revise the code so that the position never goes past the winning space. (That is, NUM_SPACES is the highest value allowed for position.)
Run the program again to ensure the player never passes position 10.
Change the position variable to an int[]
with the appropriate number of elements
(one for each player).
Add a new variable for the current player.
Start it at zero.
Now instead of having one position (for one player) we have multiple positions -- one for each player. For example, this array:
shows player 0 at position 2, player 1 at position 5, and player 2 at position 4.
2 5 4
Everywhere we used position
before,
we need to use the position of the current player --
extracted from the corresponding location of the position array:
for example: position[currentPlayer]
.
Yes, everywhere. I know thatposition
is sometimes used as an index into an array. The changed code will use an array element as an index into (another) array. That's just fine! The index into an array just needs to be an int value in the correct range. It can beIt could even be a calculation involving any or all of those (10 * r + c, or i + 3 + someIntArray[j] + someIntMethod(10 - someIntArray[i])). So long as you're careful to make sure you don't go outside the bounds, you are fine!
- a literal constant (like 1 or 5),
- a variable (like i or position),
- an element of an array of int (like position[currentPlayer]), or
- a call to a method that returns an int (like maybe rollDie()).
When you're done, all the red squiggles that appeared when you changed the position to an array should be gone.
Next add the current player number to the output in each place the program prints "Player ...". There's more than one player now, so we need to give them numbers to tell them apart. It's fine that the numbers start at zero!
Now find the code where the variable winner gets set to 0. change it so that it gets set to the current player.
One last change. After the current player has moved, we need to go on to the next player. Add one to the current player, but wrap back around to zero if we go past the last player.
Run the program again to ensure that each player gets a turn.
Please submit the game with 3 players and 10 spaces, but feel free afterwards to make the board bigger and add code to ask the user how many players they'd like to have.
Submit this/these files:
You will be graded on the following:
position
in the loop has been replaced with an access to the appropriate element of the array.
makeBoard