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:


The Game.
The Board. A simple way to keep track of the locations of chutes and ladders is to use the following array:

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.

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:

  1. The first plays one game of Chutes and Ladders, and prints out a log of the game, as was shown before in: Sample runs.

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

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

  1. C source for item 1 above and the results of a run.
  2. C source for item 2 above and the results of a run.
  3. C source for item 3 above and the results of a run.


Revision date: 2005-10-12. (Please use ISO 8601, the International Standard.)