"Craps", Game Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 8
Reputation: Mr.PinkBunny is an unknown quantity at this point 
Solved Threads: 0
Mr.PinkBunny's Avatar
Mr.PinkBunny Mr.PinkBunny is offline Offline
Newbie Poster

"Craps", Game Help

 
0
  #1
Sep 26th, 2004
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:
  1. #include <iostream> // Includeing Headers
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std; // Language
  6. // Global Variables
  7. void ShowIntroScreen (void), ShowInformation (unsigned long Money);
  8. short GetBet (void), DoDiceThrow (void);
  9. unsigned short DoMoneyCalc (short Dice, short Bet, short BetMoney);
  10. unsigned long GetAmount (void);
  11.  
  12. main (void)
  13. {
  14. unsigned long MoneyEarned, Money;
  15. short DiceValue, Bet, BetMoney;
  16.  
  17.  
  18. ShowIntroScreen ();
  19. Money = 1000;
  20.  
  21. do // Loop Actions
  22. {
  23. ShowInformation (Money);
  24.  
  25. Bet = GetBet ();
  26. BetMoney = GetAmount ();
  27. DiceValue = DoDiceThrow ();
  28. MoneyEarned = DoMoneyCalc (DiceValue, Bet, BetMoney);
  29.  
  30. Money -= BetMoney;
  31.  
  32. if (MoneyEarned == 0 )
  33. {
  34. cout << "You have lost. The number was: " << DiceValue << endl << endl;
  35. }
  36. else
  37. {
  38. cout << "You won " << MoneyEarned - BetMoney;
  39. cout << " dollars. Number was: " << DiceValue;
  40. cout << endl << endl;
  41.  
  42. Money += MoneyEarned;
  43. }
  44. }
  45. while (Money > 100);
  46. cout << "Game Over. Keep $" << Money << " for the ride home\n";
  47.  
  48. return 0;
  49. }
  50.  
  51. void ShowIntroScreen (void) // Rules and Start Up
  52. {
  53. cout << " Welcome to Craps 1.0" << endl << endl;
  54. cout << " Here are the rules:" << endl << endl;
  55. cout << "You have 1000 dollars to start gambling. ";
  56. cout << endl << endl;
  57.  
  58. cout << "There are three types of bets. You can bet on: " << endl << endl;
  59. cout << " - 2 and 12 which will give you the ratio of 5 to 1 if you win.\n";
  60. cout << " - 4 and 10 which will give you the ratio of 2.5 to 1 if you win.\n";
  61. cout << " - 6 and 8 which will give you the ration of 1.5 and 1 if you win.";
  62. cout << endl << endl;
  63.  
  64. cout << "The minimum amount of money you can bet is 10 dollars and \n" " the highest amount of money you can bet is 100.";
  65. cout << endl << endl;
  66. cout << "Have fun playing!";
  67. cout << endl << endl;
  68.  
  69. }
  70.  
  71. void ShowInformation (unsigned long Money) // Money Display
  72. {
  73. cout << "You have : " << Money << " dollars.";
  74. cout << endl << endl;
  75. }
  76.  
  77. short GetBet (void) // Gets Bet Type
  78. {
  79. unsigned short BetType;
  80.  
  81.  
  82. cout << "Enter a type of bet ( 1 = '6/8', 2 = '4/10', 3 = '2/12': ";
  83. cin >> BetType;
  84.  
  85. if ((BetType == 1) || (BetType == 2) || (BetType == 3))
  86. {
  87. return BetType;
  88. }
  89. else
  90. {
  91. return 1;
  92. }
  93.  
  94. short DoDiceThrow (void) // Randomized Numbers
  95. {
  96. short DiceValue;
  97.  
  98.  
  99. srand (time (NULL));
  100. DiceValue = (rand () % 11) + 2;
  101.  
  102. if ((DiceValue == 4 ) || (DiceValue == 10))
  103. {
  104. srand (time (NULL));
  105. DiceValue == (rand () % 12) + 1;
  106. }
  107.  
  108. if ((DiceValue == 2) || (DiceValue == 12))
  109. {
  110. srand (time (NULL));
  111. DiceValue = (rand () % 12) + 1;
  112.  
  113. if ((DiceValue == 2) || (DiceValue == 12))
  114. {
  115. srand (time (NULL));
  116. DiceValue = (rand () % 12) + 1;
  117. }
  118. }
  119. return DiceValue;
  120. }
  121. // Calculating Data
  122. unsigned short DoMoneyCalc (short Dice, short Bet, short BetMoney)
  123. {
  124. unsigned long MoneyEarned = 0;
  125.  
  126.  
  127. switch (Bet)
  128. case 1:
  129. if ((Dice == 6) || (Dice == 8))
  130. {
  131. MoneyEarned = BetMoney * 1.5;
  132. }
  133. break; break;
  134. case 2:
  135. if ((Dice == 10) || (Dice == 4))
  136. {
  137. MoneyEarned = BetMoney * 2.5;
  138. }
  139. break; break;
  140. case 3:
  141. if ((Dice == 3) || (Dice == 12))
  142. {
  143. MoneyEarned = BetMoney * 5;
  144. }
  145. break;
  146. default;
  147. MoneyEarned = 0;
  148. break;
  149. }
  150.  
  151. return MoneyEarned;
  152. }
  153.  
  154. unsigned long GetAmount (void)
  155. {
  156. unsigned short BetAmount;
  157. cout << "Enter amount to bet (min 10 - max 100): ";
  158. cin >> BetAmount;
  159.  
  160. if (BetAmount < 10)
  161. {
  162. BetAmount = 10;
  163. }
  164.  
  165. if (BetAmount > 100)
  166. {
  167. BetAmount = 100;
  168. }
  169.  
  170. return BetAmount;
  171. }
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

