Thanks
The requirements are as follows:
Requirement #1: Modify the program so that instead of flipping a coin, it
rolls two dice, printing out the numbers and the sum, and repeating until
the user requests an end to the game. The output looks like this: 6 - 4 : 10
3 - 2 : 5
2 - 5 : 7
6 - 1 : 7
Number of rolls: 4
Requirement #2: Now add control statements and user prompts so that it
actually plays a game of "craps." The rules of "craps" follow:
The user makes a bet and then rolls dice (simulated by the computer, which
displays the result of the roll)
If the dice (on the first roll) come up with a total of 7 or 11, the user
wins. (The bet would be added to the user's bank.)
If the dice come up 2 (snake eyes) on first roll, the computer wins. (The
bet would be subtracted from the user's bank.)
If the opening roll is anything other than 2, 7, or 11, that number becomes
the "point." The user throws the dice again (simulated by the computer) in
an attempt to win the game by rolling the point number again. If the user
throws a 2 or 7, however, before rolling the point again, the computer wins.
The program should keep track of the bets (user's bank account) and print
out results whenever the user requests it and when game is over. (An
alternate method is to have the user state his/her total worth at beginning
of game and have the game continue until the user either quits or runs out
of money!)
Here is what I have so far:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int number ;
int main ()
{
int role_of_dice () ; /*Prototype for role_of_dice function*/
char yn ;
int role_one ;
int role_two ;
int total ;
int total_roles = 0 ;
int bank = 50;
int bet = 0 ;
int point ;
int point_2 ;
printf ("\nWould you like to play a game of craps?") ;
printf ("\nYou will start with $50, coutisy of the bank.\nGo below $0 and
you lose, win over a $1000 and you break the bank!") ;
printf ("\nDo you want to play? Y/N :") ;
yn = getchar() ;
srand( (unsigned)time( NULL ) );
fflush (stdin) ;
while (yn == 'Y' || yn == 'y')
{
printf ("\nPlace your bet: ") ;
scanf ("%d", &bet) ;
total_roles++ ;
role_one = role_of_dice () ;
role_two = role_of_dice () ;
total = role_one + role_two ;
printf ( " %1d + %1d = %2d\n", role_one, role_two, total) ;
/*point = total ;*/
switch (total)
{
case 7 :
bank = bet + bank ;
printf ("\nYou win! Your bank is now %4d", bank) ;
printf ("\n%2d , %2d", total , point) ;
break ;
case 11 :
bank = bet + bank ;
printf ("\nYou win! Your bank is now %4d", bank) ;
printf ("\n%2d , %2d", total , point) ;
break ;
case 2 :
bank = bank - bet ;
printf ("\nSnake eyes! You lose! Your bank is now %4d", bank) ;
printf ("\n%2d , %2d", total , point) ;
break ;
default :
while )
printf ("\nI hate comupters") ;
printf ("\n%2d , %2d", total , point) ;
break ;
}
}
/*default :
total = point ;
printf ("%2d is the point\n", point) ;
if (total != point)
{
role_one = role_of_dice () ;
role_two =role_of_dice () ;
total = role_one + role_two ;
printf (" %1d + %1d = %2d\n", role_one, role_two, total) ;
}
else if (total = point) ;
{
printf ("You win! Would you like to roll again? Y/N\n") ;
}*/
/*printf ("\n Toss again? Y/N") ;
yn = getchar() ;
fflush (stdin) ;*/
/*printf (" Total number of roles = %-7d\n", total_roles) ;*/
if (yn != EOF)
{
printf("Warning: Encountered error in reading input.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/***************************************************************************
*/
int role_of_dice ()
{
int dice ;
int rollOne ;
int rollTwo ;
rollOne = rand() %6 + 1 ;
rollTwo = rand() %6 + 1 :
dice = rollOne + rollTwo ;
return printf ("%1d + %1d = %2d\n", rollOne, roleTwo, dice) ;
}
I think above is residue you don't use any more.
> int main ()
> {
> int role_of_dice () ; /*Prototype for role_of_dice function*/
Move the prototype to global, before main(). This works but is misleading,
role_of_function is
not restricted to being called by main. Putting it here misleads by causing
false inferrences on
the part of the reader of the code.
>
> char yn ;
> int role_one ;
> int role_two ;
> int total ;
> int total_roles = 0 ;
> int bank = 50;
> int bet = 0 ;
> int point ;
> int point_2 ;
>
> printf ("\nWould you like to play a game of craps?") ;
> printf ("\nYou will start with $50, coutisy of the bank.\nGo below $0 and
What is the bet when players bank is zero? What does he bet? His watch?
> you lose, win over a $1000 and you break the bank!") ;
I guess you haven't coded the 'break the bank' stuff yet. In order to do this
you need two accounts,
you only have one so far. I think. I like the notion of an 'update_accounts'
function.
It adds to one account and subtracts from the other. Otherwise, in the heat of
coding, you may
foregt one account or the other.
> printf ("\nDo you want to play? Y/N :") ;
> yn = getchar() ;
> srand( (unsigned)time( NULL ) );
> fflush (stdin) ;
>
> while (yn == 'Y' || yn == 'y')
> {
> printf ("\nPlace your bet: ") ;
> scanf ("%d", &bet) ;
> total_roles++ ;
> role_one = role_of_dice () ;
> role_two = role_of_dice () ;
> total = role_one + role_two ;
> printf ( " %1d + %1d = %2d\n", role_one, role_two, total) ;
> /*point = total ;*/
> switch (total)
> {
> case 7 :
> bank = bet + bank ;
just a nitpick. But when you get some experience, you will prefer: bank +=
bet;
> printf ("\nYou win! Your bank is now %4d", bank) ;
> printf ("\n%2d , %2d", total , point) ;
> break ;
> case 11 :
> bank = bet + bank ;
> printf ("\nYou win! Your bank is now %4d", bank) ;
> printf ("\n%2d , %2d", total , point) ;
> break ;
Look at syntax for switch statemwnt. You can combine code for cases 7 and 11
> case 2 :
> bank = bank - bet ;
> printf ("\nSnake eyes! You lose! Your bank is now %4d", bank) ;
> printf ("\n%2d , %2d", total , point) ;
> break ;
> default :
Here is a good place to insert a call to a newly written function. This code
is getting much too long.
You can think of 'making the point' as a kind of subgame. Perhaps a prototype
such as:
int make_point(int point); // return value is users gain (signed number)
> while )
Garble of your post upload? while ) makes no sense.
> printf ("\nI hate comupters") ;
> printf ("\n%2d , %2d", total , point) ;
> break ;
> }
> }
> /*default :
> total = point ;
> printf ("%2d is the point\n", point) ;
> if (total != point)
But the rules you posted sometimes cause a loss here. Where is the code for
that situation?
> {
> role_one = role_of_dice () ;
> role_two =role_of_dice () ;
> total = role_one + role_two ;
> printf (" %1d + %1d = %2d\n", role_one, role_two, total) ;
> }
> else if (total = point) ;
You probably want == above.
> {
> printf ("You win! Would you like to roll again? Y/N\n") ;
> }*/
>
>
> /*printf ("\n Toss again? Y/N") ;
> yn = getchar() ;
> fflush (stdin) ;*/
>
>
> /*printf (" Total number of roles = %-7d\n", total_roles) ;*/
> if (yn != EOF)
Where did EOF come from??? User pressed control+d? What? Its common to take y
or Y as meaning
yes and anything else as meaning no.
> {
> printf("Warning: Encountered error in reading input.\n");
> return EXIT_FAILURE;
> }
> return EXIT_SUCCESS;
> }
> /***************************************************************************
> */
>
> int role_of_dice ()
> {
> int dice ;
> int rollOne ;
> int rollTwo ;
> rollOne = rand() %6 + 1 ;
> rollTwo = rand() %6 + 1 :
> dice = rollOne + rollTwo ;
>
> return printf ("%1d + %1d = %2d\n", rollOne, roleTwo, dice) ;
printf does have a return value, but why pass it to caller? use printf(...);
return; on two lines
> }
>
>
>