rock paper scissors shoot

ALSO, if the user were to input a char instead of int, it just loops the ERROR-2A...

thanks in advance!
Last edited on
> to convert char <r, p, s> into int
lines 55--69 «userWeapon char translattion to int»

> i've tried using just char for user's input with the rand() 3+1 for computer's input; it doesn't work
'1' is not the same as 1 '42' is not the same as 42

> ALSO, if the user were to input a char instead of int, it just loops the ERROR-2A...
1
2
cin.clear(); //the input failed, the stream was in an error state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard the erroneous input 
Hello kyoo,

As I looked over your program and have had a chance to run it this is what I see.

The header files are good, but you should also include "cctype" for the "std::toupper()".

using namespace std; // <--- Best not to use. If you are interested do a search here. There are many posts that cover this.

I have found this to be the best way to use "srand()" srand(static_cast<size_t>(time(nullptr)));

Something you could do is define std::string weapon[]{ "", "Paper", "Rock", "Scissors" }; then in line 71 the "cout" would be cout << "2) Computer randomly played " << weapon[computerNumber] <<. This would replace all the if/else if statements to change a number to a word.

Inside the do/while loop you are using to many {}s to define blocks. These are not needed and could work against you since the block defines a scope thet ends with the closing }.

In addition to what ne555 has said the else state where the lines
1
2
cin.clear(); //the input failed, the stream was in an error state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard the erroneous input 
should go should be done after line 28. And for an input of a number it would work better as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "Paper-Rock-Scissors GAME by Quiyet\n"
	<< "----------------------------------------------\n\n"
	<< "1) Player choose (1-paper, 2-rock, or 3-scissors)? "; // prompts player to choose a weapon
cin >> userWeapon; //player chooses and shoots a weapon

while (!std::cin || (userWeapon < 1 || userWeapon > 3))
{
	std::cout << "\n  ERROR-2A: Invalid input. Must be a character"
		"from the list of 'P', 'R', or 'S'.\n\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		cout << "1) Player choose (1-paper, 2-rock, or 3-scissors)? "; // prompts player to choose a weapon
		cin >> userWeapon; //player chooses and shoots a weapon
}

The error message would have to be adjusted to reflect that numbers are needed or that a character is not allowed. It is better to catch it here than in the else statement.

The last do/while look is OK. Since you use "std::toupper;" to change the case of the input then in the while condition of the first do/while you only need to check for a capital letter. There should not be a lower case letter.

The first time I ran the program I found that when I entered "2" and the computer chose "3" which took me to line 133 which has the wrong output for the graphics and the wrong message. Also it added to the "loss" when it should have added to the "win".

I have an idea for entering a character for user input, but I want to test it before I say anything.

Hope that helps,

Andy

Edit: typo
Last edited on
Hello kyoo,

I grew up knowing the game as "Rock, Paper, Scissors". You having the order wrong at the beginning of the program throws the if/else if statements off. When I rearranges the beginning of the program it worked better.

The first idea I had for changing a char to an int did not work as well as I hoped , so I had to use if/else if statements,

I had to change the while loop from my previous response to deal with entering a letter instead of a number.

I removed the {}s you had to define separate blocks and it runs fine with out them.

This is your code rearranged. Take a look and notice the comments and the differences.
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include <cctype>  // <--- for std::toupper and std::tolower.

using namespace std;  // <--- Best not to use.


int main()
{
	srand(static_cast<size_t>(time(nullptr)));

	int userWeapon; //so player can choose their weapon to shoot
	//string computerWeapon; //computer chooses and shoots a weapon  No longer needed.
	const std::string weapon[]{ "", "Rock", "Paper", "Scissors" };  // <--- Changed order to fit if statements starting at line 63.
	char ans; //user's answer for a rematch

	//game scoring
	int win = 0;
	int tie = 0;
	int lose = 0;

	do
	{
		//user prompts
		cout << "Paper-Rock-Scissors GAME by Quiyet\n"
			<< "----------------------------------------------\n\n"
			<< "1) Player choose (R - rock, P - paper, or S - scissors)? "; // prompts player to choose a weapon.  <--- Changed order.
		cin >> ans; //player chooses and shoots a weapon
		ans = toupper(ans);


		while (ans != 'P' && ans != 'R' && ans != 'S')
		{
			std::cout << "\n  ERROR-2A: Invalid input. Must be a character"
				"from the list of 'R', 'P', or 'S'.\n\n";  // <--- Changed order.
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

			cout << "1) Player choose (R - rock, P - paper, or S - scissors)? "; // prompts player to choose a weapon. <--- Changed order.
			cin >> ans; //player chooses and shoots a weapon
			ans = toupper(ans);
		}

		if (ans == 'R') userWeapon = 1;
		else if (ans == 'P') userWeapon = 2;
		else if (ans == 'S') userWeapon = 3;

		cout << "\n";


		//computer choice randomizer + game outcomes
		int computerNumber = rand() % 3 + 1; //this randomizes computer's choices
		//int userWeapon1;  // <--- Not used.

		cout << "1) Plaayer's choice is: " << weapon[userWeapon]  // <--- Added for testing.
			<< "\n2) Computer randomly played " << weapon[computerNumber] << ".\n\n"
			<< "3) Determination:\n\n"
			<< "  Player           Computer\n"
			<< "----------       ------------\n\n";

		//if-else if statements for game outcome
		if (userWeapon == 1 && computerNumber == 1)
		{
			cout << "   rrrr           rrrr\n";
			cout << "  r  rrr         r  rrr\n";
			cout << " r  r rrr       r  r rrr\n";
			cout << " r   rrrr       r   rrrr\n";
			cout << "  r   rr         r   rr\n";
			cout << "   rrrr           rrrr\n";
			cout << "It is a tie. Therefore, no one wins.\n\n";
			tie++;
		}
		else if (userWeapon == 1 && computerNumber == 2)
		{
			cout << "   rrrr         ppppp\n";
			cout << "  r  rrr        p  ppp\n";
			cout << " r  r rrr       p  pppp\n";
			cout << " r   rrrr       p     p\n";
			cout << "  r   rr        p     p\n";
			cout << "   rrrr         ppppppp\n";
			cout << "Paper covers Rock. Therefore, Computer wins.\n\n";
			lose++;
		}
		else if (userWeapon == 1 && computerNumber == 3)
		{
			cout << "   rrrr             s\n";
			cout << "  r  rrr         s  s\n";
			cout << " r  r rrr       s sssssss\n";
			cout << " r   rrrr        s  s\n";
			cout << "  r   rr          s s\n";
			cout << "   rrrr             s\n";
			cout << "Rock breaks Scissors. Therefore, User wins.\n\n";
			win++;
		}
		else if (userWeapon == 2 && computerNumber == 1)
		{
			cout << " ppppp           rrrr\n";
			cout << " p  ppp         r  rrr\n";
			cout << " p  pppp       r  r rrr\n";
			cout << " p     p       r   rrrr\n";
			cout << " p     p        r   rr\n";
			cout << " ppppppp         rrrr\n";
			cout << "Paper covers Rock. Therefore, User wins.\n\n";
			win++;
		}
		else if (userWeapon == 2 && computerNumber == 2)
		{
			cout << " ppppp         ppppp\n";
			cout << " p  ppp        p  ppp\n";
			cout << " p  pppp       p  pppp\n";
			cout << " p     p       p     p\n";
			cout << " p     p       p     p\n";
			cout << " ppppppp       ppppppp\n";
			cout << "It is a tie. Therefore, no one wins.\n\n";
			tie++;
		}
		else if (userWeapon == 2 && computerNumber == 3)
		{
			cout << " ppppp             s\n";
			cout << " p  ppp         s  s\n";
			cout << " p  pppp       s sssssss\n";
			cout << " p     p        s  s\n";
			cout << " p     p         s s\n";
			cout << " ppppppp           s\n";
			cout << "Scissors cut paper. Therefore, Computer wins.\n\n";
			lose++;
		}
		else if (userWeapon == 3 && computerNumber == 1)
		{
			cout << "     s             rrrr\n";
			cout << "  s  s            r  rrr\n";
			cout << " s sssssss       r  r rrr\n";
			cout << "  s  s           r   rrrr\n";
			cout << "   s s            r   rr\n";
			cout << "     s             rrrr\n";
			cout << "Rock breaks Scissors. Therefore, Computer wins.\n\n";
			lose++;
		}
		else if (userWeapon == 3 && computerNumber == 2)
		{
			cout << "     s           ppppp\n";
			cout << "  s  s           p  ppp\n";
			cout << " s sssssss       p  pppp\n";
			cout << "  s  s           p     p\n";
			cout << "   s s           p     p\n";
			cout << "     s           ppppppp\n";
			cout << "Scissors cuts Paper. Therefore, User wins.\n\n";
			win++;
		}
		else if (userWeapon == 3 && computerNumber == 3)
		{
			cout << "     s                s\n";
			cout << "  s  s             s  s\n";
			cout << " s sssssss        s sssssss\n";
			cout << "  s  s             s  s\n";
			cout << "   s s              s s\n";
			cout << "     s                s\n";
			cout << "It is a tie. therefore, no one wins.\n\n";
		}
	
		//game scores
		cout << "4) Player Score: " << win <<
			"     Computer Score: " << lose << "\n\n";

		//rematch prompt
		do
		{
			cout << "Rematch (Y-yes || N-no)? ";
			cin >> ans;
			cout << "\n";
			ans = toupper(ans); //turns ans case-nonsensitive

			if (ans == 'Y' || ans == 'N')
				break;
			else
				cout << "Invalid input. Must be Y || N only.\n\n";
		} while (true);

	} while (ans == 'Y');  // <--- The test for a lower case letter is not needed.

	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}


Hope that helps,

Andy

Edit:
Last edited on
Hello kyoo,

Do not edit and remove anything from your original post. Anyone who might look at this later will have no idea what is being talked about. It is rather confusing to those that follow.

Andy
Topic archived. No new replies allowed.