Thread: Craps game in C!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Craps game in C!

    Hi everyone! I have to do a program for my language C course in university but I have some problems ...
    I have to do a program on the game of craps. I am supposed to have a user roll the dice and if they roll a 7 or an 11, they win. If they roll a 2, 3, or 12, they lose. If the 1st roll is not a win or a loss, the player's first roll becomes the 'point' value. The player then rolls again and again until either a 7 is rolled (rolling player loses) or the 'point' value is rolled a second time (rolling player wins). Also the player have initially 500 $ and he has to bet before each game until he don’t have any money.
    The program works well but the professor wants to do the program with a switch instruction (with Case 7 and 11, Case 2,3,12 and default) but I did it with an if instruction and I don’t know how to change it. Can someone please help me?? Thanks

    Here is my program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int get_bet(int max);
    int roll_dice(void);
    
    int main(void)
    {	
    int cash = 500;	 // The player starts with 500$
        
    printf("Welcome to Crap Craps\n");	
    srand( time(NULL));	
    
    do 
    
    {
    int bet = get_bet(cash);		
    int point = roll_dice();	
                 
     	if( !bet) break;	
       	printf("you rolled %d\n", point);	
      	if( point == 7 || point == 11)	// The player win with 7 or 11
       	{			printf("You Win!\n");		
                 cash += bet;		}	
                 else if( point == 2 || point == 3 || point == 12) // The player loose with 2, 3, or 12
                   	{	printf("You Loose!\n");		
                      	cash -= bet;		}	
                            	else		{	
                		for(;;)		
                                                  
             	{	int roll = roll_dice();	 // If you get 7 other than the firt round you loose
                          	printf("you rolled %d\n", roll);	
                           	if( roll == 7)			
       	{					printf("Craps! you loose.\n");	
    				cash -= bet;		
                     	break;				}		
                       else if( roll == point)			// If you get your point, you win
    	{	printf("You Win!\n");					
        
                 cash += bet;					
              break;				}			}		}			}
        while(cash != 0); // while you have cash
        
      	printf("thanks for playing you walk away with $%d\n", cash);
       	system("pause");}
           int get_bet(int max)
           {	int bet;
           	printf("0 to quit, max bet is %d\nenter bet:", max);	
               do	{		scanf("%d", &bet);
               	}
            while( bet < 0 || bet > max); 
            	fflush( stdin);	return bet;
                }
                int roll_dice(void)
                {	return (rand() % 6) + (rand() % 6) + 2;}

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the part of your book that talks about switch and case. Or read this: Switch Case in C and C++ - Cprogramming.com


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    switch (point) {
    case 7:
    case 11:
        /* you win part */
        break;
    case 2:
    case 3:
    case 12:
        /* you lose part */
        break;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Umm... help me find a Game Engine?
    By Hmph in forum Game Programming
    Replies: 8
    Last Post: 03-20-2011, 10:07 AM
  2. Craps Game
    By yacek in forum C Programming
    Replies: 2
    Last Post: 10-08-2010, 06:46 PM
  3. [freefps]Tactical Assault coders needed
    By Sickmind in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-07-2010, 05:06 PM
  4. Craps Game
    By TWIXMIX in forum Game Programming
    Replies: 5
    Last Post: 06-12-2004, 07:47 PM
  5. help with a craps game!
    By Jessie in forum C Programming
    Replies: 10
    Last Post: 10-16-2002, 09:19 AM