Question: 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

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 doesnt exceed the players 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 players 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 ' '). 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 users 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() != ' '); (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

Here is what I have so far... Make sure to create a .h file. This is plain C

craps.c :

/* Fig. 5.10: fig05_10.c * Game of Craps from Deitel & Associates, * Craps */ #include #include #include /* contains prototype for function time */

/* enumeration constants represent game status */ enum Status { CONTINUE, WON, LOST };

int rollDice( void ); /* function prototype */

/* function main begins program execution */ int main(void) { int sum; /* sum of rolled dice */ int myPoint; /* point earned */

enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */

/* randomize random number generator using current time */ srand( time( NULL ) );

sum = rollDice(); /* first roll of the dice */

/* determine game status based on sum of dice */ switch( sum ) {

/* win on first roll */ case 7: case 11: gameStatus = WON; break;

/* lose on first roll */ case 2: case 3: case 12: gameStatus = LOST; break;

/* remember point */ default: gameStatus = CONTINUE; myPoint = sum; printf( "Point is %d ", myPoint ); break; /* optional */ } /* end switch */

/* while game not complete */ while ( gameStatus == CONTINUE ) { sum = rollDice(); /* roll dice again */

/* determine game status */ if ( sum == myPoint ) { /* win by making point */ gameStatus = WON; /* game over, player won */ } /* end if */ else {

if ( sum == 7 ) { /* lose by rolling 7 */ gameStatus = LOST; /* game over, player lost */ } /* end if */ } /* end else */

} /* end while */

/* display won or lost message */ if ( gameStatus == WON ) { /* did player win? */ printf( "Player wins " ); } /* end if */ else { /* player lost */ printf( "Player loses " ); } /* end else */

return 0; /* indicates successful termination */

} /* end main */

/* roll dice, calculate sum and display results */ int rollDice( void ) { int die1; /* first die */ int die2; /* second die */ int workSum; /* sum of dice */

die1 = 1 + ( rand() % 6 ); /* pick random die1 value */ die2 = 1 + ( rand() % 6 ); /* pick random die2 value */ workSum = die1 + die2; /* sum die1 and die2 */

/* display results of this roll */ printf( "Player rolled %d + %d = %d ", die1, die2, workSum ); return workSum; /* return sum of dice */

} /* end function rollRice */

clearscreen.c:

#include #include

#include // tgetstr, tgeten - termcap library routines #include // tgetstr, tgeten - termcap library routines

// --------------------------------------------------------------- // http://stackoverflow.com/questions/ // 17271576/clear-screen-in-c-and-c-on-unix-based-system // --------------------------------------------------------------- // // Compile line on nike.cc // gcc -g clearscreen.c -o clearscreen -ltermcap // need to link to termcap library // ---------------------------------------------------------------

#define clearScreenEscapeSequence() printf("\033[H\033[J")

void clearScreenTermcap() { char buf[1024]; char *str;

tgetent( buf, getenv("TERM") ); str = tgetstr( "cl", NULL ); // linux fputs( str, stdout ); }

void display_menu() { printf(" Type 5, 55, 555 then enter to clear screen "); printf( "\t(termcap (5), escape sequence(55), system(555) " ); printf(" Enter 0 then enter to quit " ); printf(" Enter 9 then enter to display menu " ); printf(" (0 to quit, 9 for menu) Enter a Number (1-10)---> "); }

int main( int argc, char * argv[] ) { int file_stdin = 1; int number;

display_menu(); while ( scanf( "%d", & number ) == 1 ) { printf("We just read %d --> ", number); if( number==0 ) break; if( number==5 ) { clearScreenTermcap(); printf("(termcap clear) Enter a Number (1-10)---> "); } else if( number == 55 ) { clearScreenEscapeSequence(); printf("(escape sequence clear) Enter a Number (1-10)---> "); } else if( number == 555 ) { system("clear"); printf("(system clear) Enter a Number (1-10)---> "); } else if ( number == 9 ) display_menu(); } // end while } // end main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!