Advertisement
DavidsInferno

Rock Paper Scissors

Feb 1st, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. int choiceAI();
  7.  
  8. int main(void) {
  9. int input = 0;
  10. int inputAI = 0;
  11. bool finished = false;
  12. int scoreUser = 0;
  13. int scorePC = 0;
  14. srand((unsigned)time(NULL));
  15.  
  16.  
  17.  
  18. while (!finished) {
  19. cout << "Choose Rock(1), Scissors(2) or Paper(3)! If you are done press '-1'\Your input: ";
  20. cin >> input;
  21.  
  22. //Input check
  23. if (!cin) {
  24. cout << "Please input a number." << endl;
  25. cin.clear();//clear bad input flag
  26. cin.ignore(numeric_limits<streamsize>::max(), '\n');//discards input
  27. continue;
  28. }
  29. if (input < 1 || input > 3) {
  30. if (input == -1)
  31. return 0;
  32.  
  33. cout << "Enter one of the options!\n";
  34. continue;
  35. }
  36. //________________________
  37.  
  38.  
  39. inputAI = choiceAI();
  40.  
  41.  
  42. if ((input < inputAI && input + 2 != inputAI) || input - 2 == inputAI) {
  43. cout << "You win" << endl;
  44. scoreUser++;
  45. }
  46. else if (input == inputAI) {
  47. cout << "Draw" << endl;
  48. }
  49. else {
  50. cout << "You lose" << endl;
  51. scorePC++;
  52. }
  53. cout << "User: " << scoreUser << " " << "Score PC: " << scorePC;
  54. cout << endl << endl;
  55.  
  56. }
  57. return 0;
  58. }
  59.  
  60.  
  61. int choiceAI() {
  62. int temp = 0;
  63. temp = rand() % 3 + 1;
  64.  
  65. if (temp == 1)
  66. cout << "Opponent chose rock";
  67. else if (temp == 2)
  68. cout << "Opponent chose scissors";
  69. else
  70. cout << "Opponent chose paper";
  71.  
  72. cout << endl;
  73. return temp;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement