View Single Post
Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Two Player Craps

 
0
  #1
Oct 28th, 2007
This is my first time posting an I need some quick help. I need to get this program to work for two players. once Player 1 loses Player 2 gets their turn. the program works fine just can't figure out the while loop for 2 player.
  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. /*=================================*/
  8. // enumeration constants represent game status
  9. enum Status { CONTINUE, WON, LOST };
  10. /*=================================*/
  11. /* function prototype */
  12. int rollDice( void );
  13. void playGame ( Status & gameStatus );
  14. void displayWonOrLost ( Status, int &, int & );
  15. /*=====================================*/
  16. int main()
  17. {
  18. int BankBalance = 1000;
  19. int Wager = 100;
  20. int count;
  21. char Response;
  22. const int player1= 1;
  23. const int player2= 2;
  24. Status gameStatus; // can contain CONTINUE, WON or LOST
  25.  
  26.  
  27. cout << " Player 1 wagers $ "<< Wager << endl;
  28. cout << " Player 1 Is rolling the DICE " << endl;
  29.  
  30. while ( Wager <= BankBalance ) {
  31. cout << "Continue with current wager (Y) or end game (E)?" << endl;
  32. cin >> Response;
  33.  
  34. switch ( Response ) {
  35.  
  36. case 'Y':
  37. case 'y':
  38. playGame( gameStatus );
  39. displayWonOrLost ( gameStatus, BankBalance, Wager );
  40. break;
  41.  
  42. case 'E':
  43. case 'e':
  44. cout << "Your final balance is " << BankBalance << endl;
  45. cout << "Thank you, come again!" << endl;
  46.  
  47. return (0);
  48. break;
  49.  
  50. /* case 'N':
  51. case 'n':
  52. cout << "Please enter the new wager!" << endl;
  53. cin >> Wager;
  54. break;*/
  55.  
  56. default:
  57. cout << "Incorrect Response. Please try again!" << endl;
  58. break;
  59.  
  60. } // end switch
  61.  
  62. }
  63.  
  64. if ( BankBalance == 0 )
  65. cout << "Sorry, you're busted!" << endl;
  66.  
  67. return (0); // indicates successful termination
  68.  
  69. } // end main
  70.  
  71. void displayWonOrLost ( Status gameStatus, int & passedBalance, int &
  72. passedWager )
  73. {
  74. // display won or lost message
  75.  
  76. if ( gameStatus == WON ) {
  77. passedBalance += passedWager;
  78. cout << "Player wins, new balance is " << passedBalance << endl;
  79. }
  80. else {
  81. passedBalance -= passedWager;
  82. cout << "That's craps! Player 1 loses " << endl;
  83. cout << "Player 1 has $" << passedBalance << endl;
  84.  
  85. } // End display won or lost message
  86. }
  87.  
  88. void playGame (Status & gameStatus)
  89. {
  90. int sum;
  91. int myPoint;
  92.  
  93. // randomize random number generator using current time
  94. srand( time( 0 ) );
  95.  
  96. sum = rollDice(); // first roll of the dice
  97.  
  98. // determine game status and point based on sum of dice
  99. switch ( sum ) {
  100.  
  101. // win on first roll
  102. case 7:
  103. case 11:
  104. gameStatus = WON;
  105. break;
  106.  
  107. // lose on first roll
  108. case 2:
  109. case 3:
  110. case 12:
  111. gameStatus = LOST;
  112. break;
  113.  
  114. // remember point
  115. default:
  116. gameStatus = CONTINUE;
  117. myPoint = sum;
  118. cout << "Point is " << myPoint << endl;
  119. break; // optional
  120.  
  121. } // end switch
  122.  
  123. // while game not complete ...
  124. while ( gameStatus == CONTINUE ) {
  125. sum = rollDice(); // roll dice again
  126.  
  127. // determine game status
  128. if ( sum == myPoint ) // win by making point
  129. gameStatus = WON;
  130. else
  131. if ( sum == 7 ) // lose by rolling 7
  132. gameStatus = LOST;
  133.  
  134. } // end while
  135.  
  136. } // end playGame
  137.  
  138. // roll dice, calculate sum and display results
  139. int rollDice( void )
  140. {
  141. int die1;
  142. int die2;
  143. int workSum;
  144.  
  145. die1 = 1 + rand() % 6; // pick random die1 value
  146. die2 = 1 + rand() % 6; // pick random die2 value
  147. workSum = die1 + die2; // sum die1 and die2
  148.  
  149. // display results of this roll
  150. cout << "Player rolled " << die1 << " + " << die2
  151. << " = " << workSum << endl;
  152.  
  153.  
  154. return workSum; // return sum of dice
  155.  
  156. } // end function rollDice
Last edited by Ancient Dragon; Oct 28th, 2007 at 4:51 pm. Reason: correct code tags
Reply With Quote