__________________
:evil::cry::evil::cry::evil::cry::evil::cry::evil:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 19
Reputation: C#Coder is an unknown quantity at this point 
Solved Threads: 0
C#Coder C#Coder is offline Offline
Newbie Poster

Re: "Craps", Game Help

 
0
  #2
Sep 26th, 2004
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.
  1. Welcome to Craps 1.0
  2.  
  3. Here are the rules:
  4.  
  5. You have 1000 dollars to start gambling.
  6.  
  7. There are three types of bets. You can bet on:
  8.  
  9. - 2 and 12 which will give you the ratio of 5 to 1 if you win.
  10. - 4 and 10 which will give you the ratio of 2.5 to 1 if you win.
  11. - 6 and 8 which will give you the ration of 1.5 and 1 if you win.
  12.  
  13. The minimum amount of money you can bet is 10 dollars and
  14. the highest amount of money you can bet is 100.
  15.  
  16. Have fun playing!
  17.  
  18. You have : 1000 dollars.
  19.  
  20. 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:
  1. #include <iostream> // Includeing Headers
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std; // Language
  6. // Global Variables
  7. void ShowIntroScreen(void);
  8. void ShowInformation(unsigned long Money);
  9. short GetBet(void);
  10. short DoDiceThrow(void);
  11. unsigned short DoMoneyCalc(short Dice, short Bet, short BetMoney);
  12. unsigned long GetAmount(void);
  13.  
  14. void main(void)
  15. {
  16. unsigned long MoneyEarned, Money;
  17. short DiceValue, Bet, BetMoney;
  18.  
  19. ShowIntroScreen();
  20. Money = 1000;
  21.  
  22. do // Loop Actions
  23. {
  24. ShowInformation (Money);
  25.  
  26. Bet = GetBet();
  27. BetMoney = GetAmount();
  28. DiceValue = DoDiceThrow();
  29. MoneyEarned = DoMoneyCalc(DiceValue, Bet, BetMoney);
  30.  
  31. Money -= BetMoney;
  32.  
  33. if(MoneyEarned == 0 )
  34. {
  35. cout << "You have lost. The number was: " << DiceValue << endl << endl;
  36. }
  37. else
  38. {
  39. cout << "You won " << MoneyEarned - BetMoney;
  40. cout << " dollars. Number was: " << DiceValue;
  41. cout << endl << endl;
  42.  
  43. Money += MoneyEarned;
  44. }
  45. }
  46. while (Money > 100);
  47.  
  48. cout << "Game Over. Keep $" << Money << " for the ride home\n";
  49. }
  50.  
  51. void ShowIntroScreen (void) // Rules and Start Up
  52. {
  53. cout << " Welcome to Craps 1.0" << endl << endl;
  54. cout << " Here are the rules:" << endl << endl;
  55. cout << "You have 1000 dollars to start gambling. ";
  56. cout << endl << endl;
  57.  
  58. cout << "There are three types of bets. You can bet on: " << endl << endl;
  59. cout << " - 2 and 12 which will give you the ratio of 5 to 1 if you win.\n";
  60. cout << " - 4 and 10 which will give you the ratio of 2.5 to 1 if you win.\n";
  61. cout << " - 6 and 8 which will give you the ration of 1.5 and 1 if you win.";
  62. cout << endl << endl;
  63.  
  64. cout << "The minimum amount of money you can bet is 10 dollars and \n" " the highest amount of money you can bet is 100.";
  65. cout << endl << endl;
  66. cout << "Have fun playing!";
  67. cout << endl << endl;
  68.  
  69. }
  70.  
  71. void ShowInformation (unsigned long Money) // Money Display
  72. {
  73. cout << "You have : " << Money << " dollars.";
  74. cout << endl << endl;
  75. }
  76.  
  77. short GetBet (void) // Gets Bet Type
  78. {
  79. unsigned short BetType;
  80.  
  81.  
  82. cout << "Enter a type of bet ( 1 = '6/8', 2 = '4/10', 3 = '2/12': ";
  83. cin >> BetType;
  84.  
  85. if ((BetType == 1) || (BetType == 2) || (BetType == 3))
  86. {
  87. return BetType;
  88. }
  89. else
  90. {
  91. return 1;
  92. }
  93. }
  94.  
  95. short DoDiceThrow (void) // Randomized Numbers
  96. {
  97. short DiceValue;
  98.  
  99.  
  100. srand (time (NULL));
  101. DiceValue = (rand () % 11) + 2;
  102.  
  103. if ((DiceValue == 4 ) || (DiceValue == 10))
  104. {
  105. srand (time (NULL));
  106. DiceValue == (rand () % 12) + 1;
  107. }
  108.  
  109. if ((DiceValue == 2) || (DiceValue == 12))
  110. {
  111. srand (time (NULL));
  112. DiceValue = (rand () % 12) + 1;
  113.  
  114. if ((DiceValue == 2) || (DiceValue == 12))
  115. {
  116. srand (time (NULL));
  117. DiceValue = (rand () % 12) + 1;
  118. }
  119. }
  120.  
  121. return DiceValue;
  122. }
  123. // Calculating Data
  124. unsigned short DoMoneyCalc (short Dice, short Bet, short BetMoney)
  125. {
  126. unsigned long MoneyEarned = 0;
  127.  
  128.  
  129. switch (Bet)
  130. {
  131. case 1:
  132. if ((Dice == 6) || (Dice == 8))
  133. {
  134. MoneyEarned = BetMoney * 1.5;
  135. }
  136. break;
  137. case 2:
  138. if ((Dice == 10) || (Dice == 4))
  139. {
  140. MoneyEarned = BetMoney * 2.5;
  141. }
  142. break;
  143. case 3:
  144. if ((Dice == 3) || (Dice == 12))
  145. {
  146. MoneyEarned = BetMoney * 5;
  147. }
  148. break;
  149. default:
  150. MoneyEarned = 0;
  151. break;
  152. }
  153.  
  154. return MoneyEarned;
  155. }
  156.  
  157. unsigned long GetAmount (void)
  158. {
  159. unsigned short BetAmount;
  160. cout << "Enter amount to bet (min 10 - max 100): ";
  161. cin >> BetAmount;
  162.  
  163. if (BetAmount < 10)
  164. {
  165. BetAmount = 10;
  166. }
  167.  
  168. if (BetAmount > 100)
  169. {
  170. BetAmount = 100;
  171. }
  172.  
  173. return BetAmount;
  174. }
