Simple Craps Game

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

Join Date: Oct 2008
Posts: 4
Reputation: thinfineline is an unknown quantity at this point 
Solved Threads: 0
thinfineline thinfineline is offline Offline
Newbie Poster

Simple Craps Game

 
0
  #1
Dec 2nd, 2008
I'm trying to get the game below to work with 2 players. It's setup the way I need it (even the entering the numbers for the die instead of using a rand function and not allowing the bet amount to change(crazy teacher)) I just can't figure out how to make it two players. Also I'm supposed to use the rand function for one die to determine which player goes first. Heres what I've got.
Any help would really be appreciated.
================================================
  1. /* header files */
  2. #include <iostream>
  3. #include <cstdlib> // contains function prototypes for functions srand and rand
  4. #include <ctime> // contains prototype for function time
  5. using namespace std;
  6.  
  7. // enumeration constants represent game status
  8. enum Status { CONTINUE, WON, LOST };
  9.  
  10. int rollDice( void );
  11. void playGame ( Status & gameStatus );
  12. void displayWonOrLost ( Status, int &, int & );
  13. int main()
  14. {
  15. int BankBalance = 1000;
  16. int Wager = 100;
  17. int count;
  18. char Response;
  19. const int player1= 1;
  20. const int player2= 2;
  21. Status gameStatus; // can contain CONTINUE, WON or LOST
  22.  
  23.  
  24. cout << " Player 1 wagers $ "<< Wager << endl;
  25. cout << " Player 1 Is rolling the DICE " << endl;
  26.  
  27. while ( Wager <= BankBalance ) {
  28. cout << "Continue with current wager (Y) or end game (E)?" << endl;
  29. cin >> Response;
  30.  
  31. switch ( Response ) {
  32.  
  33. case 'Y':
  34. case 'y':
  35. playGame( gameStatus );
  36. displayWonOrLost ( gameStatus, BankBalance, Wager );
  37. break;
  38.  
  39. case 'E':
  40. case 'e':
  41. cout << "Your final balance is " << BankBalance << endl;
  42. cout << "Thank you, come again!" << endl;
  43.  
  44. return (0);
  45. break;
  46.  
  47. default:
  48. cout << "Incorrect Response. Please try again!" << endl;
  49. break;
  50.  
  51. } // end switch
  52.  
  53. }
  54.  
  55. if ( BankBalance == 0 )
  56. cout << "Sorry, No money remaining!" << endl;
  57.  
  58. return (0); // indicates successful termination
  59.  
  60. } // end main
  61.  
  62. void displayWonOrLost ( Status gameStatus, int & balance, int &
  63. wager )
  64. {
  65. // display won or lost message
  66.  
  67. if ( gameStatus == WON ) {
  68. balance += wager;
  69. cout << "Player wins, new balance is " << balance << endl;
  70. }
  71. else {
  72. balance -= wager;
  73. cout << "That's craps! Player 1 loses " << endl;
  74. cout << "Player 1 has $" << balance << endl;
  75.  
  76. } // End display won or lost message
  77. }
  78.  
  79. void playGame (Status & gameStatus)
  80. {
  81. int sum;
  82. int myPoint;
  83.  
  84. sum = rollDice(); // first roll of the dice
  85.  
  86. // determine game status and point based on sum of dice
  87. switch ( sum )
  88. {
  89. // win on first roll
  90. case 7:
  91. case 11:
  92. gameStatus = WON;
  93. break;
  94.  
  95. // lose on first roll
  96. case 2:
  97. case 3:
  98. case 12:
  99. gameStatus = LOST;
  100. break;
  101.  
  102. // remember point
  103. default:
  104. gameStatus = CONTINUE;
  105. myPoint = sum;
  106. cout << "Point is " << myPoint << endl;
  107. break; // optional
  108.  
  109. } // end switch
  110.  
  111. // while game not complete ...
  112. while ( gameStatus == CONTINUE ) {
  113. sum = rollDice(); // roll dice again
  114.  
  115. // determine game status
  116. if ( sum == myPoint ) // win by making point
  117. gameStatus = WON;
  118. else
  119. if ( sum == 7 ) // lose by rolling 7
  120. gameStatus = LOST;
  121.  
  122. } // end while
  123.  
  124. } // end playGame
  125.  
  126. // roll dice, calculate sum and display results
  127. int rollDice( void )
  128. {
  129. int die1;
  130. int die2;
  131. int workSum;
  132. cout<<"Please enter a value for die 1: ";
  133. cin>>die1;
  134. cout<<"please enter a value for die 2: ";
  135. cin>>die2;
  136. workSum = die1 + die2; // sum die1 and die2
  137.  
  138. // display results of this roll
  139. cout << "Player rolled " << die1 << " + " << die2
  140. << " = " << workSum << endl;
  141.  
  142.  
  143. return workSum; // return sum of dice
  144.  
  145. } // end function rollDice
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Simple Craps Game

 
0
  #2
Dec 3rd, 2008
Originally Posted by thinfineline View Post
I'm trying to get the game below to work with 2 players. It's setup the way I need it (even the entering the numbers for the die instead of using a rand function and not allowing the bet amount to change(crazy teacher))
First off you teacher is trying to save you some complexity. Complexity KILLS.

Second, Assuming that you seem to have done some c++. I think this is an excellent example of the break between old-style procedural programming and the beginnings of object orientated programming.
You can to this problem in a procedural way BUT it is easier to do it in an OO manor.

Thirdly. I suspect that you were initially asked to write a craps game and then asked to modify, which reinforces the difference. It is important that those who started very OO at the beginning are laughing and those that didn't are facing a big re-write.

Solution:

What all mathematicians know (instinctively), is that there are only
three number 0, 1, and infinity. 0 is an easy case (no-code) and 1 is what you currently have. 2 or many makes no difference it is the same.

For example you have a variable BankBalance. What you want is a BankBalance for every player, you have a Wager, you want one for every player.

You need to do several things.

(a) write a class for player. That has the things that are associated to the player.

(b) modifiy game method that acts on a player class.

(c) Manage the program so that you create 2 or however many players that are required and interact with the program.

Finally look up rand() and that will tell you how to get a random number for the dice throw. However, as has been discussed on recent threads rand() is not truly random but good enough for this project.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC