How to simplfy my code? Paper-rock-scissor game

This is an exercise from the textbook by Savitch
Write a program to score the paper-rock-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 breaks scissors,
Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase
as well as uppercase letters. Your program should include a loop that lets
the user play again until the user says she or he is done.


And here's my code

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;

int main()
{
	char p1, p2;
	string check;
	do
	{
		cout << "Player 1, please enter your choice [P/R/S]" << endl;
		cin >> p1;
		cout << "Player 2, please enter your choice [P/R/S]" << endl;
		cin >> p2;

		if(p1 == p2)
			cout << "Nobody wins" << endl;
		if( (p1 == 'P' && p2 == 'p') || (p1 == 'p' && p2 == 'P') )
			cout << "Nobody wins" << endl;
		if( (p1 == 'S' && p2 == 's') || (p1 == 'S' && p2 == 's') )
			cout << "Nobody wins" << endl;
		if( (p1 == 'R' && p2 == 'r') || (p1 == 'R' && p2 == 'r') )
			cout << "Nobody wins" << endl;

		if((p1 == 'P' || p1 == 'p') && (p2 == 'R' || p2 == 'r'))
			cout << "Paper covers rock, Player 1 win" << endl;
		if((p1 == 'R' || p1 == 'r') && (p2 == 'P' || p2 == 'p'))
			cout << "Paper covers rock, Player 2 win" << endl;

		if((p1 == 'P' || p1 == 'p') && (p2 == 'S' || p2 == 's'))
			cout << "Scissors cut paper, Player 2 win" << endl;
		if((p1 == 'S' || p1 == 's') && (p2 == 'P' || p2 == 'p'))
			cout << "Scissors cut paper, Player 1 win" << endl;

		if((p1 == 'R' || p1 == 'r') && (p2 == 'S' || p2 == 's'))
			cout << "Rock breaks scissors, Player 1 win" << endl;
		if((p1 == 'S' || p1 == 's') && (p2 == 'R' || p2 == 'r'))
			cout << "Rock breaks scissors, Player 1 win" << endl;

		cout << "Do you want to continue? [y/n]" << endl;
		cin >> check;
	}while(check != "n");

	return 0;
}



My program can run without problem but my question is:
Is it possible to simplify the code? It seems I can use less lines to finish the program.

What I have in mind is: I have every winning case, can I simply use the else statement to summarize all draw situations?

Or there is a even better way to make my code shorter?
Last edited on
Personally I would use toupper so you don't have to keep checking uppercase vs lowercase and I would use nested if statements.
http://www.cplusplus.com/reference/cctype/toupper/

You could only need 1 check for a tie if you do it correctly. How you have it is almost correct here.
1
2
if(p1 == p2)
	cout << "Nobody wins" << endl;


Here is 1 way of doing it, I'm sure there are nicer ways that it could be done though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(toupper(p1) == 'P')
{
	if(toupper(p2) == 'S')
		cout << "Player 2 won" << endl;

	else
		cout << "Player 1 won" << endl;
}

else if() 
{
	//etc 
}

else
{
	//etc
}
Last edited on
You could only need 1 check for a tie if you do it correctly. How you have it is almost correct here.
1
2
if(p1 == p2)
	cout << "Nobody wins" << endl;



How about Player 1 types "P" and Player 2 types "p" or vice versa?

So the check should be like this?
if(p1 == p2 || toupper(p1) == p2 || tolower(p1) == p2)
which requires at least 3 checks.
Perhaps change the input to upper case immediately after input,
1
2
3
4
5
6
    cout << "Player 1, please enter your choice [P/R/S]" << endl;
    cin >> p1;
    cout << "Player 2, please enter your choice [P/R/S]" << endl;
    cin >> p2;
    p1 = toupper(p1);
    p2 = toupper(p2);

From that point on, everything is certain to be upper case only.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	char p1, p2;
	string check;
	do
	{
		cout << "Player 1, please enter your choice [P/R/S]" << endl;
		cin >> p1;
		cout << "Player 2, please enter your choice [P/R/S]" << endl;
		cin >> p2;
		
		p1 = toupper(p1);
		p2 = toupper(p2);
		if	(p1 == p2)
			cout << "Nobody wins" << endl;
		else
		{
			switch (p1)
			{
			case 'P':
				if	(p2 == 'R')
					cout << "Paper covers rock, Player 1 wins" << endl;
				else
					cout << "Scissors cut paper, Player 2 wins" << endl;
				break;
				
			case 'R':
				if	(p2 == 'S')
					cout << "Rock breaks scissors, Player 1 wins" << endl;
				else
					cout << "Paper covers rock, Player 2 wins" << endl;
				break;	
				
			case 'S':
				if	(p2 == 'P')
					cout << "Scissors cut paper, Player1 wins" << endl;
				else
					cout << "Rock breaks scissors, Player 2 wins" << endl;
				break;
			}
		}
		cout << "Do you want to continue? [y/n]" << endl;
		cin >> check;
	}while(check != "n");

	return 0;
}


Is it simple enough?
Thanks everyone! I learn so many new codes now!
Topic archived. No new replies allowed.