943,843 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1661
  • C++ RSS
Oct 28th, 2007
0

Two Player Craps

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Oct 28th, 2007
0

Re: Two Player Craps

Neither can we. You need to format your code, and use CODE tags.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pointers and multi-dimensional arrays
Next Thread in C++ Forum Timeline: Not sure why [continue] is not working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC