google-site-verification: googlebaca44933768a824.html Craps simplified C++ - Old Royal Hack Forum

Announcement

Collapse
No announcement yet.

Craps simplified C++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Craps simplified C++

    Hi, im doing some exercices from my school.

    I should use functions to make it work.

    Here is simplified craps rules:
    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, player lose.
    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 if the player roll a 7 the player lose.

    I'm having trouble with the 3th rule ( 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 the player lose. )


    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int perdu(int &jet10, int &m1s3) // function called when player lose
    {
        return jet10 -= m1s3;
    }
    
    int gagne(int &jet10, int &m1s3) // function called when player win
    {
        return jet10 += m1s3*2;
    }
    
    int lance() // function roll.
    {
        int de1,de2,somme;
        de1 = rand()%6+1;
        de2 = rand()%6+1;
        somme = de1 + de2;
    
        return somme;
    }
    
    void jouerCraps() // function playcraps
    {
        retry:
        char retry; // asked for retry at the end of program
        int jetons = 10; // cash
        int parie=1; // bet
        int tmp;
        int tmp1;
        cout<<"-------------------------------------------------------"<<endl;
    	cout<<"                    XxCRAPSxX                          "<<endl;
    	cout<<"-------------------------------------------------------"<<endl;
        debut: 
        while(parie !=0 && parie < jetons)
        {
    
            head:
            cout << "choose amount to bet "<< 1 << " between " << jetons << " (0 for quit) : ";
            cin >> parie;
            if(parie == 0)
            {
                break;
            }
            if(parie > jetons)
            {
                cout << "Your bet can't be higher than your cash"<<endl;
                goto head;
            }
            tmp = lance();
            if((tmp == 7) || (tmp ==11)) // checking 1st rule
            {
                cout << "Roll :" << tmp <<endl;
                cout << "You win"<<endl;
                gagne(jetons,parie);
                goto debut;
    
            }
            else if((tmp == 2) || (tmp == 3) || (tmp == 12)) // checking 2nd rule
                {
                    cout << "Roll :" << tmp <<endl;
                    cout << "You lose"<<endl;
                    perdu(jetons,parie);
                    goto debut;
    
                }
            else
                cout << "Roll :"<< tmp <<endl; //checking 3th rule
                while(tmp1!= tmp) 
                {
                    cout << "Roll :" << lance() <<endl;
                    tmp1 = lance();
                    if(tmp1 == 7)
                    {
                        cout << "Roll :" << tmp1 <<endl;
                        cout << "You lose"<<endl;
                        perdu(jetons,parie);
                        goto debut;
                    }
    
                }
                cout << "Roll :" << tmp1 <<endl;
                cout << "You win"<<endl;
                gagne(jetons,parie);
                goto debut;
    
        }
        tour:
        cout << "Retry again? : Y/N" <<endl;
        cin >> retry;
        if(retry == 'Y' or 'y')
        {
            goto retry;
        }
        else if(retry == 'N' or 'n')
        {
            cout <<"Ciao!!"<<endl;
        }
        else
        {
            cout<<"Write only Y for yes or N for no"<<endl;
            goto tour;
        }
    
    }
    
    
    
    int main()
    {
        srand(time(NULL));
        jouerCraps();
    }
    Old Clans:
    [Gp] => GangstaParadise(2007)
    [G-$]=> Gangster-Strike
    [F-h]=> Furyon-Hackers
    [Gp] => GangstaParadise(RIP)(2009)
    Cheats:
    Catalyst Gold Edition (2007) ===> RoyalHack ∞


    #2
    Re: Craps simplified C++

    Ok what I first noticed.

    Code:
    if(retry == 'Y' || 'y')
        {
            goto retry;
        }
        else if(retry == 'N' || 'n')
        {
            cout <<"Ciao!!"<<endl;
        }
    This is wrong, the proper syntax is
    Code:
    if( statement == condition || statement == condition2 || statement == condition3 )
    etc.
    Also using all those "goto"'s makes everything look messy as fuck and just complicates things.

    This here :
    Code:
    cout << "Roll :" << [COLOR="#FF0000"]lance() [/COLOR]<<endl;
                    tmp1 = lance();
    Is actually calling the "lance" twice, I dont think that's what you meant since it's result is not actually applied anywhere at first call, only on the second
    Code:
    tmp1 = lance();
    You're also not returning anything in your main.

    Other than these I'm not sure at the moment, what is your exact problem with the third rule?
    Last edited by mencore; 11-16-2012, 10:48 AM.
    lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
    I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

    Comment


      #3
      Re: Craps simplified C++

      I was bored so I made a dicegame myself, maybe this helps you somehow:

      Code:
      #include "StdAfx.h"
      
      /*
      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, player lose.
      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 if the player roll a 7 the player lose.
      */
      
      bool makeBet( int &bankAcc, int &betAmount )
      {
      	std::cin >> betAmount;	// input, if this ain't number it's gonna fuck up
      
      	if( betAmount > bankAcc )	// brokeass nigga
      	{
      		std::cout << "You dont have that much money, lower your bet" << std::endl;
      		return false;			// no bet set
      	}
      	else if( betAmount <= bankAcc )	// if we bet less or as much as we have
      	{
      		bankAcc -= betAmount;		// give ca$h to dealer
      		return true;				// bet has been set
      	}
      	
      	return false;
      }
      
      int rollDices( void )
      {
      	int diceOne, diceTwo = 0;
      
      	diceOne = rand() % 6+1;
      	diceTwo = rand() % 6+1;
      
      	std::cout << "You rolled: " << (diceOne + diceTwo) << std::endl;
      
      	return (diceOne + diceTwo);
      }
      
      void theGame( void )
      {
      	static int currThrow		= 0;		// holds the value of dices
      	static int desiredThrow		= 0;		// if you cant win on first throw, you need to roll this
      	static bool firstThrow		= false;	// is this the first throw?
      	static int playerCash		= 10;		// starting money, 10 dollars $wag
      	static int playerBet		= 0;		// holds the amount of money we bet		
      	
      	while( playerCash > 0 ) // do we have any money?
      	{
      		while( playerBet == 0 ) // we havent bet yet?
      		{
      			std::cout << "Bank account: " << playerCash << "$" << std::endl;
      			
      			do 
      			{
      				firstThrow = true;	// this is the first throw
      				std::cout << "Please make a bet" << std::endl;
      			} while ( makeBet( playerCash, playerBet ) == false ); // call makeBet, check it's returnvalue until it's true and we have set a bet
      		}
      
      		if( firstThrow ) // this is the first throw
      		{
      			currThrow = rollDices(); // give currThrow a value by calling rollDices
      
      			if( currThrow == 7 || currThrow == 11 ) // if first throw is 7 or 11, it means we won!
      			{
      				std::cout << "You won: " << (playerBet * 2 ) << "$" << std::endl;
      				playerCash += (playerBet * 2); // add bet * 2 into our bank
      
      				playerBet = 0; // set playerBet to 0 so we go back to loop where we must bet
      			}
      			else if( currThrow == 2 || currThrow == 3 || currThrow == 12 ) // 2,3 or 12 and we lose
      			{
      				std::cout << "You lost" << std::endl;
      				playerBet = 0; // back to betting
      			}
      			else // 1,3,4,5,6,8,9,10?
      			{
      				desiredThrow = currThrow;	// we need to roll our previous roll again to win
      				std::cout << "Try to roll " << desiredThrow << std::endl;
      				firstThrow = false;			// set firstThrow to false, this is now second roll already
      			}
      			
      		}
      		else	// second throw
      		{
      			currThrow = rollDices();	// throw again
      
      			if( currThrow == 7 ) // 7 loses
      			{
      				std::cout << "Bad luck, rolled 7!" << std::endl;
      				playerBet = 0; // back to betting
      			}
      			else if( currThrow == desiredThrow ) // same throw as before, #winning
      			{
      				std::cout << "Congratulations, you won!" << std::endl;
      				std::cout << "You won: " << (playerBet * 2 ) << "$" << std::endl;
      				playerCash += (playerBet * 2); // add bet * 2 into our bank
      				playerBet = 0;	// back to betting
      			}
      			else
      			{
      				std::cout << "Bad roll, rolling again..." << std::endl;
      				playerCash -= playerBet; // rolling costs of course, this makes the game harder, however if you dont want it, just comment out this line
      			}
      		}
      	}
      	// we ran out of money, exit program
      }
      
      int main( int argc, const char* argv[] )
      {
      	srand( time(NULL) ); // random seed
      	
      	theGame();			// main game function
      	
      	return EXIT_SUCCESS;
      }
      I tried to comment it as good as possible and name variables + functions so it's easy to understand what's happening.
      Last edited by mencore; 11-16-2012, 01:23 PM.
      lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
      I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

      Comment


        #4
        Re: Craps simplified C++

        Originally posted by mencore View Post
        I was bored so I made a dicegame myself, maybe this helps you somehow:

        Code:
        #include "StdAfx.h"
        
        /*
        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, player lose.
        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 if the player roll a 7 the player lose.
        */
        
        bool makeBet( int &bankAcc, int &betAmount )
        {
        	std::cin >> betAmount;	// input, if this ain't number it's gonna fuck up
        
        	if( betAmount > bankAcc )	// brokeass nigga
        	{
        		std::cout << "You dont have that much money, lower your bet" << std::endl;
        		return false;			// no bet set
        	}
        	else if( betAmount <= bankAcc )	// if we bet less or as much as we have
        	{
        		bankAcc -= betAmount;		// give ca$h to dealer
        		return true;				// bet has been set
        	}
        	
        	return false;
        }
        
        int rollDices( void )
        {
        	int diceOne, diceTwo = 0;
        
        	diceOne = rand() % 6+1;
        	diceTwo = rand() % 6+1;
        
        	std::cout << "You rolled: " << (diceOne + diceTwo) << std::endl;
        
        	return (diceOne + diceTwo);
        }
        
        void theGame( void )
        {
        	static int currThrow		= 0;		// holds the value of dices
        	static int desiredThrow		= 0;		// if you cant win on first throw, you need to roll this
        	static bool firstThrow		= false;	// is this the first throw?
        	static int playerCash		= 10;		// starting money, 10 dollars $wag
        	static int playerBet		= 0;		// holds the amount of money we bet		
        	
        	while( playerCash > 0 ) // do we have any money?
        	{
        		while( playerBet == 0 ) // we havent bet yet?
        		{
        			std::cout << "Bank account: " << playerCash << "$" << std::endl;
        			
        			do 
        			{
        				firstThrow = true;	// this is the first throw
        				std::cout << "Please make a bet" << std::endl;
        			} while ( makeBet( playerCash, playerBet ) == false ); // call makeBet, check it's returnvalue until it's true and we have set a bet
        		}
        
        		if( firstThrow ) // this is the first throw
        		{
        			currThrow = rollDices(); // give currThrow a value by calling rollDices
        
        			if( currThrow == 7 || currThrow == 11 ) // if first throw is 7 or 11, it means we won!
        			{
        				std::cout << "You won: " << (playerBet * 2 ) << "$" << std::endl;
        				playerCash += (playerBet * 2); // add bet * 2 into our bank
        
        				playerBet = 0; // set playerBet to 0 so we go back to loop where we must bet
        			}
        			else if( currThrow == 2 || currThrow == 3 || currThrow == 12 ) // 2,3 or 12 and we lose
        			{
        				std::cout << "You lost" << std::endl;
        				playerBet = 0; // back to betting
        			}
        			else // 1,3,4,5,6,8,9,10?
        			{
        				desiredThrow = currThrow;	// we need to roll our previous roll again to win
        				std::cout << "Try to roll " << desiredThrow << std::endl;
        				firstThrow = false;			// set firstThrow to false, this is now second roll already
        			}
        			
        		}
        		else	// second throw
        		{
        			currThrow = rollDices();	// throw again
        
        			if( currThrow == 7 ) // 7 loses
        			{
        				std::cout << "Bad luck, rolled 7!" << std::endl;
        				playerBet = 0; // back to betting
        			}
        			else if( currThrow == desiredThrow ) // same throw as before, #winning
        			{
        				std::cout << "Congratulations, you won!" << std::endl;
        				std::cout << "You won: " << (playerBet * 2 ) << "$" << std::endl;
        				playerCash += (playerBet * 2); // add bet * 2 into our bank
        				playerBet = 0;	// back to betting
        			}
        			else
        			{
        				std::cout << "Bad roll, rolling again..." << std::endl;
        				playerCash -= playerBet; // rolling costs of course, this makes the game harder, however if you dont want it, just comment out this line
        			}
        		}
        	}
        	// we ran out of money, exit program
        }
        
        int main( int argc, const char* argv[] )
        {
        	srand( time(NULL) ); // random seed
        	
        	theGame();			// main game function
        	
        	return EXIT_SUCCESS;
        }
        I tried to comment it as good as possible and name variables + functions so it's easy to understand what's happening.

        good guy m3nc0re
        Pressing thanks helps alot!

        Comment


          #5
          Re: Craps simplified C++

          Originally posted by mencore View Post
          I was bored so I made a dicegame myself, maybe this helps you somehow:

          Code:
          #include "StdAfx.h"
          
          /*
          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, player lose.
          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 if the player roll a 7 the player lose.
          */
          
          bool makeBet( int &bankAcc, int &betAmount )
          {
          	std::cin >> betAmount;	// input, if this ain't number it's gonna fuck up
          
          	if( betAmount > bankAcc )	// brokeass nigga
          	{
          		std::cout << "You dont have that much money, lower your bet" << std::endl;
          		return false;			// no bet set
          	}
          	else if( betAmount <= bankAcc )	// if we bet less or as much as we have
          	{
          		bankAcc -= betAmount;		// give ca$h to dealer
          		return true;				// bet has been set
          	}
          	
          	return false;
          }
          
          int rollDices( void )
          {
          	int diceOne, diceTwo = 0;
          
          	diceOne = rand() % 6+1;
          	diceTwo = rand() % 6+1;
          
          	std::cout << "You rolled: " << (diceOne + diceTwo) << std::endl;
          
          	return (diceOne + diceTwo);
          }
          
          void theGame( void )
          {
          	static int currThrow		= 0;		// holds the value of dices
          	static int desiredThrow		= 0;		// if you cant win on first throw, you need to roll this
          	static bool firstThrow		= false;	// is this the first throw?
          	static int playerCash		= 10;		// starting money, 10 dollars $wag
          	static int playerBet		= 0;		// holds the amount of money we bet		
          	
          	while( playerCash > 0 ) // do we have any money?
          	{
          		while( playerBet == 0 ) // we havent bet yet?
          		{
          			std::cout << "Bank account: " << playerCash << "$" << std::endl;
          			
          			do 
          			{
          				firstThrow = true;	// this is the first throw
          				std::cout << "Please make a bet" << std::endl;
          			} while ( makeBet( playerCash, playerBet ) == false ); // call makeBet, check it's returnvalue until it's true and we have set a bet
          		}
          
          		if( firstThrow ) // this is the first throw
          		{
          			currThrow = rollDices(); // give currThrow a value by calling rollDices
          
          			if( currThrow == 7 || currThrow == 11 ) // if first throw is 7 or 11, it means we won!
          			{
          				std::cout << "You won: " << (playerBet * 2 ) << "$" << std::endl;
          				playerCash += (playerBet * 2); // add bet * 2 into our bank
          
          				playerBet = 0; // set playerBet to 0 so we go back to loop where we must bet
          			}
          			else if( currThrow == 2 || currThrow == 3 || currThrow == 12 ) // 2,3 or 12 and we lose
          			{
          				std::cout << "You lost" << std::endl;
          				playerBet = 0; // back to betting
          			}
          			else // 1,3,4,5,6,8,9,10?
          			{
          				desiredThrow = currThrow;	// we need to roll our previous roll again to win
          				std::cout << "Try to roll " << desiredThrow << std::endl;
          				firstThrow = false;			// set firstThrow to false, this is now second roll already
          			}
          			
          		}
          		else	// second throw
          		{
          			currThrow = rollDices();	// throw again
          
          			if( currThrow == 7 ) // 7 loses
          			{
          				std::cout << "Bad luck, rolled 7!" << std::endl;
          				playerBet = 0; // back to betting
          			}
          			else if( currThrow == desiredThrow ) // same throw as before, #winning
          			{
          				std::cout << "Congratulations, you won!" << std::endl;
          				std::cout << "You won: " << (playerBet * 2 ) << "$" << std::endl;
          				playerCash += (playerBet * 2); // add bet * 2 into our bank
          				playerBet = 0;	// back to betting
          			}
          			else
          			{
          				std::cout << "Bad roll, rolling again..." << std::endl;
          				playerCash -= playerBet; // rolling costs of course, this makes the game harder, however if you dont want it, just comment out this line
          			}
          		}
          	}
          	// we ran out of money, exit program
          }
          
          int main( int argc, const char* argv[] )
          {
          	srand( time(NULL) ); // random seed
          	
          	theGame();			// main game function
          	
          	return EXIT_SUCCESS;
          }
          I tried to comment it as good as possible and name variables + functions so it's easy to understand what's happening.
          Thank you mencore

          This will help me a lot.

          I'm just beginner & this is my first experience with functions.
          Old Clans:
          [Gp] => GangstaParadise(2007)
          [G-$]=> Gangster-Strike
          [F-h]=> Furyon-Hackers
          [Gp] => GangstaParadise(RIP)(2009)
          Cheats:
          Catalyst Gold Edition (2007) ===> RoyalHack ∞

          Comment

          Working...
          X