Rock-paper-scissors game

I got about halfway into coding this game using if/else statements but then I realized the next question wants me to allow the user to play as many times as they want. So I'm guessing I could have designed the game using a while loop; but, I am not sure how I would go about doing that. I also don't know how I'd allow the loop to repeat so that the game can played as much as the user wants. Any advice would be greatly appreciated. The question is as follows:

Write a program to score the rock-paper-scissor game. Each of two users types in either P, R, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock break scissors, Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase as well as uppercase letters.
Using problem 5, allow the user to play the game as many times as they want until they are done.

Also, here is where I got to with the if/else statements before realizing that I may be able to code this more effectively using a loop:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    string userInput1, userInput2;
     cout << "Rock-paper-scissor game - player one enter your choice: ";
     cin >> userInput1;
     cout << "Player two enter your choice: ";
     cin >> userInput2;
     if(userInput1 == "p" || userInput1 == "P" and userInput2 == "p" ||
             userInput2 == "P")
     {
         cout << "Nobody wins.";
     }
     else if(userInput1 == "r" || userInput1 == "R" and userInput2 == "r" ||
             userInput2 == "R")
     {
         cout << "Nobody wins.";
     }
     else if(userInput1 == "s" || userInput1 == "S" and userInput2 == "s" ||
             userInput2 == "S")
     {
         cout << "Nobody wins.";
     }
     else if(userInput1 == "p" || userInput1 == "P" and userInput2 == "r" ||
             userInput2 == "R")
     {
         cout << "Player one wins - paper covers rock.";
     }
     else if(userInput1 == "r" || userInput1 == "R" and userInput2 == "p" ||
             userInput2 == "P")
     {
         cout << "Player two wins - paper covers rock.";
     }
     
One idea:
1
2
3
4
5
6
7
8
9
10
11
12
#include <conio.h>
  bool game_over = false;
  int choice = 0;

while (!game_over)
{
   // play game as before
   // at the end of the game
   cout << "\nDo you want to play another game - enter n for no, all other keys mean yes: ";
   choice = _getche();
   game_over = toupper(choice) == 'N';
}
Okay I appreciate that I'll go ahead and implement that to repeat it. I'll have to study up on what some of those commands are. Is there a more effective way to code the game itself though? As opposed to if/ and lots of elseif statements?
Topic archived. No new replies allowed.