L11

Due by the end of Tuesday, November 28

Starter Files:


SUBMIT / CHECK

Today's Activities

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:

We're going to play Chutes and ladders! ...press enter... Player rolls 2 Player reaches 2 Player slides down a chute to 0 ...press enter... Player rolls 3 Player reaches 3 Player slides down a chute to 0 ...press enter... Player rolls 6 Player reaches 6 ...press enter... Player 0 wins!

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.

Don't create static variables for other kinds of objects. Static variables make your program less robust (more likely to have a mistake), so we keep them to a minimum.
Activity 1
First thing we're going to do is modify the board so that players don't "slide down" every time. Find the 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.

We're going to play Chutes and ladders! ...press enter... Player rolls 5 Player reaches 5 ...press enter... Player rolls 1 Player reaches 6 ...press enter... Player rolls 6 Player reaches 12 ...press enter... Player 0 wins!
Activity 2
Notice that sometimes the player is said to reach a place after the winning spot. We'd like to avoid that.

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.

We're going to play Chutes and ladders! ...press enter... Player rolls 1 Player reaches 1 ...press enter... Player rolls 3 Player reaches 4 ...press enter... Player rolls 3 Player reaches 7 ...press enter... Player rolls 6 Player reaches 10 ...press enter... Player 0 wins!

Activity 3

OK, now lets put a chute and a ladder back into the game. Go back to the method that makes the board and add code to place: Run the program again -- often enuf to see that the chute and the ladder are there.
We're going to play Chutes and ladders! ...press enter... Player rolls 3 Player reaches 3 Player climbs up a ladder to 7 ...press enter... Player rolls 2 Player reaches 9 Player slides down a chute to 6 ...press enter... Player rolls 5 Player reaches 10 ...press enter... Player 0 wins!

Activity 4

This activity is a big one. We're going to add more players. Create a class constant for the number of players. Set it to three.

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:

2 5 4
shows player 0 at position 2, player 1 at position 5, and player 2 at position 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 that position 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 be
  • 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()).
It 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!

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.

We're going to play Chutes and ladders! ...press enter... Player 0 rolls 1 Player 0 reaches 1 ...press enter... Player 1 rolls 5 Player 1 reaches 5 ...press enter... Player 2 rolls 1 Player 2 reaches 1 ...press enter... Player 0 rolls 4 Player 0 reaches 5 ...press enter... Player 1 rolls 4 Player 1 reaches 9 Player 1 slides down a chute to 6 ...press enter... Player 2 rolls 2 Player 2 reaches 3 Player 2 climbs up a ladder to 7 ...press enter... Player 0 rolls 4 Player 0 reaches 9 Player 0 slides down a chute to 6 ...press enter... Player 1 rolls 1 Player 1 reaches 7 ...press enter... Player 2 rolls 2 Player 2 reaches 9 Player 2 slides down a chute to 6 ...press enter... Player 0 rolls 2 Player 0 reaches 8 ...press enter... Player 1 rolls 4 Player 1 reaches 10 ...press enter... Player 1 wins!

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:

  1. Program has been passed in revised. It compiles and running it plays the game.
  2. Number of spaces has been updated to ten.
  3. Number of players has a class constant set to three.
  4. The position variable has been changed to an int[] and initialized to the correct size.
  5. Variable for current player has been added and initialized properly.
  6. Every occurrence of position in the loop has been replaced with an access to the appropriate element of the array.
  7. No player's position is allowed to go past end of the board.
  8. Current player updated correctly at end of while loop ...
  9. ... unless current player is winner.
  10. Board is mostly not chutes or ladders ...
  11. ... but there is one chute from 9 to 6 ...
  12. ... and a ladder from 3 to 7.
  13. ... and all changes to the board are made in makeBoard
  14. All code meets the usual style requirements

SUBMIT   /   CHECK