V22.0101 Homework Assignment 3 Fall 1998
A Game of Craps
Section 3; Marateck

Assigned: Thur Oct 8
Due: Thurs Oct 29, 11:59 pm
Midterm Exam: Tues Oct 27

In this assignment you will write a program to play craps and to bet on the outcomes of the games. It is very important that you make sufficient progress on the assignment before the midterm exam. Especially, you should get some practice correctly implementing while and/or repeat loops before the exam.

Craps is a simple dice game. You roll two dice. If you get a 7 or an 11 on this first roll you win; if you get a 2 or a 12 on the first roll you lose; otherwise the result of the first roll is called your point. You then continue to roll the dice until you match the point (in which case you win) or until you roll a 7 (in which case you lose).

You are to write a program that allows the user to place a bet (on either winning or losing) and play a game of craps. It should continue until the person doesn't want to play anymore or until he or she runs out of money. Have the player start with \$100 dollars. Don't allow bets that are less than 0 or more than the player has.

The computer rolls the dice by setting each of the two dice values to a random value between 1 and 6 and adding the two values together.

Here is a sample run.

[Some introductory remarks....]

You have \$100 dollars. How much would you like to bet? \$40
Are you betting on winning(y/n)? y

Press return key to roll the dice.

You rolled a 3 and a 4 for a total of 7.

Congratulations! You won the game and the bet!
You won \$40.
You now have \$140.

Would you like to play again (y/n)? j
That was not a valid response. Please type y or n.
Would you like to play again(y/n)? y
================================================================

You have \$140. How much would you like to bet? \$150
That's more money than you have.
You have \$140. How much would you like to bet? \$130
Are you betting on winning(y/n)? n

You rolled a 3 and a 2 for a total of 5.

Press return key to roll the dice.

You rolled a 3 and a 5 for a total of 8.

Press return key to roll the dice.

You rolled a 1 and a 4 for a total of 5.

You won the game but you lost the bet.
You lost \$130.
You now have \$10.

Would you like to play again(y/n)? n

==================================================================

Bye. You lost \$90.

As the sample shows, you should reject anything other than a ``y" or a ``n" when asking if the user wants to play again. Feel free to make the output nicer as long as it includes the basic information shown here.

The main execution of your program should call the following procedures (which, yes, you need to define):

get_bet
This needs to take three parameters: one for the current amount the that person has, one for the amount of the bet, and a boolean that tells whether the player bet to win. It should tell the player the current amount of money that he or she has and then prompt for the amount of the wager and whether the bet is to win. It should do the error checking outlined above.
play_game
This takes one parameter, a boolean that states whether the player wins. This procedure does the bulk of the work. It should have some procedures of its own, but the design of that is left to you.
calc_money
The purpose of this procedure is to calculate the amount of money that the player has after a game, so it needs four parameters: the amount of money the player has, the amount of the bet, whether the bet was to win, and the result of the game.
disp_message
This procedure takes four parameters: the result of the game, whether the bet was to win, the amount of the bet and the amount of money that the player now has. It prints an appropriate message (``Oh, too bad. You won the game but you bet against yourself. You lost \$25. You now have \$75").
play_again
This also takes one parameter, a boolean that states whether the player wants to play again. It should prompt for and then read the player's answer, doing appropriate error checking.

Be sure not to ask a player who has run out of money if he or she would like to play again.

Design your program so that it fits the description given above, though you may (and should, in some cases) add extra procedures. Your assignment will be graded partially on whether it executes correctly, but programming style will also count considerably: comments, good identifier names, appropriate constants, no non-local variable accesses, a parameter should be declared as var parameters only if it needs to be, and generally readable code.

Sam Marateck
WED Oct 7 13:15:39 EDT 1998