Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int humanTurn(int hTotal);
  8. int compTurn(int cTotal);
  9.  
  10. int main()
  11. {
  12. //seedint the random number generator
  13. srand(time(0));
  14.  
  15. int cTotal = 0; // keeos track of computer score
  16. int hTotal = 0; // keeos track of user score
  17.  
  18. while(cTotal < 100 && hTotal < 100)
  19. {
  20. //human turn first
  21. int humanTurnScore = humanTurn(hTotal);
  22.  
  23. hTotal += humanTurnScore;
  24.  
  25. int compTurnScore = compTurn(cTotal);
  26.  
  27. cTotal += compTurnScore;
  28.  
  29. }
  30.  
  31. if(hTotal >= 100)
  32. cout << "You won the game!!!";
  33. else
  34. cout << "The computer won the game.";
  35. return 0;
  36. }
  37.  
  38. int humanTurn(int hTotal)
  39. {
  40. int turnTotal = 0;
  41. char input = 'r';
  42.  
  43. while(input == 'r')
  44. {
  45. int face = 1 + rand()%6;
  46. cout << "You rolled " << face << endl;
  47.  
  48. if(face == 1)
  49. {
  50. cout << "you lost your turn, you lose all your points for this turn";
  51. input = 'h';
  52. turnTotal = 0; //you lose all your accumulated turn points, turnTotal reset to zero
  53. }
  54.  
  55. // you have not gotten 1, increment total and ask user to roll again
  56. else
  57. {
  58. turnTotal += face;
  59. cout << "do you want to roll again?" << endl;
  60. cin >> input;
  61.  
  62. }
  63. }
  64.  
  65. return turnTotal;
  66. }
  67.  
  68. int compTurn(int cTotal)
  69. {
  70. int turnTotal = 0;
  71.  
  72. while(turnTotal < 20)
  73. {
  74. int face = 1 *rand()%6;
  75. if(face == 1)
  76. return 0;
  77. else
  78. turnTotal += face;
  79. }
  80. cout << "computer gets " << turnTotal << " points this turn.\n"<< endl;
  81. return turnTotal;
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement