InnaSibirova

Камень, ножницы, бумага

Oct 18th, 2019
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <ctime>
  5. using namespace std;
  6. string check_user_input() {
  7.     string user_answer;
  8.     int i = 0;
  9.     while (i != 1) {
  10.     cin >> user_answer;
  11.   if (user_answer == "Skissor" || user_answer == "Paper" || user_answer == "Stone"){
  12.     i = 1;
  13.   }
  14.   else if (user_answer == "Quit" ) {
  15.     exit(0);
  16.   }
  17.   else {
  18.     cout << "Wrong answer" << endl << "Make a move ";
  19.   }
  20. }
  21. return user_answer;
  22. }
  23.  
  24.  
  25. string generate_computer_answer() {
  26.   string computer_answer;
  27.   int n;
  28.   srand(time(0));
  29.   n = 1 + (rand() % 3);
  30.   if (n == 1) {
  31.     computer_answer = "Skissor";
  32.   }
  33.   else if (n == 2) {
  34.     computer_answer = "Paper"; 
  35.   }
  36.   else {
  37.     computer_answer = "Stone";
  38.   }
  39.   return computer_answer;
  40. }
  41.  
  42.  
  43. int play(string user_answer, string computer_answer, int &user_score, int &computer_score) {
  44.   if((user_answer == "Skissor" && computer_answer == "Paper") || (user_answer == "Paper" && computer_answer == "Stone") || (user_answer == "Stone" && computer_answer == "Skissor")) {
  45.     cout << "User win" << endl;
  46.     user_score = user_score + 1;
  47.     }
  48.     else if((user_answer == "Skissor" && computer_answer == "Stone") || (user_answer == "Paper" && computer_answer == "Skissor") || (user_answer == "Stone" && computer_answer == "Paper")) {
  49.         cout << "Computer win" << endl;
  50.         computer_score = computer_score + 1;
  51.     }
  52.     else {
  53.         cout << "Draw" << endl;
  54.     }
  55.     return user_score, computer_score;
  56.  }
  57.  
  58. int main() {
  59.   int user_score = 0;
  60.   int computer_score = 0;
  61.   int a;
  62.   string user_answer;
  63.   string computer_answer;
  64.  
  65.   while (user_answer != "Quit") {
  66.  
  67.   cout << "Make a move ";
  68.   user_answer = check_user_input();
  69.   computer_answer = generate_computer_answer();
  70.   play(user_answer, computer_answer, user_score, computer_score);
  71.   cout << endl;
  72.   cout << "          Score table:" << endl << "User scores        " << "Computer scores" << endl << user_score << "                  " << computer_score << endl;
  73.  
  74. }
  75. return 0;
  76. }
RAW Paste Data