Thread: craps.c

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    68

    craps.c

    Can somebody please help with this craps modification?

    Everything works fine, except when we get a win case, it won't loop back and play again. It just keeps winning. This is driving me nuts!

    Code:
    /*A player rolls two dice. If the sum is 7 or 11 on the first throw, the player wins.
    If the sum is 2,3 or 12 (craps), the player loses.  If the sum is 
    4,5,6,8,9 or 10 on the first throw, then that 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.
    */
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    //enumeration constants represent game status
    enum Status{CONTINUE, WON, LOST};
    
    
    //function prototype
    int rollDice(void); 
    int winQuotes(void);
    int loseQuotes(void);
    int crapsGame(void);
    int enterWager(void);
    
    
    /*int enterWager(void)
    {
            double bankBalance;
    	double  wager;
    	printf("Bank Balance = ");
            scanf("%lf", &bankBalance);
            printf("Balance is $%lf\n", bankBalance);
    
    
            printf("Please enter a wager:  ");
            scanf("%lf", &wager);
    
    
    
    
    	printf("Please enter a wager:  ");
            scanf("%lf", &wager);
    
    
            while(wager >= bankBalance)
            {
                    printf("Can't bet more than you have.\nPlease enter valid wager:  ");
                    scanf("%lf", &wager);
            }//end while
    
    
    }//end wager*/
    
    
    int winQuotes(void)
    {
    	int rn = rand() % 6;
    	switch(rn)
    	{
    		case 0:
    			printf("Very Lucky!\n");
    			break;
    		case 1:
    			printf("Big WINNER!!!\n");
    			break;
    		case 2:
    			printf("You WIN! PERFECT!\n");
    			break;
    		case 3:
    			printf("THE WINNER!\n");
    			break;
    		case 4:
    			printf("Hey there! You're cleaning me out over here!\n");
    			break;
    		case 5:
    			printf("Looks like I'm going broke!  Stop winning so much!\n");
    			break;
    	}//end switch
    }//end winQuotes
    
    
    int loseQuotes(void)
    { 
            int rn = rand() % 6;
            switch(rn)
            { 
                    case 0:
                            printf("uh-oh! You lost\n");
                            break;
                    case 1:
                            printf("Big LOSER!!!\n");
                            break;
                    case 2:
                            printf("You LOSE! NOT PERFECT!\n");
                            break;
                    case 3:
                            printf("YOU LOSE!\n");
                            break;
                    case 4:
                            printf("Hey there! I'm cleaning you out over here!\n");
                            break;
                    case 5:
                            printf("HA! Looks like you're going broke!  Keep it up!\n");
                            break;
            }//end switch
    }//end loseQuotes
    
    
    
    
    //roll the 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 of the two die rolls
    
    
    	//display results of the roll
    	printf("Player rolled %d + %d = %d\n", die1, die2, workSum);
    	return workSum;
    }//end rollDice
    
    
    int main(void)
    {
    //	int sum;//sum rolled dice
    //	int myPoint;
    //	double bankBalance;
    ////	double wager;
    	
    	enum Status gameStatus;//can contain CONTINUE, WON, or LOST
    
    
    	//randomize RNG using time
    	srand(time(NULL));
    
    
    //	sum = rollDice(); //roll the dice
    
    
     	crapsGame();
    
    
    }//end main
    
    
    int crapsGame(void)
    {
            int sum;//sum rolled dice
            int myPoint;
    	double bankBalance;
    	double wager;
    
    
            printf("Bank Balance = ");
    	scanf("%lf", &bankBalance);
    	printf("Balance is $%lf\n", bankBalance);
    
    
             printf("Please enter a wager:  ");
             scanf("%lf", &wager);
    
    
            while(wager > bankBalance)
            {
                  printf("Can't bet more than you have.\nPlease enter valid wager:  ");
                  scanf("%lf", &wager);
            }//end while
    
    
    
    
            enum Status gameStatus;//can contain CONTINUE, WON, or LOST
    
    
            sum = rollDice(); //roll the dice
    
    
    	while(bankBalance != 0)
    	{
    		//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--if WIN or LOST has not been achieved
    			default:
    				gameStatus = CONTINUE;
    				myPoint = sum;
    				printf("Point is %d\n", myPoint);
    				break; //optional
    		}//end switch
    
    
    			//while game not complete
    		while(gameStatus == CONTINUE)
    		{
    		
    			sum = rollDice(); // roll the dice again
    		
    			//determine game status
    			if(sum == myPoint)//win by making point
    			{
    				gameStatus = WON;
    			}//end if
    			else
    			{
    				if(sum == 7)//lose by roling a 7
    				{
    					gameStatus = LOST; //game over
    				}//end if
    			}//end else
    		}//end while
    
    
    		if(gameStatus == WON)
    		{
    			printf("Player Wins\n");
    			bankBalance += wager;
    			printf("New Balance = $%lf\n", bankBalance);
    			winQuotes();
    //			break;
    //		        printf("Please enter a wager:  ");
      //                      scanf("%lf", &wager);
    //
    //	                while(wager > bankBalance)
      //      	        {
        //            	        printf("Can't bet more than you have.\nPlease enter valid wager:  ");
          //                  	scanf("%lf", &wager);
    	//                }//end while
    		}//end if
    	        if(gameStatus == LOST)
            	{
                    	printf("Player Loses\n");
    			bankBalance -= wager;
    			printf("New Balance = $%lf\n", bankBalance);
    			loseQuotes();
    //			break;
    //	            	printf("Please enter a wager:  ");
    //	  	        scanf("%lf", &wager);
    //
      //              	while(wager > bankBalance)
     //			{
       //                     	printf("Can't bet more than you have.\nPlease enter valid wager:  ");
    //	                        scanf("%lf", &wager);
      //      	        }//end while
            	}//end if
    
    
    //	       printf("Please enter a wager:  ");
    //	       scanf("%lf", &wager);
    //
    //	     	while(wager > bankBalance)
    //	        {
      //      	        printf("Can't bet more than you have.\nPlease enter valid wager:  ");
        //            	scanf("%lf", &wager);
    //	        }//end while
    	}//end while
    }//end crapsGame
    Any help is appreciated. Thanks in advance

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    • You never check whether scanf("%lf",&var) actually reads something into var or not.
      The function returns the number of matched assignments, i.e. 1 if it did read a number.
      If it returns less than 1, var will still contain whatever value it had before the scanf() call.
    • You have a number of functions that should return an int, but don't have any kind of return statements. At least my compiler complains:
      Code:
      jwall.c: In function ‘main’:
      jwall.c:135:17: warning: unused variable ‘gameStatus’ [-Wunused-variable]
      jwall.c: In function ‘crapsGame’:
      jwall.c:276:1: warning: control reaches end of non-void function [-Wreturn-type]
      jwall.c: In function ‘main’:
      jwall.c:148:1: warning: control reaches end of non-void function [-Wreturn-type]
      jwall.c: In function ‘loseQuotes’:
      jwall.c:104:1: warning: control reaches end of non-void function [-Wreturn-type]
      jwall.c: In function ‘winQuotes’:
      jwall.c:77:1: warning: control reaches end of non-void function [-Wreturn-type]
      and the complaints are for a reason. At least add a return 0 at the end of main()!
      If the other functions do not return any values, just declare them void instead of int.
    • Weird indentation and commented out code lines make it hard to read.
      If you want our help, the least you can do is make it not difficult.
      Proper code formatting is considered a basic courtesy!
    • You only roll the dice once; it's outside your bank balance loop.
      If you win, you keep winning, because the dice never changes.
    • bankBalance is a floating-point number, but you explicitly compare against zero.
      This means that if you have 0.5, then play four games worth 0.1 each, you still have 0.000000000000000028 to play with. Just because a floating point number looks exact, does not mean it is not exact: the closest value a double can represent to 0.1 is either 0.09999999999999999167 or 0.10000000000000000555, depending on your definition of "closest".
      You should instead use an inequality, something like (bankBalance <= 0.005), to test for the loop to exit.


    Save a copy of your current code in another file for safe-keeping, and just outright remove the unused lines. Make sure your indenting is correct, too, so that it is easier to see the structure of the code.

    Have you thought about what each part of the code should do, in detail? Not just in the overall "it should play craps" level, but at the individual action level. Your plan should be detailed enough that if you gave it to somebody who has never played craps, they should be able to do what the program should do.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    It works better now. I was trying to wrap the entire game into a function that I could call from main. I decided that this wasn't necessary, though I would love to know how to do it.

    Code:
    /*A player rolls the dice. 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 that 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.
    */
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>//to utilize time
    
    
    //enumeration constants represent game status
    enum Status{ CONTINUE, WON, LOST};
    
    
    //function prototypes
    int rollDice(void);//rolls the dice
    int winQuotes(void);//prints winner messages when you win
    int loseQuotes(void);//prints loser messages when you lose
    
    
    int rollDice(void)
    {
    	int die1; //first die
    	int die2; //second die
    	int workSum; //sum of dice
    
    
    	die1 = 1 + (rand() % 6); //set random die1 value
    	die2 = 1 + (rand() % 6); //set the random die2 value
    	workSum = die1 + die2;//dum die1 and die2
    
    
    	//display results of this roll
    	printf("Player rolled %d + %d = %d\n", die1, die2, workSum);
    	return workSum;
    }//end rollDice
    
    
    int winQuotes(void)
    {
    	int rn = rand() % 6;
    	switch(rn)
    	{
    		case 0:
    			printf("Lucky for you!!\n");
    			break;
    		case 1:
                            printf("Winner!!\n");
                            break;
    		case 2:
                            printf("Hey there! You're cleaning me out over here!!\n");
                            break;
    		case 3:
                            printf("Big WINNER!\n");
                            break;
    		case 4:
                            printf("Ouch!! Looks like I'm going brtoke!\n");
                            break;
    		case 5:
                            printf("You Win! PERFECT!\n");
                            break;
    	}//end winQuotes
           return 0;
    }//end winQuotes
    
    
    int loseQuotes(void)
    {
            int rn = rand() % 6;
            switch(rn)
            { 
                    case 0:
                            printf("Not so lucky, are we?\n");
                            break;
                    case 1:
                            printf("YOU LOSE!!\n");
                            break;
                    case 2:
                            printf("Hey there! Glad to be cleaning you out over here!!\n");
                            break;
                    case 3:
                            printf("LOSER!\n");
                            break;
                    case 4:
                            printf("HA!! Looks like you're going broke!\n");
                            break;
                    case 5:
                            printf("You lose! NOT PERFECT!\n");
                            break;
            }//end winQuotes
    return 0;
    }//end winQuotes
    
    
    int main(void)
    {
    	int sum; //sum of rolled dice
    	int myPoint; //point earned
    	double bankBalance;// bank balance
    	double wager; //wager
    
    
    	enum Status gameStatus; //will contain CONTINUE, WON, or LOST
    
    
    	printf("Bank Balance = ");//prompt
    	scanf("%lf", &bankBalance);
    	printf("Balance = $%lf\n", bankBalance);
    
    
    	srand(time(NULL));//seed the RNG
    
    
    	while(bankBalance != 0.00000)
    	{
    	
    	  printf("wager = "); //prompt
    		scanf("%lf", &wager);
    
    
    		while(wager > bankBalance)
    		{
    			printf("Can't bet more than what you have.\n  Please enter a valid wager:  ");
    			scanf("%lf", &wager);
    		}//end while
    
    
    		sum = rollDice(); //roll 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;
    	
    			//if you don't win or lose on first roll continue until you do
    			default:
    				gameStatus = CONTINUE;		
    				myPoint = sum;
    				printf("Point is %d\n", myPoint);
    				break;
    		}//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;//Player wins
    			}
    			else
    			{
    				if(sum == 7)//if player rolls a 7
    				{
    					gameStatus = LOST; //player loses
    				}//end if
    			}//end else
    		}//end while
    	
    		//display won or lost message
    		if(gameStatus == WON)
    		{
    			printf("Player wins\n");
    			winQuotes();
    			bankBalance += wager;
    			printf("New Balance = %lf\n", bankBalance);
    		}//end if
    		else
    		{
    			printf("Player Loses\n");
    			loseQuotes();
    			bankBalance -= wager;
                            printf("New Balance = %lf\n", bankBalance);
    		}//end if
    	}//end while
    }//end main
    Code:
    amenomurakumo@kubuntu:~/C/CTraining$ ./craps
    Bank Balance = 10
    Balance = $10.000000
    wager = 2
    Player rolled 2 + 6 = 8
    Point is 8
    Player rolled 3 + 6 = 9
    Player rolled 4 + 1 = 5
    Player rolled 3 + 4 = 7
    Player Loses
    Not so lucky, are we?
    New Balance = 8.000000
    wager = 3
    Player rolled 3 + 2 = 5
    Point is 5
    Player rolled 2 + 1 = 3
    Player rolled 6 + 1 = 7
    Player Loses
    LOSER!
    New Balance = 5.000000
    wager = 4
    Player rolled 5 + 4 = 9
    Point is 9
    Player rolled 1 + 4 = 5
    Player rolled 5 + 5 = 10
    Player rolled 6 + 3 = 9
    Player wins
    Hey there! You're cleaning me out over here!!
    New Balance = 9.000000
    wager = 5
    Player rolled 6 + 5 = 11
    Player wins
    Ouch!! Looks like I'm going brtoke!
    New Balance = 14.000000
    wager = 6
    Player rolled 5 + 4 = 9
    Point is 9
    Player rolled 1 + 4 = 5
    Player rolled 4 + 3 = 7
    Player Loses
    Not so lucky, are we?
    New Balance = 8.000000
    wager = 7
    Player rolled 5 + 1 = 6
    Point is 6
    Player rolled 1 + 2 = 3
    Player rolled 1 + 3 = 4
    Player rolled 2 + 6 = 8
    Player rolled 2 + 1 = 3
    Player rolled 4 + 5 = 9
    Player rolled 3 + 2 = 5
    Player rolled 5 + 6 = 11
    Player rolled 6 + 2 = 8
    Player rolled 3 + 1 = 4
    Player rolled 2 + 3 = 5
    Player rolled 5 + 6 = 11
    Player rolled 5 + 2 = 7
    Player Loses
    LOSER!
    New Balance = 1.000000
    wager = 1
    Player rolled 2 + 5 = 7
    Player wins
    You Win! PERFECT!
    New Balance = 2.000000
    wager = 2
    Player rolled 6 + 1 = 7
    Player wins
    Big WINNER!
    New Balance = 4.000000
    wager = 3
    Player rolled 6 + 3 = 9
    Point is 9
    Player rolled 3 + 1 = 4
    Player rolled 2 + 2 = 4
    Player rolled 2 + 2 = 4
    Player rolled 4 + 4 = 8
    Player rolled 2 + 5 = 7
    Player Loses
    YOU LOSE!!
    New Balance = 1.000000
    wager = 1
    Player rolled 5 + 4 = 9
    Point is 9
    Player rolled 1 + 1 = 2
    Player rolled 2 + 2 = 4
    Player rolled 3 + 6 = 9
    Player wins
    Lucky for you!!
    New Balance = 2.000000
    wager = 2
    Player rolled 1 + 6 = 7
    Player wins
    Big WINNER!
    New Balance = 4.000000
    wager = 4
    Player rolled 6 + 2 = 8
    Point is 8
    Player rolled 1 + 3 = 4
    Player rolled 3 + 5 = 8
    Player wins
    Hey there! You're cleaning me out over here!!
    New Balance = 8.000000
    wager = 8
    Player rolled 3 + 5 = 8
    Point is 8
    Player rolled 1 + 4 = 5
    Player rolled 4 + 2 = 6
    Player rolled 3 + 1 = 4
    Player rolled 5 + 4 = 9
    Player rolled 5 + 4 = 9
    Player rolled 2 + 1 = 3
    Player rolled 5 + 2 = 7
    Player Loses
    YOU LOSE!!
    New Balance = 0.000000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Craps - stuck
    By critical in forum C++ Programming
    Replies: 6
    Last Post: 12-13-2011, 08:47 PM
  2. Craps Game
    By yacek in forum C Programming
    Replies: 2
    Last Post: 10-08-2010, 06:46 PM
  3. Need help with Craps program
    By green11420 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 10:50 PM
  4. Craps game: please help
    By C++Amanda in forum C++ Programming
    Replies: 10
    Last Post: 10-01-2007, 12:03 PM
  5. craps anyone?
    By blight2c in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 05:35 PM