Thread: Craps game: please help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    Craps game: please help

    I am very new to C++. I have to write a program for class that reflects the game of craps and I'm kinda stuck at this point. Any help someone could give me would be HUGELY appreciated.
    The guidelines we were given are that the program should execute 10000 times to compute the probability of the "player" winning and the "house" winning.

    Basic game rules are:
    Player rolls two dice.
    When the sum is 7 or 11 on first throw, player wins.
    When the sum is 2, 3, or 12 on first throw, "house" wins.
    When the sum is 4,5,6,8,9, or 10 on first throw, that sum becomes the player's "point".
    Now, to win the player must continue rolling the dice until he makes "point"; however should he roll a 7 then the "house" wins.
    The game ends.

    My code this far is below. It seems as if my game counter is not updating. Also, how can I make this automated to run the 10000 times without myself having to push the Y or N keys? Thanks to whoever helps me!

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {	
    	srand((unsigned)time(NULL));
    
    		int firstRoll, nextRoll, die1, die2, sum, gamenum;
    		char playAgain;
    			
    	cout << "Craps game for class" << endl << endl;
    
    	do
    	{
    		cout << "The game is ready to begin" << endl;
    		cout << endl;
    
    		die1 = (rand() &#37; 6 + 1);
    		die2 = (rand() % 6 + 1);
    		firstRoll = die1 + die2;
    		gamenum = 1;
    		sum = 0;
    		
    		cout << "Dice1= " << die1 << " and Dice2= " << die2 << " for a total of " << firstRoll << "." << endl;
    
    		if (firstRoll == 7 || firstRoll == 11)
    		{
    			cout << "Player wins!" << endl;
    			gamenum++;
    		}
    
    		else if ((firstRoll == 2) || (firstRoll == 3) || (firstRoll == 12))
    		{
    			 cout << "House wins." << endl;
    			 gamenum++;
    		}
    
    		else
    		{
    			do 
    			{
    				cout << "Point value is: " << firstRoll << endl;
    				system("pause");
    				cout << endl;
    
    				die1 = (rand() % 6 + 1);
    				die2 = (rand() % 6 + 1);
    				nextRoll = die1 + die2;
    
    				cout << "Dice1= " << die1 << " and Dice2= " << die2 << " for a total of " << nextRoll << "." << endl;
    
    				if (nextRoll == firstRoll)
    				{
    					cout << "Point value rolled. Player wins" << endl;
    					gamenum = gamenum+1;
    				}
    			
    				else if (nextRoll == 7)
    				{
    					cout << "Player loses" << endl;
    					gamenum = gamenum+1;
    				}
    
    				else
    				{
    					cout << "Roll again." << endl;
    					system("pause");
    					cout << endl;
    				}
    			}
    			while ((nextRoll != 7) && (nextRoll != firstRoll));
    		}
    				
    		while (gamenum<=10000)						//Need to focus here because
    		{											//Gamenum is not incrementing
    			sum += gamenum;
    			gamenum++;
    		}
    		cout<<gamenum<<endl;
    		cin >> playAgain;
    
    		if ((playAgain == 'n') || (playAgain == 'N'))
    		{
    			cout << "See ya next time" << endl;
    		}
    
    		else if ((playAgain == 'y') || (playAgain == 'Y'))
    		{
    		}
    
    		else 
    		{
    			cout << "Please enter either Y or N!" << endl;
    		}
    					//}while;	
    	}while ((playAgain == 'y') || (playAgain == 'Y'));
    }// end of main
    Last edited by C++Amanda; 10-01-2007 at 12:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First off, add a bit of structure to the program, rather than some massive main() doing the whole thing.

    Code:
    int main ( ) {
        srand((unsigned)time(NULL));
        do {
            playCraps();
            choice = goAgain();
        } while ( choice == 'Y' );
        return 0;
    }
    Where playCraps() does everything to play just one game, and goAgain() issues the prompt and returns with the choice made.

    You can then enhance this to play automatically for a number of turns by adding something like this to main
    Code:
    for ( i = 0 ; i < 10000 ; i++ ) {
        playCraps();
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Awesome! Let me trying implementing something like that real quick and I'll let you know.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Quote Originally Posted by Salem View Post
    First off, add a bit of structure to the program, rather than some massive main() doing the whole thing.

    Code:
    int main ( ) {
        srand((unsigned)time(NULL));
        do {
            playCraps();
            choice = goAgain();
        } while ( choice == 'Y' );
        return 0;
    }
    Where playCraps() does everything to play just one game, and goAgain() issues the prompt and returns with the choice made.

    You can then enhance this to play automatically for a number of turns by adding something like this to main
    Code:
    for ( i = 0 ; i < 10000 ; i++ ) {
        playCraps();
    }
    I hate to sound like a numbskull, but where do you suggest I setup the:
    Code:
    int main ( ) {
        srand((unsigned)time(NULL));
        do {
            playCraps();
            choice = goAgain();
        } while ( choice == 'Y' );
        return 0;
    }
    It looked obvious to me so I set it up that way. However, it returned info like choice is an undeclared identifier, playCraps and playAgain identifier not found. Any advice for a noob?

    And this you say I can put anywhere inside main?
    Code:
    for ( i = 0 ; i < 10000 ; i++ ) {
        playCraps();
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to declare a variable (of type char perhaps) that is called "choice", and make goAgain() return a value of this type.

    And yes, you can put a loop of (for example) 10000 goes like Salem suggested. You'd need to declare a variable 'i' to make it work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Quote Originally Posted by matsp View Post
    You need to declare a variable (of type char perhaps) that is called "choice", and make goAgain() return a value of this type.

    And yes, you can put a loop of (for example) 10000 goes like Salem suggested. You'd need to declare a variable 'i' to make it work.

    --
    Mats
    Should I remove my gamenum and sum variables to make this work?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C++Amanda View Post
    Should I remove my gamenum and sum variables to make this work?
    They don't actually do much good in the current code, so I guess so. [Assuming current code is looking like what you posted, that is].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    When I have the code written like this:
    [code]
    When I have the code written like this:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {	
    	srand((unsigned)time(NULL));
    	
    		int firstRoll, nextRoll, die1, die2, gamenum;
    		char playAgain;
    			
    	cout << "Craps game for class" << endl << endl;
    
    	do
    	{
    		do
    			{
    			playCraps();
    			playAgain = goAgain();
    			}
    		while ( playAgain == 'Y' );
    		return 0;
    		cout << "The game is ready to begin" << endl;
    		cout << endl;
    
    		die1 = (rand() &#37; 6 + 1);
    		die2 = (rand() % 6 + 1);
    		firstRoll = die1 + die2;
    		
    		for ( gamenum = 0 ; gamenum < 10000 ; gamenum++ )
    			{
    				playCraps();
    			}
    				
    		cout << "Dice1= " << die1 << " and Dice2= " << die2 << " for a total of " << firstRoll << "." << endl;
    
    		if (firstRoll == 7 || firstRoll == 11)
    		{
    			cout << "Player wins!" << endl;
    		}
    
    		else if ((firstRoll == 2) || (firstRoll == 3) || (firstRoll == 12))
    		{
    			 cout << "House wins." << endl;
    		}
    
    		else
    		{
    			do 
    			{
    				cout << "Point value is: " << firstRoll << endl;
    				system("pause");
    				cout << endl;
    
    				die1 = (rand() % 6 + 1);
    				die2 = (rand() % 6 + 1);
    				nextRoll = die1 + die2;
    
    				cout << "Dice1= " << die1 << " and Dice2= " << die2 << " for a total of " << nextRoll << "." << endl;
    
    				if (nextRoll == firstRoll)
    				{
    					cout << "Point value rolled. Player wins" << endl;
    				}
    			
    				else if (nextRoll == 7)
    				{
    					cout << "Player loses" << endl;
    				}
    
    				else
    				{
    					cout << "Roll again." << endl;
    					system("pause");
    					cout << endl;
    				}
    			}
    			while ((nextRoll != 7) && (nextRoll != firstRoll));
    		}
    				
    		cin >> playAgain;
    		
    		if ((playAgain == 'n') || (playAgain == 'N'))
    		{
    			cout << "See ya next time" << endl;
    		}
    
    		else if ((playAgain == 'y') || (playAgain == 'Y'))
    		{
    		}
    
    		else 
    		{
    			cout << "Please enter either Y or N!" << endl;
    		}
    					//}while;	
    	}while ((playAgain == 'y') || (playAgain == 'Y'));
    }// end of main
    I get:
    playCraps identifier not found Line 27
    goAgain identifier not found Line 28
    playCraps identifier not found Line 41

    Is this starting to look better or am I crazy?
    Last edited by C++Amanda; 10-01-2007 at 10:14 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They're functions you need to write.

    Taking a wild stab (like not declaring all the locals), perhaps
    Code:
    void playCraps ( void ) {
    		die1 = (rand() &#37; 6 + 1);
    		die2 = (rand() % 6 + 1);
    		firstRoll = die1 + die2;
    		
    		cout << "Dice1= " << die1 << " and Dice2= " << die2 << " for a total of " << firstRoll << "." << endl;
    
    		if (firstRoll == 7 || firstRoll == 11)
    		{
    			cout << "Player wins!" << endl;
    			return;
    		}
    
    		else if ((firstRoll == 2) || (firstRoll == 3) || (firstRoll == 12))
    		{
    			 cout << "House wins." << endl;
    			return;
    		}
    
    		else
    		{
    			do 
    			{
    				cout << "Point value is: " << firstRoll << endl;
    				system("pause");
    				cout << endl;
    
    				die1 = (rand() % 6 + 1);
    				die2 = (rand() % 6 + 1);
    				nextRoll = die1 + die2;
    
    				cout << "Dice1= " << die1 << " and Dice2= " << die2 << " for a total of " << nextRoll << "." << endl;
    
    				if (nextRoll == firstRoll)
    				{
    					cout << "Point value rolled. Player wins" << endl;
    				}
    			
    				else if (nextRoll == 7)
    				{
    					cout << "Player loses" << endl;
    				}
    
    				else
    				{
    					cout << "Roll again." << endl;
    					system("pause");
    					cout << endl;
    				}
    			}
    			while ((nextRoll != 7) && (nextRoll != firstRoll));
    		}
    }
    Something along those lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    All the stuff below this :
    Code:
    		return 0;
    in your code needs to go into a new function called playCraps.

    You see, if you put code beneath your return statement, it's never going to get executed and putting 'playCraps()' in there won't let the compiler know what you want to associate that identifier with.

    So just like you have a main() function, you need to create a playCraps() function
    we are one

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Thanks Salem and wiiire. I appreciate the effort. I'm new to this, so I'm trying to learn. I'm much more of a visual learner (show me what to do and it'll make sense) as opposed to telling me. Either way, thank you both so much! I'll give it a try and will let you know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 11:03 AM
  2. Craps Game
    By TWIXMIX in forum Game Programming
    Replies: 5
    Last Post: 06-12-2004, 07:47 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. help with a craps game!
    By Jessie in forum C Programming
    Replies: 10
    Last Post: 10-16-2002, 09:19 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM