Thread: Craps Game

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    Craps Game

    Heres my first really c++ game that I did some functions and stuff in. I'm just starting to program reading an eBook I downloaded. Please feel free to leave comments or suggestions.

    Code:
    /********************
    *
    *	Craps Game
    *	main.cpp
    *
    ********************/
    
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    using namespace std;
    
    void showIntro (void);
    void showInformation (unsigned long playerMoney);
    short playerBet (void);
    short diceRoll (void);
    unsigned short moneyCalc (short diceValue, short getBet, short betMoney);
    unsigned long getAmount (void);
    
    
    int main ()
    {
    	unsigned long playerEarned;
    	unsigned long playerMoney;
    
    	short diceValue;
    	short getBet;
    	short betMoney;
    
    	showIntro ();
    	playerMoney = 1000;
    
    	do
    	{
    		showInformation (playerMoney);
    		getBet       =   playerBet ();
    		betMoney     =   getAmount ();
    		diceValue    =   diceRoll ();
    		playerEarned =   moneyCalc (diceValue, getBet, betMoney);
    
    		playerMoney -= betMoney;
    
    		if (playerEarned == 0)
    		{
    			cout << "You lost. The number was : " << diceValue;
    			cout << endl << endl;
    		}
    		else
    		{
    			cout << "You earned : " << playerEarned - betMoney << " dollars," << endl;
    			cout << "The number was : " << diceValue;
    			cout << endl << endl;
    		
    			playerMoney  += playerEarned;
    		}
    	} while (playerMoney > 100);
    	
    	cout << "Game over. You're gonna need that $" << playerMoney << "for the taxi ride home.";
    	cout << endl << endl;
    	return 0;
    }
    
    void showIntro (void)
    {
    	cout << "Welcome to Craps v1. Written by : Ryan Nielson" << endl;
    	cout << endl;
    	cout << "Here are the rules :"<< endl << endl;
    	cout << "-You start of with $1000 to bet." << endl << endl;
    	cout << "-You bet in amounts of $10 or $100." << endl << endl;
    	cout << "-You can bet on numbers 2 and 12 which will win you a ratio of 5 : 1." << endl << endl;
    	cout << "-You can bet on numbers 4 and 10 which will win you a ratio of 2.5 : 1." << endl << endl;
    	cout << "-You can bet on numbers 6 and 8 which will win you a ratio of 1.5 : 1." << endl << endl << endl;
    	cout << "-Have fun and enjoy the game!" << endl << endl;
    }
    
    void showInformation (unsigned long playerMoney)
    {
    	cout << "You have " << playerMoney << "dollars." << endl << endl; 
    }
    
    short playerBet (void)
    {
    	unsigned short betType;
    	
    	/*Get the bet ----------------------------------------------------------------------------*/
    	
    	cout << "What numbers would you like to bet on ?" << endl;
    	cout << "(1. '2/12', 2. '4/10', 3. '6/8') : ";
    	cin >> betType;
    
    	if ((betType == 1) || (betType == 2) || (betType == 3))
    	{
    		return betType;
    	}
    	else
    	{
    		return 1;
    	}
    }
    
    short diceRoll (void)
    {
    	short diceValue;
    
    	srand (time (NULL));							// Get Dice Value
    	diceValue = (rand () % 11) + 2;
    
    	if ((diceValue == 4) || (diceValue == 10))		// Makes 4/10 harder to get.
    	{
    		srand (time (NULL));			
    		diceValue = (rand () % 12) + 1;
    	}
    	if ((diceValue == 2) || (diceValue == 12))		// Makes 2/12 harder to get.
    	{
    		srand (time (NULL));			
    		diceValue = (rand () % 12) + 1;
    		
    		if ((diceValue == 2) || (diceValue == 12))		// Makes 2/12 harder to get.
    		{
    			srand (time (NULL));			
    			diceValue = (rand () % 12) + 1;
    		}
    	}
    	return diceValue;
    }
    
    unsigned short moneyCalc (short diceValue, short getBet, short betMoney)
    {
    	unsigned long playerEarned = 0;
    
    	switch (getBet)
    	{
    	case 1:
    		if ((diceValue == 6) || (diceValue == 8))
    		{
    			playerEarned = betMoney * 1.5;
    		}
    		break;
    	case 2:
    		if ((diceValue == 4) || (diceValue == 10))
    		{
    			playerEarned = betMoney * 2.5;
    		}
    		break;
    	case 3:
    		if ((diceValue == 2) || (diceValue == 12))
    		{
    			playerEarned = betMoney * 5;
    		}
    		break;
    	default :
    		playerEarned = 0;
    		break;
    	}
    	return playerEarned;
    }
    
    unsigned long getAmount (void)
    {
    	unsigned short betAmount;
    
    	cout << "How much would you like to bet? $10 or $100 : ";
    	cin >> betAmount;
    
    	/*If the betting amounts are off----------------------------------------------------------------*/
    
    	if (betAmount < 10)
    	{
    		betAmount = 10;
    	}
    	else if (betAmount > 100)
    	{
    		betAmount = 100;
    	}
    	return betAmount;
    }
    Feel free to put my reputations up if you thinks it's good for a beginner
    I'm j/k.
    But if you want to go right ahead lol
    Learning C++
    Programmer in training

  2. #2
    Banned
    Join Date
    May 2004
    Posts
    129
    Howdy,
    definitely a great place to start. I don't have experience playing the real gambling game, but I did play this for a few minutes. It looks like a real good start, keep up the good work!

    And sure, I'll send some good reputation your way

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You have two warnings you know
    Code:
    playerEarned = betMoney * 1.5;
    should be
    Code:
    playerEarned = betMoney * (unsigned)1.5L;
    and with
    Code:
     playerEarned = betMoney * 2.5;
    should be
    Code:
     playerEarned = betMoney * (unsigned)2.5L;
    Plus
    Why does g++ say that UL is an invalid suffix?
    Unsigned constants are written with a terminal u or U, and the suffix ul or UL indicates unsigned long.
    The C Programming Language 2ed
    Last edited by linuxdude; 06-12-2004 at 07:26 PM.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by linuxdude
    playerEarned = betMoney * 2.5;[/code]should be
    Code:
     playerEarned = betMoney * (unsigned)2.5L;
    2.5 is not a long it is a double. You want to cast the result and not the individual numbers. IE
    Code:
     playerEarned = (unsigned long)(betMoney * 2.5);
    Oh any my copy of g++ has no problems with UL.

    @TWIXMIX: You need to provide a gracious way to end the game early.
    Last edited by Thantos; 06-12-2004 at 07:41 PM.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I am sorry I thought bet money was an unsigned long value.[EDIT]and why did it silence the warning.[/EDIT]
    Last edited by linuxdude; 06-12-2004 at 07:45 PM.

  6. #6
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    hmm thats weird Ill have to fix some of those warnings you guys are getting I didn't get any.
    and yah ill make the game so u can exit earlier. I'm to tired at the moment so I'll do later.

    Thx for the comments guys
    Learning C++
    Programmer in training

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. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  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