If you don't know it, learn it ;)
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 8
Reputation: Mr.PinkBunny is an unknown quantity at this point 
Solved Threads: 0
Mr.PinkBunny's Avatar
Mr.PinkBunny Mr.PinkBunny is offline Offline
Newbie Poster

Re: "Craps", Game Help

 
0
  #3
Sep 26th, 2004
Thanks! ^^ I'm still having some issues with it, but it was just a test!
:evil::cry::evil::cry::evil::cry::evil::cry::evil:
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: Tales is an unknown quantity at this point 
Solved Threads: 1
Tales Tales is offline Offline
Light Poster
 
-1
  #4
2 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: Tales is an unknown quantity at this point 
Solved Threads: 1
Tales Tales is offline Offline
Light Poster

Your program working

 
0
  #5
2 Days Ago
  1. #include <iostream> // Includeing Headers
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. void ShowIntroScreen(void), ShowInformation(double Money);
  8. short GetBet(void), DoDiceThrow(void), GetAmount(void);
  9. double DoMoneyCalc(short Dice, short Bet, short BetMoney);
  10.  
  11. int main(void){
  12. double MoneyEarned, Money;
  13. unsigned short BetMoney;
  14. short DiceValue, Bet;
  15.  
  16. ShowIntroScreen();
  17. Money = 1000;
  18.  
  19. do{
  20. ShowInformation(Money);
  21.  
  22. Bet = GetBet();
  23. BetMoney = GetAmount();
  24. DiceValue = DoDiceThrow();
  25. MoneyEarned = DoMoneyCalc(DiceValue, Bet, BetMoney);
  26.  
  27. Money -= BetMoney;
  28.  
  29. if(MoneyEarned == 0){
  30. cout << "You have lost. The number was: " << DiceValue << endl << endl;
  31. }
  32. else{
  33. cout << "You won " << MoneyEarned - BetMoney;
  34. cout << " dollars. Number was: " << DiceValue;
  35. cout << endl << endl;
  36.  
  37. Money += MoneyEarned;
  38. }
  39. } while (Money > 100);
  40.  
  41. cout << "Game Over. Keep $" << Money << " for the ride home\n";
  42. return 0;
  43. }
  44.  
  45. void ShowIntroScreen(void){ // Rules and Start Up
  46. cout << " Welcome to Craps 1.0" << endl << endl;
  47. cout << " Here are the rules:" << endl << endl;
  48. cout << "You have 1000 dollars to start gambling. ";
  49. cout << endl << endl;
  50.  
  51. cout << "There are three types of bets. You can bet on: " << endl << endl;
  52. cout << " - 2 and 12 which will give you the ratio of 5 to 1 if you win.\n";
  53. cout << " - 4 and 10 which will give you the ratio of 2.5 to 1 if you win.\n";
  54. cout << " - 6 and 8 which will give you the ration of 1.5 and 1 if you win.";
  55. cout << endl << endl;
  56.  
  57. cout << "The minimum amount of money you can bet is 10 dollars and \n" " the highest amount of money you can bet is 100.";
  58. cout << endl << endl;
  59. cout << "Have fun playing!";
  60. cout << endl << endl;
  61. }
  62.  
  63. void ShowInformation(double Money){ // Money Display
  64. cout << "You have : " << Money << " dollars.";
  65. cout << endl << endl;
  66. }
  67.  
  68. short GetBet(void){ // Gets Bet Type
  69. unsigned short BetType;
  70. cout << "Enter a type of bet ( 1 = '6/8', 2 = '4/10', 3 = '2/12': ";
  71. cin >> BetType;
  72. if((BetType == 1) || (BetType == 2) || (BetType == 3))
  73. return BetType;
  74. else
  75. return 1;
  76. }
  77.  
  78. short DoDiceThrow(void){ // Randomized Numbers
  79. short DiceValue;
  80.  
  81. srand((unsigned) time(NULL));
  82. DiceValue = (rand() % 11) + 2;
  83.  
  84. if((DiceValue == 4 ) || (DiceValue == 10)){
  85. srand((unsigned) time (NULL));
  86. DiceValue = (rand () % 12) + 1;
  87. }
  88.  
  89. if((DiceValue == 2) || (DiceValue == 12)){
  90. srand((unsigned) time (NULL));
  91. DiceValue = (rand () % 12) + 1;
  92.  
  93. if((DiceValue == 2) || (DiceValue == 12)){
  94. srand((unsigned) time (NULL));
  95. DiceValue = (rand () % 12) + 1;
  96. }
  97. }
  98. return DiceValue;
  99. }
  100.  
  101. double DoMoneyCalc(short Dice, short Bet, short BetMoney){
  102. double MoneyEarned = 0;
  103.  
  104. switch(Bet){
  105. case 1:
  106. if((Dice == 6) || (Dice == 8)){ MoneyEarned = static_cast<double>(BetMoney) * 1.5; }
  107. break;
  108. case 2:
  109. if((Dice == 10) || (Dice == 4)){ MoneyEarned = static_cast<double>(BetMoney) * 2.5; }
  110. break;
  111. case 3:
  112. if((Dice == 3) || (Dice == 12)){ MoneyEarned = static_cast<double>(BetMoney) * 5; }
  113. break;
  114. default:
  115. MoneyEarned = 0;
  116. break;
  117. }
  118. return MoneyEarned;
  119. }
  120.  
  121. short GetAmount(void){
  122. unsigned short BetAmount;
  123.  
  124. cout << "Enter amount to bet (min 10 - max 100): ";
  125. cin >> BetAmount;
  126. if(BetAmount < 10){ BetAmount = 10; }
  127. if(BetAmount > 100){ BetAmount = 100; }
  128. return BetAmount;
  129. }
Last edited by Tales; 2 Days Ago at 9:35 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC