RSS Forums RSS

Two Player Craps

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Two Player Craps

  #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
  157.  
Last edited by Ancient Dragon : Oct 28th, 2007 at 3:51 pm. Reason: correct code tags
AddThis Social Bookmark Button
Reply With Quote  
Posts: 3,026
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 267
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Two Player Craps

  #2  
Oct 28th, 2007
Neither can we. You need to format your code, and use CODE tags.
Sometimes, when I look at my children, I say to myself, "Lillian, you should have remained a virgin."
-- Lillian Carter (mother of Jimmy Carter)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Views: 1044 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:20 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC