|
|
CS 2073, Fall 2005
Program 7
Chutes and Ladders
Week 8: Oct 17-21
Due (on time):
2005-10-24 23:59:59
Due (late):
2005-10-26 23:59:59
|
Program 7 must be emailed to:
nrwagner@cs.utsa.edu
following directions for: running
and submitting a C program, with deadlines:
- 2005-10-24 23:59:59 (that's Monday, 24 October 2005, 11:59:59 pm)
for full credit.
- 2005-10-26 23:59:59 (that's Wednesday, 26 October 2005, 11:59:59 pm)
for 75% credit.
|
Introduction:
- Implement the children's game Chutes
and Ladders. (Your program will pretend to be
one person playing this game.)
- Find all of the shortest winning games by simulating
tens of thousands of games.
The Game.
- Description:
This assignment simulates the play of a popular
children's game known as Chutes and Ladders, with a board as shown
in the picture above and the diagram below.
The board has 100 squares, numbered 1 through 100. Players
start just before the first square and roll a single die
(1 through 6 spots, all 6 equally likely) at each play.
A player's token is moved forward by the number of spots on the die.
After moving, the new square is examined for a ladder
(green line in the diagram below) leading up to a
higher-numbered square, and if one is present the player proceeds
up the ladder to the new square.
A square might also have the top of a chute
(blue line in the diagram below) in it,
leading to a lower-numbered square, and in this case the player "slides"
down the chute after arriving at the square.
Arriving either at the top of a
ladder or at the bottom of a chute has no significance.
The game ends when a player first reaches space 100.
Players are not permitted to move to square 100 if the current position
plus the number of spots is greater than 100.
(In that case you lose a turn.)
- Diagram Showing Ladders (green) and Chutes (blue):

- Java Applet Showing One or Two Persons
Playing the Chutes and Ladders Game:
The Board.
A simple way to keep track of the locations of chutes and ladders
is to use the following array:
int board[] =
{-1, 38, 0, 0, 14, 0, 0, 0, 0, 31, 0,
0, 0, 0, 0, 0, 6, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 84, 0, 0,
0, 0, 0, 0, 0, 44, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 26, 0, 11, 0,
67, 0, 0, 0, 0, 53, 0, 0, 0, 0,
0, 19, 0, 60, 0, 0, 0, 0, 0, 0,
91, 0, 0, 0, 0, 0, 0, 0, 0, 100,
0, 0, 0, 0, 0, 0, 24, 0, 0, 0,
0, 0, 73, 0, 75, 0, 0, 78, 0, 0};
Here the array index i goes from 0
to 100 inclusive. Position 0
has no meaning, but otherwise if position i is not zero,
it gives the far end of a chute or ladder starting at i.
A ladder will have board[i] greater
than i and a chute will have board[i]
less than i. For example, board[4] is
a 14, meaning that there is a ladder from
4 to 4.
Similarly board[16] is
a 6, meaning that there is a chute from
16 to 6.
To start writing the program, you just need to declare
the array board outside and
before the function definitions.
You could then use a function
int getend(int position)
that will return the value of board[position].
Playing the Game:
In your program, you can keep track of
the position of the piece on the board using a variable named
position, say, initially 0.
Use a roll() function to roll one die, and
put the result in a variable die, say.
Then update position to position + die.
Finally use the getend()
to check if the new position is at the start of a chute or ladder.
If it is, change position to the far end of
the chute or ladder. You also have to worry about position
100, since the board piece can't go beyond
that position, and if you reach position 100
the game is over.
Here is a roll() function that we have seen before.
This function is illustrated at the very end of
dice examples.
int roll() {
return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
}
Here are several sample runs of the game:
Sample runs.
Searching for the Shortest Game:
Add a loop to your program that will repeatedly play the game
looking for
the shortest possible sequence of dice throws that will finish
a game. Do not search for this answer
by hand, since the only interest is in what you can program.
Hints about this part: Each time you play another game, record the
sequence of dice rolls in a String. If the new game is shorter
than any you've had before, print this string, but otherwise
just discard the string and go on to the next trial game.
Here is a sample partial run of a program carrying out the search,
though the final results of the search are not included
(no games shorter than 14 moves):
Sample run.
What to turn in:
You should create and run three separate programs:
- The first plays one game of Chutes and Ladders, and prints
out a log of the game, as was shown before in:
Sample runs.
- The second searches for smaller and smaller lengths of
the game, each entry should be smaller than the previous one.
Continue this through tens of thousands of games (or more)
until you are convinced you won't hit on a shorter one.
The output could look like what was shown in:
Sample run.
- The third only prints games that are as short as
the shortest game. Print several dozen of these and see
how many truly distinct games you come up with.
(In case you move 8 spaces along in two moves, there are a number
of pairs of dice that would do this: 6 and 2, 5 and 3, 4 and 4,
3 and 5, or 2 and 6. However these are not considered real differences.
What you should email:
Refer to the submissions directions and to
deadlines at the top of this page. The text file that you submit
should first have Your Name, the Course Number,
and the Program Number. The rest of the file
should have the following in it, in the order below, and clearly labeled,
including at the beginning the appropriate item letters:
a, b, c, etc.
|
Contents of email submission
for Program 7:
Last Name, First Name; Course Number; Program Number.
- C source for item 1 above and the results of a run.
- C source for item 2 above and the results of a run.
- C source for item 3 above and the results of a run.
|
Revision date: 2005-10-12.
(Please use ISO
8601, the International Standard.)