CS 112 - Introduction to Computer Science II
Homework #1 |
1. Bash: Have you taken the bash tutorial in a prior course?
3. Chutes and Ladders: Chutes and Ladders is a popular Milton Bradley/Hasbro version of the ancient children's board game Snakes and Ladders.
The board consists of 100 numbered squares arranged in a 10-by-10 grid. The winner is the first player to reach square 100. Players start with their playing pieces off the board near square one. Players take turns spinning a spinner with numbers 1 - 6. After spinning, one advances one's piece according to the number of the spin. Exception: If this would take one beyond square 100, one does not advance at all. (Square 100 must be reached by exact count.) Some squares depict the beginning of a chute or ladder. After advancing to such a space, one slides the piece down the chute or climbs the piece up the ladder. If, after advancing and/or climbing, a player's piece is on square 100, the player wins and the game is over. Here is a table describing the chutes and ladders of the 2004 edition:
From Square | To Square |
---|---|
1 | 38 |
4 | 14 |
9 | 31 |
16 | 6 |
21 | 42 |
28 | 84 |
36 | 44 |
48 | 26 |
49 | 11 |
51 | 67 |
56 | 53 |
62 | 19 |
64 | 60 |
71 | 91 |
80 | 100 |
87 | 24 |
93 | 73 |
95 | 75 |
98 | 78 |
Your task: Simulate the game of Chutes and Ladders a given number of games for a single player and print statistics on the number of turns taken. Input/output behavior for random seed 0 is described by the partial transcript below (user input underlined):
$ java ChutesAndLaddersSimulator Number of games to simulate? 1000000 Average turns: 39.579545 Total games: 1000000 Turns Games 7 1465 8 4433 9 6865 10 9471 11 12387 12 15408 13 17248 14 18926 15 20152 . . . 300 1 309 2 326 1 332 1 379 1
The user runs the java program and is prompted for the number of games to simulate. After entering a positive integer, the simulator simulates the given number of games, both counting the total number of turns and counting the number of times a game has taken each number of turns. After all simulation is completed, the average number of turns, the total games, and a tab-separated table of turns and games is printed. The left column shows the number of turns in increasing order. For each left column number of turns, the right column shows the non-zero number of games that took that number of turns.
Implementation specifications: The main purpose of this exercise is to review/practice the use of arrays and ArrayLists. Represent the chutes and ladders table above in a fixed length array. We will discuss possible representations in class. Do not assume that a game will take at most some number of turns. For collection of game statistics in the output table, you will use an ArrayList, which can be dynamically resized to accommodate games of any practical number of turns. The ArrayList used in the execution shown above grew as needed to a maximum size of 380. Your implementation should do the same.