Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <ctime>
- using namespace std;
- string check_user_input() {
- string user_answer;
- int i = 0;
- while (i != 1) {
- cin >> user_answer;
- if (user_answer == "Skissor" || user_answer == "Paper" || user_answer == "Stone"){
- i = 1;
- }
- else if (user_answer == "Quit" ) {
- exit(0);
- }
- else {
- cout << "Wrong answer" << endl << "Make a move ";
- }
- }
- return user_answer;
- }
- string generate_computer_answer() {
- string computer_answer;
- int n;
- srand(time(0));
- n = 1 + (rand() % 3);
- if (n == 1) {
- computer_answer = "Skissor";
- }
- else if (n == 2) {
- computer_answer = "Paper";
- }
- else {
- computer_answer = "Stone";
- }
- return computer_answer;
- }
- int play(string user_answer, string computer_answer, int &user_score, int &computer_score) {
- if((user_answer == "Skissor" && computer_answer == "Paper") || (user_answer == "Paper" && computer_answer == "Stone") || (user_answer == "Stone" && computer_answer == "Skissor")) {
- cout << "User win" << endl;
- user_score = user_score + 1;
- }
- else if((user_answer == "Skissor" && computer_answer == "Stone") || (user_answer == "Paper" && computer_answer == "Skissor") || (user_answer == "Stone" && computer_answer == "Paper")) {
- cout << "Computer win" << endl;
- computer_score = computer_score + 1;
- }
- else {
- cout << "Draw" << endl;
- }
- return user_score, computer_score;
- }
- int main() {
- int user_score = 0;
- int computer_score = 0;
- int a;
- string user_answer;
- string computer_answer;
- while (user_answer != "Quit") {
- cout << "Make a move ";
- user_answer = check_user_input();
- computer_answer = generate_computer_answer();
- play(user_answer, computer_answer, user_score, computer_score);
- cout << endl;
- cout << " Score table:" << endl << "User scores " << "Computer scores" << endl << user_score << " " << computer_score << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment