CPSC 225: Intermediate Programming, Spring 2003
Programming Assignment 1: Craps



THE FIRST PROGRAMMING ASSIGNMENT in C++ asks you to write a program that simulates the dice game known as craps. The idea is to get an idea of the odds of winning this game by simulating a large number of games and computing the percentage of games that are won. The program will use input and output, random numbers, and control statements such as if, for, and while. It is possible to write the entire program in a single main() routine.

This program is due in class next Wednesday, January 22. You should, of course, start working on it as soon as possible so that you can get help if you run into trouble. Your program must follow all style rules, including in particular rules about commenting, indentation, and naming of variables. Please print out a copy of your program using the a2ps command and turn it in in class. Your program should be available in your math account so that it can be run for testing purposes during grading.


The game of craps is played by rolling a pair of dice. Depending on the value that comes up on the dice, the game might be over in one roll or it might take several rolls. Here are the rules for one game:

The player rolls the pair of dice. If the total on the dice is 2, 3, or 12, then the game is lost immediately. If the total on the dice is 7 or 11, then the game is won immediately. If the total is any other number, then the game continues. The total on the dice becomes the "point." The player then rolls the pair of dice over and over until one of two things happen: If the player's roll is a 7, the player loses the game. If the player's roll is equal to the "point" (the total from the first roll), then the player loses. (Otherwise, the player keeps rolling.)

The program that you write should work as follows: The program should first ask the user how many games of craps to play. The program should read the user's input. It should then simulate the specified number of games and should count the number of games that are won. The program should not produce any output while the games are being simulated. After all the games have been played, the program should print the number of games that were won and the percentage of games that were won.

A compiled program that does all this can be found in the file /home/cs225/craps. You can try running this if you want to see how your program should work. Note that simulating even 1000000 games will only take a few seconds.


David Eck