Assignment 2 : C Primer Continued - Game of Craps.

Assignment Day           Tuesday, September 01, 2014
Due Date Tuesday, September 09, 2014
Note: Lab on the 10th may use this project,
so make sure that you complete this on time so that you are ready for the lab.
 

 

Learning Objectives:

  • Learn more C
  • Modify Code
  • Create Functions
  • Modularize code (and project structure, i.e,. create multiple .c and .h files).

Background Material:

Game of Craps ( overview here, and details here: its structure, rules, and history )

Functions, Functions calls, more familiarization of rand(), srand().

Download, and study the program craps.c ( here ).

Program Description

You will modify a program of craps so that is simulates play in a casino, i.e., how it would work in a 'gambling' situation.

As you may know, the game of craps is dice game, and is one of the most popular games of chance. The rules of the game are straightforward (and the provided craps.c contain some of these rules, if not all).

A player rolls two dice.
Each die has six faces.
These faces contain 1, 2, 3, 4, 5, and 6 spots.
After the dice have come to rest, the sum of the spots on the two upward faces is calculated.
If the sum is 7 or 11 on the first throw, the player wins.
If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses (i.e., the"house" wins).
If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point."
To win, you must continue rolling the dice until you "make your point."
The player loses by rolling a 7 before making the point.

Now for the challenging part of the assignment.

The game should allow for wagering. This means that you need to prompt that user for an initial bank balance from which wagers will be added or subtracted. Before each roll prompt the user for a wager. Once a game is lost or won, the bank balance should be adjusted.

You should make sure your program adheres to good coding style, e.g.,

All user input should be validated, e.g.,:

  1. Initial bank balance is a positive number
  2. Wagers must be less than or equal to the available balance.

As the game progresses, your program should print various messages to create some "chatter" such as, "Sorry, you busted!", or "Oh, you're going for broke, huh?", or "Aw c’mon, take a chance!", or "You're up big, now's the time to cashin your chips!"

Here are some assumptions of your program.

  • The gambler can play multiple games of craps.
  • The gambler wagers money at each game of craps.

Minimal Requirements:

You should structure you program as a set of functions, e.g., one game of craps should be structures as at least function. Here are some required functions (some of the below have more complete descriptions, such as return types, and parameters, and some do not - you should think about the missing parameters on your own).

  • void printGameRules(void): Prints out the rules of craps.
  • double getBankBalance(void): Prompts the player for an initial bank balance from which wagering will be added orsubtracted. The player entered bank balance (in dollars, i.e., $100.00) is returned.
  • int checkWager(double wager, double balance): This function gets a wager amount as a parameter and checks 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 exceeds - and returns 0 (false).
  • double getWager(void): Prompts the player for a wager on a particular roll. The wager is returned.
  • playGame(): Plays a single game of craps, rolling the dice as many times as necessary and printing the result to standard output (the terminal). It returns to the caller one of the enumerated constants WON or LOST.
  • double adjustBalance(double bankbalance, double wageamount, int addOrSubtractToBalance ): 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 should repeatedly ask for a y/n response until a valid response is entered. getYesOrNo should return 1 (TRUE) if the answer is 'y' and 0 (FALSE) if the answer is 'n'. Assumption: only called if the player has a non zero balance.

In the comments of your code, for each function, you should write pre- and post-conditions of the function, describe the parameters, and return type. As you code you should design the function as a black box, define the prototype, then write a stub, and, once the program compiles successfully with each stub, fill in the function definitions (one at a time).

Suggested Include Files:

  • stdio.h: provides printf(), scanf(), getchar()
  • stdlib.h: provides rand(), srand()
  • time.h: provides time, which as you know, "seed" the random number generator.

Useful Functions.

  • system("cls"): clear screen.
  • system("pause"): press any key to continue.

Sample Execution:

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

Balance = $1100.00
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.00
Do you want to play another game?  (y or n):  q
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.00
Do you want to play another game?  (y or n):  n

Your final balance is $4000.00

Notes on Character Input in C:

When reading in single characters (like y or n) in C it is usually easier to use the function getchar() instead of scanf(). getchar() returns the next 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' followed by the ENTER key (i.e., 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 q 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 newline, 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 drains 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. You may find getYesOrNo as a difficult function to write for this assignment. It is suggested that you get everything else working in your program first, and then add the code that relies on getYesOrNo.

Program Description:

You must submit the following files (i.e., all the files necessary to compile your program):
README.txt
*.c (all your .c files, you should have at least 2 .c files)
*.h (all your .h files, you should have at least 1 .h file).
Makefile

To submit the files, you will need to use the submit program. Your files need to be under a common subdirectory called "1730_program2". If the 1730_program2 subdirectory is directly under your home directory you execute the below command line while in your home directory:

submit 1730_program2 cs1730

NOTE 1: you need to be LOGGED ONTO nike.cs.uga when you execute the command

NOTE 2: look at the example Makefile and program available from the scheduler page.

NOTE 3: be aware of white space in your makefiles

Appendix A – pseudo-random generators (modifed from Internet Resources)

Many engineering and scientific problems require programs to use random numbers. For example, random numbers can be used to simulate noise in an electronic circuit or arrival times of events in the external world. For purpose like these, random number generators are used. A random number generator is a function that returns a seemingly random value each time it is called. A large body of mathematical theory exists for random number generators, dealing with their behavior, the quality of their randomness, and other factors.
Linux provides several random number generators. In this assignment, the function
int rand(void);
returns a new random number each time it is called. The random values are in the range
     0 .. RAND_MAX,
where RAND_MAX is as least as large as 215-1 (i.e., 32,767). If you want a random number in the range, say, 0 .. 5, then you can generate one with the expression
rand() % 6
However, random numbers are particularly vexing when trying to debug a program, because they produce a different value each time. Therefore, random number generators are typically designed to accept a seed (i.e., a starting value) and generate a sequence of random numbers from that seed. This way, you can always get the same sequence while you are debugging your program. Once your program is running correctly, you can then replace the seed with something different — for example, the time of day — to generate more realistically random values.
To seed the Linux random number generator, the function
void srand (unsigned int seed);
may be used. The default value for seed is 1.
In the craps.c file, you will notice that srand is called with an argument time(NULL), which returns the time in seconds since an arbitrary beginning used by all Unix and Linux systems. If you have problems during your debugging, it is suggested that you temporarily comment out the call to srand().