Homework 3
User-defined functions: Modifying the craps game program

Due: Tuesday, November 13, 2012 at 5pm

Outcomes

After successfully completing this assignment, you will be able to...

Before Starting

Read Chapter 5 of Deitel & Deitel, particularly sections 5.10 and 5.11.

Use this link to access the Deitel & Deitel home page for the textbook. In the left margin there is a link for downloading code and examples. You will be modifying the code from Figure 5.14 (the "craps" program). Download and study the program you will be modifying. Run the program several times and make sure you thoroughly understand how it works before you start working on this assignment.

The Assignment

In this assignment you will modify the craps program so that it contains the following enhancements: The program will be modified so that the portion of the program that plays one game of craps will be packaged as a function. In addition, separate functions will be defined to initialize the bank balance, verify the wager, adjust the bank balance after a game is played, and read in a yes or no answer. Here are the specifications for each of the functions:

initializeBalance This function prompts the user to enter a positive whole-dollar amount that will be used as the initial bank balance. A valid (i.e. positive) response is read in, and the value of the bank balance is returned by the function.

validWager This function checks the wager to make sure it doesn't exceed the player's current bank balance. If the wager is less than or equal to the current bank balance, the function returns 1 (true), otherwise it returns 0 (false).

playGame plays a single game of craps. It returns to the caller one of the enumerated constants WON or LOST. (This function is the one that contains most of the code from Figure 5.14.)

adjustBalance This function either adds the wager to or subtracts the wager from the player's current balance, depending upon whether the last game played was WON or LOST.

getYesOrNo This function asks if the player would like to play another game of craps. The function checks the response to make sure it is either 'y' or 'n'. The function will repeatedly ask for a y/n response until a valid response is entered. The answer (either 'y' or 'n') is returned to the calling function.

For each function you should write pre- and post-conditions, design the function as a black box (you don't have to turn in the black box), define a prototype, write a stub, and, once the program compiles successfully with each stub, fill in the function definitions (one at a time).

Include files

Assumptions and Restrictions

The main function should call getYesOrNo only as long as the player's balance is greater than 0 (once the player's balance goes to 0, the player is not allowed to continue).

You may assume that when the program asks for a number, the user enters a number.

When the program asks for a character ('y' or 'n'), you may not make any assumptions about what the user enters. See the note about mixed numeric and character input, below.

Sample Execution

Enter initial bank balance (positive, whole-dollar amount):  1000

Balance = $1000
Enter wager:  100
Player rolled 5 + 6 = 11
Player wins

Balance = $1100
Do you want to play another game?  (y or n):  y
Enter wager:  1500
Your wager must not exceed your current balance.
Enter a new wager:  900
Player rolled 3 + 1 = 4
Point is 4
Player rolled 4 + 5 = 9
Player rolled 5 + 5 = 10
Player rolled 5 + 6 = 11
Player rolled 3 + 3 = 6
Player rolled 3 + 5 = 8
Player rolled 6 + 2 = 8
Player rolled 1 + 5 = 6
Player rolled 3 + 5 = 8
Player rolled 5 + 6 = 11
Player rolled 3 + 1 = 4
Player wins

Balance = $2000
Do you want to play another game?  (y or n):  2000
You must answer y or n.
Do you want to play another game?  (y or n):  y
Enter wager:  2000
Player rolled 6 + 5 = 11
Player wins

Balance = $4000
Do you want to play another game?  (y or n):  n

Your final balance is $4000

A note about mixed numeric and character input

This program will require a deeper understanding of how C does keyboard input. When reading in single characters (like y or n) it is usually easier to use the C function getchar() instead of using scanf(). getchar() returns the next single character from the input stream. Look at the Sample Execution above. You would use a printf() statement to output the prompt, "Do you want to play another game? (y or n): ". Now think of what the user types in response...not the single character y, but two characters, y and the ENTER key (the C constant '\n'). You can read in the y with the statement
  ch = getchar();
That still leaves the newline character in the input buffer. This becomes a problem if the user had typed in an invalid response like 1800 instead of y or n, because the next time you try to use getchar() to read in a valid response it will read in the 8 of the number 1800, not the user's new input value.Similar problems are encountered if you use getchar() after reading in a numeric value; scanf() reads in the characters for the numeric value, and leaves the next character to be read (the newline character) still in the input buffer. A quick way to fix this problem is to follow each input statement with the loop:
  while (getchar() != '\n');
(Notice the placement of the semi-colon. The loop body is empty.) This loop essentially reads the rest of the characters on the line, and ignores them, until it reaches the end of the line. The next time your program executes an input statement (either scanf() or getchar()) it will get the input from the next line typed by the user.

Deliverables

Write a short file called README.txt that summarizes your program, how to run it, and any problems that you encountered. Submit your C source file and README.txt file to web-based turnin no later than 5pm on November 13. The name of the project is Homework 3. Programs submitted after 5pm on November 13 will be tagged as late, and will be subject to the late homework policy.

Grading

This assignment is worth 25 points. Programs must compile successfully in order to receive any credit. It is suggested that before you submit your program, compile it again on a CCC system to be sure that it compiles correctly.