juinda

guess number

Jun 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <ctime>
  5.  
  6. #define LOW 0
  7. #define HIGH 100
  8.  
  9. using namespace std;
  10.  
  11. class Player {
  12. private:
  13. int low = LOW;
  14. int high = HIGH;
  15. public:
  16. virtual int getGuess() = 0;
  17. void setLow(int ip) { low = ip; }
  18. void setHigh(int ip) { high = ip; }
  19. int getLow() { return low; }
  20. int getHigh() { return high; }
  21. void ShowRange() { cout << low << "~" << high << endl; }
  22. void Clear(){
  23. low = LOW;
  24. high = HIGH;
  25. }
  26. };
  27.  
  28. class HumanPlayer :public Player {
  29. public:
  30. int getGuess() {
  31. int temp;
  32. while (1) {
  33. //cout << "enter : ";
  34. cin >> temp;
  35. if (temp > getLow() && temp < getHigh()) {
  36. return temp;
  37. }
  38. else cout << "you guess out of range." << endl;
  39. }
  40. }
  41. };
  42.  
  43. class ComputerPlayer :public Player {
  44. public:
  45. int getGuess() {
  46. int temp = rand() % (getHigh() - getLow() - 1) + 1 + getLow();
  47. cout << "computer guess " << temp << endl;
  48. return temp;
  49. }
  50. };
  51.  
  52. int checkForWin(int guess, int answer)
  53. {
  54. if (answer == guess)
  55. {
  56. cout << "You're right! You win!" << endl;
  57. return 1;
  58. }
  59. else if (answer < guess) {
  60. cout << "Your guess is too high." << endl;
  61. return 0;
  62. }
  63. else {
  64. cout << "Your guess is too low." << endl;
  65. return -1;
  66. }
  67. }
  68.  
  69. void play(Player &player1, Player &player2)
  70. {
  71. srand(time(NULL));
  72.  
  73. int answer = 0, guess = 0;
  74. answer = rand() % 99 + 1;
  75. int win = 0;
  76.  
  77. while (1)
  78. {
  79. cout << "Player 1's turn to guess." << endl;
  80. guess = player1.getGuess();
  81. win = checkForWin(guess, answer);
  82. if (win == 1) {
  83. player1.Clear();
  84. player2.Clear();
  85. return;
  86. }
  87. else if (win == 0) {
  88. player1.setHigh(guess);
  89. player2.setHigh(guess);
  90. }
  91. else {
  92. player1.setLow(guess);
  93. player2.setLow(guess);
  94. }
  95. player1.ShowRange();
  96.  
  97. cout << "Player 2's turn to guess." << endl;
  98. guess = player2.getGuess();
  99. win = checkForWin(guess, answer);
  100. if (win == 1) {
  101. player1.Clear();
  102. player2.Clear();
  103. return;
  104. }
  105. else if (win == 0) {
  106. player1.setHigh(guess);
  107. player2.setHigh(guess);
  108. }
  109. else {
  110. player1.setLow(guess);
  111. player2.setLow(guess);
  112. }
  113. player1.ShowRange();
  114. }
  115. }
  116.  
  117. int main()
  118. {
  119. HumanPlayer playerH1, playerH2;
  120. ComputerPlayer playerC1, playerC2;
  121.  
  122. play(playerH1, playerH2);
  123. play(playerH1, playerC1);
  124. play(playerC1, playerC2);
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment