DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   "Craps", Game Help (http://www.daniweb.com/forums/thread11427.html)

Mr.PinkBunny Sep 26th, 2004 3:02 pm
"Craps", Game Help
 
I've been studying VC++ for about 2 months, and I was working on my first game from a book (Game Programming All In One)
The game is 'Craps', it's a dice game. Anyways, I've had a few prolems with the codeing, can someone help me? (Windows 32 Console)
Here is the game....


Code:
#include <iostream>                                        // Includeing Headers
#include <stdlib.h>                                       
#include <time.h>

using namespace std;                                      // Language
                                                          // Global Variables
void ShowIntroScreen (void), ShowInformation (unsigned long Money);
short GetBet (void), DoDiceThrow (void);                               
unsigned short DoMoneyCalc (short Dice, short Bet, short BetMoney);
unsigned long GetAmount (void);

main (void)
    {
        unsigned long MoneyEarned, Money;                   
            short DiceValue, Bet, BetMoney;


            ShowIntroScreen ();
            Money = 1000;

            do                                                  // Loop Actions
                {
                    ShowInformation (Money);                 

                      Bet          = GetBet ();
                    BetMoney    = GetAmount ();
                    DiceValue    = DoDiceThrow ();
                    MoneyEarned  = DoMoneyCalc (DiceValue, Bet, BetMoney);

                    Money -= BetMoney;

                        if (MoneyEarned == 0 )
                            {
                                cout << "You have lost.  The number was: " << DiceValue << endl << endl;
                    }
                        else
                            {
                                cout << "You won " << MoneyEarned - BetMoney;
                                cout << " dollars. Number was: " << DiceValue;
                                cout << endl << endl;

                                Money += MoneyEarned;
                          }
          }
        while (Money > 100);
            cout << "Game Over. Keep $" << Money << " for the ride home\n";

            return 0;
}
                       
void ShowIntroScreen (void)                                // Rules and Start Up
    {
                        cout << "    Welcome to Craps 1.0" << endl << endl;
                    cout << " Here are the rules:" << endl << endl;
            cout << "You have 1000 dollars to start gambling. ";
                        cout << endl << endl;
               
                        cout << "There are three types of bets. You can bet on: " << endl << endl;
                        cout << "  - 2 and 12 which will give you the ratio of 5 to 1 if you win.\n";
                        cout << "  - 4 and 10 which will give you the ratio of 2.5 to 1 if you win.\n";
                        cout << "  - 6 and 8 which will give you the ration of 1.5 and 1 if you win.";
                            cout << endl << endl;

                        cout << "The minimum amount of money you can bet is 10 dollars and \n" " the highest amount of money you can bet is 100.";
                                cout << endl << endl;
            cout << "Have fun playing!";
                                cout << endl << endl;

    }

void ShowInformation (unsigned long Money)                // Money Display
    {
                                cout << "You have : " << Money << " dollars.";
                                    cout << endl << endl;
    }

short GetBet (void)                                        // Gets Bet Type
    {
        unsigned short BetType;   


        cout << "Enter a type of bet ( 1 = '6/8',  2 = '4/10',  3 = '2/12': ";
        cin >> BetType;

        if ((BetType == 1) || (BetType == 2) || (BetType == 3))
        {
                    return BetType;
            }
        else
            {
                    return 1;
        }

short DoDiceThrow (void)                                  // Randomized Numbers
    {
            short DiceValue;
       

            srand (time (NULL));
            DiceValue = (rand () % 11) + 2;

        if ((DiceValue == 4 ) || (DiceValue == 10))
            {
                    srand (time (NULL));
                    DiceValue == (rand () % 12) + 1;
            }

        if ((DiceValue == 2) || (DiceValue == 12))
            {
                    srand (time (NULL));
                    DiceValue = (rand () % 12) + 1;
           
                  if ((DiceValue == 2) || (DiceValue == 12))
                {
                        srand (time (NULL));
                        DiceValue = (rand () % 12) + 1;
                }
        }
return DiceValue;
          }
                                                          // Calculating Data
unsigned short DoMoneyCalc (short Dice, short Bet, short BetMoney)
{
          unsigned long MoneyEarned = 0;


          switch (Bet)
              case 1:
                      if ((Dice == 6) || (Dice == 8))
                              {
                                          MoneyEarned = BetMoney * 1.5;
                                  }
                                  break; break;
                  case 2:
                          if ((Dice == 10) || (Dice == 4))
                              {
                                          MoneyEarned = BetMoney * 2.5;
                                  }
                                  break; break;
                  case 3:
                          if ((Dice == 3) || (Dice == 12))
                              {
                                      MoneyEarned = BetMoney * 5;
                              }
                              break;
                              default;
                                  MoneyEarned = 0;
                                  break;
      }

  return MoneyEarned;
        }

        unsigned long GetAmount (void)                       
                    {
                          unsigned short BetAmount;
                cout << "Enter amount to bet (min 10 - max 100): ";
                                  cin >> BetAmount;

                if (BetAmount < 10)
                    {
                            BetAmount = 10;
                    }

                if (BetAmount > 100)
                    {
                            BetAmount = 100;
                    }
               
                return BetAmount;
        }
I've had a few problems with this book already, it's about 2 years old or so, it uses an older version of c++ and there has been some
modifications over those few years. ANYWAYS!!! I need help, here are the errors!


c:\Projects\Craps\Craps.cpp(151): error C2065: 'MoneyEarned' : undeclared identifier
c:\Projects\Craps\Craps.cpp(95): error C2601: 'DoDiceThrow' : local function definitions are illegal
c:\Projects\Craps\Craps.cpp(123): error C2601: 'DoMoneyCalc' : local function definitions are illegal
c:\Projects\Craps\Craps.cpp(26): warning C4244: '=' : conversion from 'unsigned long' to 'short', possible loss of data

__________________

C#Coder Sep 26th, 2004 3:31 pm
Re: "Craps", Game Help
 
Well I got it to compile and run.

Here's the errors that I found:

Your function prototypes didn't match the function's return type in it's code. (Aka, return a short but prototype says it returns void.)

You didn't close all functions with braces. Forgot a few :-P

You closed the switch statement with a brace but didn't open with one.

Other than that it's all good. One thing that I would change, not bad code just style, is to not use tab in the parameter list of a function and don't use it inside strings unless needed.

Here's the output from the console and I think you'll see what I mean about tabs in strings. It makes it hard to read.
          Welcome to Craps 1.0

 Here are the rules:

You have 1000 dollars to start  gambling.

There are three types of bets.  You    can    bet    on:

  - 2 and 12 which      will give you the ratio of 5 to 1 if you win.
  - 4 and 10 which      will give you the ratio of 2.5 to 1    if you win.
  - 6 and 8 which will  give you the ration    of 1.5 and 1 if you    win.

The minimum amount      of money you can bet is 10 dollars and
 the highest amount    of money you can bet is 100.

Have fun playing!

You have :      1000    dollars.

Enter a type of bet ( 1 = '6/8',  2 = '4/10',  3 =    '2/12':

Other than that, nice job overall!

Here's the revised code:
#include <iostream>                                                                                   // Includeing Headers
#include <stdlib.h>                                                                                 
#include <time.h>

using namespace        std;                                                                          // Language
                                                                                                                  // Global Variables
void ShowIntroScreen(void);
void ShowInformation(unsigned long Money);       
short GetBet(void);
short DoDiceThrow(void);                                                               
unsigned short DoMoneyCalc(short Dice, short Bet, short BetMoney);
unsigned long GetAmount(void);

void main(void)
{
        unsigned long MoneyEarned, Money;                                         
        short DiceValue, Bet, BetMoney;

        ShowIntroScreen();
        Money =        1000;

        do                                                                                                        //        Loop Actions
        {
                ShowInformation        (Money);                                 

                Bet                        = GetBet();
                BetMoney        = GetAmount();
                DiceValue        = DoDiceThrow();
                MoneyEarned        = DoMoneyCalc(DiceValue, Bet, BetMoney);

                Money -= BetMoney;

                if(MoneyEarned        == 0 )
                {
                        cout <<        "You have lost.        The number        was: " << DiceValue        << endl        << endl;
                }
                else
                {
                        cout <<        "You won " << MoneyEarned -        BetMoney;
                        cout <<        " dollars. Number was: " <<        DiceValue;
                        cout <<        endl <<        endl;

                        Money += MoneyEarned;
                }
        }
        while (Money > 100);
       
        cout <<        "Game Over.        Keep $"        << Money <<        " for the ride home\n";
}
                       
void ShowIntroScreen (void)                                                                  // Rules        and        Start Up
{
        cout <<        "          Welcome to Craps 1.0"        << endl        << endl;
        cout <<        " Here are the rules:" << endl << endl;
        cout <<        "You have 1000 dollars to start        gambling. ";
        cout <<        endl <<        endl;
               
        cout <<        "There are three types of bets.        You        can        bet        on:        " << endl << endl;
        cout <<        "  - 2 and 12 which        will give you the ratio        of 5 to        1 if you win.\n";
        cout <<        "  - 4 and 10 which        will give you the ratio        of 2.5 to 1        if you win.\n";
        cout <<        "  - 6 and 8 which will        give you the ration        of 1.5 and 1 if        you        win.";
        cout <<        endl <<        endl;

        cout <<        "The minimum amount        of money you can bet is        10 dollars and \n" " the highest amount        of money you can bet is        100.";
        cout <<        endl <<        endl;
        cout <<        "Have fun playing!";
        cout <<        endl <<        endl;

}

void ShowInformation (unsigned long        Money)                                  // Money        Display
{
        cout <<        "You have :        " << Money << "        dollars.";
        cout <<        endl <<        endl;
}

short GetBet (void)                                                                                  // Gets Bet Type
{
        unsigned short BetType;               


        cout <<        "Enter a type of bet ( 1 = '6/8',  2 = '4/10',        3 =        '2/12':        ";
        cin        >> BetType;

        if ((BetType ==        1) || (BetType == 2) ||        (BetType ==        3))
        {
                return BetType;
        }
        else
        {
                return 1;
        }
}

short DoDiceThrow (void)                                                                  // Randomized Numbers
{
        short DiceValue;


        srand (time        (NULL));
        DiceValue =        (rand () % 11) + 2;

        if ((DiceValue == 4        ) || (DiceValue        == 10))
        {
                srand (time        (NULL));
                DiceValue == (rand () %        12)        + 1;
        }

        if ((DiceValue == 2) ||        (DiceValue == 12))
        {
                srand (time        (NULL));
                DiceValue =        (rand () % 12) + 1;
       
                if ((DiceValue ==        2) || (DiceValue ==        12))
                {
                        srand (time        (NULL));
                        DiceValue =        (rand () % 12) + 1;
                }
        }

        return DiceValue;
}
                                                                                                                  // Calculating Data
unsigned short DoMoneyCalc (short Dice,        short Bet, short BetMoney)
{
          unsigned        long MoneyEarned = 0;


          switch (Bet)
          {
                  case        1:
                          if ((Dice ==        6) || (Dice        == 8))
                                {
                                        MoneyEarned = BetMoney *        1.5;
                                }
                                break;
                  case        2:
                          if ((Dice ==        10)        || (Dice ==        4))
                                {
                                        MoneyEarned = BetMoney *        2.5;
                                }
                                break;
                  case        3:
                          if ((Dice ==        3) || (Dice        == 12))
                                {
                                        MoneyEarned = BetMoney *        5;
                                }
                                break;
                  default:
                                MoneyEarned = 0;
                                break;
          }

        return MoneyEarned;
}

unsigned long GetAmount        (void)                                                 
{
        unsigned short BetAmount;
                cout <<        "Enter amount to bet (min 10 - max 100): ";
                                                                  cin >> BetAmount;

                if (BetAmount <        10)
                        {
                                BetAmount =        10;
                        }

                if (BetAmount >        100)
                        {
                                BetAmount =        100;
                        }
               
                return BetAmount;
}

Mr.PinkBunny Sep 26th, 2004 7:27 pm
Re: "Craps", Game Help
 
Thanks! ^^ I'm still having some issues with it, but it was just a test!

Tales Nov 19th, 2009 9:09 pm
I saw the following in your code:

- You're missing the closing parenthesis of GetBet(void)
- You're also missing the opening parenthesis on the switch(Bet) in DoMoneyCalc()
- You are using break; break; in the switch(Bet) statement, you only need 1 break;
- Also in the switch(Bet) you have default; but you need to use default: (notice the : )

You should add main() type int

Tales Nov 19th, 2009 9:31 pm
Your program working
 
#include <iostream>                                        // Includeing Headers
#include <stdlib.h>                                       
#include <time.h>

using namespace std;

void ShowIntroScreen(void), ShowInformation(double Money);
short GetBet(void), DoDiceThrow(void), GetAmount(void);
double DoMoneyCalc(short Dice, short Bet, short BetMoney);

int main(void){
        double MoneyEarned, Money;
        unsigned short BetMoney;
        short DiceValue, Bet;
       
        ShowIntroScreen();
        Money = 1000;
       
        do{
                ShowInformation(Money);
               
                Bet                        = GetBet();
                BetMoney        = GetAmount();
                DiceValue        = DoDiceThrow();
                MoneyEarned        = DoMoneyCalc(DiceValue, Bet, BetMoney);
               
                Money -= BetMoney;
               
                if(MoneyEarned == 0){
                        cout << "You have lost.  The number was: " << DiceValue << endl << endl;
                }
                else{
                        cout << "You won " << MoneyEarned - BetMoney;
                        cout << " dollars. Number was: " << DiceValue;
                        cout << endl << endl;
                       
                        Money += MoneyEarned;
                }
        } while (Money > 100);
       
        cout << "Game Over. Keep $" << Money << " for the ride home\n";
        return 0;
}
                       
void ShowIntroScreen(void){                                                // Rules and Start Up
        cout << "    Welcome to Craps 1.0" << endl << endl;
        cout << " Here are the rules:" << endl << endl;
        cout << "You have 1000 dollars to start gambling. ";
        cout << endl << endl;
       
        cout << "There are three types of bets. You can bet on: " << endl << endl;
        cout << "  - 2 and 12 which will give you the ratio of 5 to 1 if you win.\n";
        cout << "  - 4 and 10 which will give you the ratio of 2.5 to 1 if you win.\n";
        cout << "  - 6 and 8 which will give you the ration of 1.5 and 1 if you win.";
        cout << endl << endl;
       
        cout << "The minimum amount of money you can bet is 10 dollars and \n" " the highest amount of money you can bet is 100.";
        cout << endl << endl;
        cout << "Have fun playing!";
        cout << endl << endl;
}

void ShowInformation(double Money){                // Money Display
        cout << "You have : " << Money << " dollars.";
        cout << endl << endl;
}

short GetBet(void){                                                                // Gets Bet Type
        unsigned short BetType;
        cout << "Enter a type of bet ( 1 = '6/8',  2 = '4/10',  3 = '2/12': ";
        cin >> BetType;
        if((BetType == 1) || (BetType == 2) || (BetType == 3))
                return BetType;
        else
                return 1;
}

short DoDiceThrow(void){                                                // Randomized Numbers
        short DiceValue;
       
        srand((unsigned) time(NULL));
        DiceValue = (rand() % 11) + 2;
       
        if((DiceValue == 4 ) || (DiceValue == 10)){
                srand((unsigned) time (NULL));
                DiceValue = (rand () % 12) + 1;
        }

        if((DiceValue == 2) || (DiceValue == 12)){
                srand((unsigned) time (NULL));
                DiceValue = (rand () % 12) + 1;

                if((DiceValue == 2) || (DiceValue == 12)){
                        srand((unsigned) time (NULL));
                        DiceValue = (rand () % 12) + 1;
                }
        }
        return DiceValue;
}

double DoMoneyCalc(short Dice, short Bet, short BetMoney){
        double MoneyEarned = 0;
       
        switch(Bet){
                case 1:
                        if((Dice == 6) || (Dice == 8)){ MoneyEarned = static_cast<double>(BetMoney) * 1.5; }
                        break;
                case 2:
                        if((Dice == 10) || (Dice == 4)){ MoneyEarned = static_cast<double>(BetMoney) * 2.5; }
                        break;
                case 3:
                        if((Dice == 3) || (Dice == 12)){ MoneyEarned = static_cast<double>(BetMoney) * 5; }
                        break;
                default:
                        MoneyEarned = 0;
                        break;
        }
        return MoneyEarned;
}

short GetAmount(void){
        unsigned short BetAmount;
       
        cout << "Enter amount to bet (min 10 - max 100): ";
        cin >> BetAmount;
        if(BetAmount < 10){ BetAmount = 10; }
        if(BetAmount > 100){ BetAmount = 100; }
        return BetAmount;
}


All times are GMT -4. The time now is 1:54 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC