Rock Paper Scissors

I'm taking a C++ class where I have to write code for a rock paper scissors game where the computer randomly chooses one of the three and if there is a tie, the user selects again. At the of the game, the user is asked whether or not they wanna play again. The results of the game are to be recorded and written to a file. I don't really have a question about it, I guess I'm just putting it here for a reference. It doesn't output the results to a file and the tie stuff doesn't work yet. Suggestions are welcome and I will update it as I finish it. This is my work so far.

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
// Rock Paper Scissors

#include <iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main() {
	enum choice {ROCK, PAPER, SCISSORS};
	
	choice select;
	char user;
	bool play_again;
	string play;
	int num_games = 0;
	int player_wins = 0;
	int computer_wins = 0;
	bool tie = false;
	int num_ties = 0;
	
	// Ask the user if they would like to play.
	cout << "Would you like to play Rock, Paper, Scissors? (y or n): ";
	cin >> play;
	
	// If they answer yes, then play, otherwise no.
	if(play == "y" || play == "Y"){
		play_again = true;
		num_games++;
		}
	
	// Start playing the actual game if they want to play.
	while(play_again != false){
		cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
		cin >> user;
		cout << endl;
		
		// Taking the input and applying it to constant in enum.
		if (user == 'r' || user == 'R'){
			select = ROCK;
			}
		
		else if (user == 'p' || user == 'P'){
			select = PAPER; 
			}
			
		else if (user == 's' || user == 'S'){
			select = SCISSORS;
			}
		
		else {
			cout << "Come on... That's not an option." << endl;
			cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
			cin >> user;
			}
		
		
		long  seed = time(NULL); // gets current time
		srand(seed);
		
		// Computer's random selection
		int computer = rand();
		
		//DEBUG: cout << computer << endl;
		
		// Computer's random selection
		computer = rand() % 3;
		
		//DEBUG: cout << computer << endl << endl;;
		//DEBUG: cout << "Here is a test: " << select << endl;
		
		
		// Applying the computer's selection to the appropriate value in enum.
		choice cpu_select;
		
		if (computer == 0){
			cpu_select = ROCK;
			}
		
		else if (computer == 1){
			cpu_select = PAPER; 
			}
			
		else if (computer == 2){
			cpu_select = SCISSORS;
			}
		
		// In the even of a tie. [This creates an infinite loop if there is ever a tie for some reason.]
		
		/*
		if (cpu_select == select){
			tie = true;
			}
			
		while(tie == true){
			cout << endl << "It's a tie, Pick again please: ";
			cin >> user;
			
			if(cpu_select != select)
				tie = false;
			}	
		*/
			
		// Displays each respective selection.
		cout << endl << endl << "Your selection is " << select << endl;
		cout << "The computer selects " << cpu_select << endl;
		
		bool you_win = false;
		
		
		// Win conditions

		if ((select == ROCK && cpu_select == SCISSORS) || (select == PAPER && cpu_select == ROCK) || (select == SCISSORS && cpu_select == PAPER)){
				cout << endl << "You win." << endl;
				player_wins ++;
			}

		else if((cpu_select == ROCK && select == SCISSORS) || (cpu_select == PAPER && select == ROCK) || (cpu_select == SCISSORS && select == PAPER)){
			cout << endl << "The computer wins." << endl;
			computer_wins ++;
			}
			
		else if (cpu_select == select){
			num_ties++;
			}
		
		
		// Show the results of all the games.
		cout << endl << "You have won " << player_wins << " games." << endl;
		cout << "The computer has won " << computer_wins << " games." << endl;
		cout << endl << "There have been " << num_ties << " ties." << endl;
		
		// Ask if the player would like to play again.
		cout << endl << endl << "Would you like to play again?: ";
		cin >> play;
		
		// Exit the loop if the player chooses no.
		if (play == "n" || play == "N"){
			play_again = false;
			}
		else if (play == "y" || play == "Y"){
			num_games++;
			}
		
	}	
	
	// DEBUG: Shows number of games played.
	cout << endl << "You just played " << num_games << " games of Rock Paper Scissors." << endl << endl;
	
	
	
	system("pause");
	return 0;
	
	}
U can uncomment line 91 like this
1
2
3
4
5
if(cpu_select == select)
{
   cout << endl << "It's a tie, Pick again please: ";
   continue;
}


Look into ofstream for outputting the results to a file.
Thanks soranz! I'll try that tonight probably.

Here is the code for the new version that outputs to an external .txt file.

It's pretty much the same I think except for the bit close to the bottom for the actual output stuff.

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
// Rock Paper Scissors

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

int main() {
	enum choice {ROCK, PAPER, SCISSORS};
	
	choice select;
	char user;
	bool play_again;
	string play;
	int num_games = 0;
	int player_wins = 0;
	int computer_wins = 0;
	bool tie = false;
	int num_ties = 0;
	
	ofstream outfile;
	ofstream infile;
	
	// Ask the user if they would like to play.
	cout << "Would you like to play Rock, Paper, Scissors? (y or n): ";
	cin >> play;
	
	// If they answer yes, then play, otherwise no.
	if(play == "y" || play == "Y"){
		play_again = true;
		num_games++;
		
		//outfile.open("RockPaperScissors.txt");
		}
	
	// Start playing the actual game if they want to play.
	while(play_again != false){
		cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
		cin >> user;
		cout << endl;
		
		// Taking the input and applying it to constant in enum.
		if (user == 'r' || user == 'R'){
			select = ROCK;
			}
		
		else if (user == 'p' || user == 'P'){
			select = PAPER; 
			}
			
		else if (user == 's' || user == 'S'){
			select = SCISSORS;
			}
		
		else {
			cout << "Come on... That's not an option." << endl;
			cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
			cin >> user;
			}
		
		
		long  seed = time(NULL); // gets current time
		srand(seed);
		
		// Computer's random selection
		int computer = rand();
		
		//DEBUG: cout << computer << endl;
		
		// Computer's random selection
		computer = rand() % 3;
		
		//DEBUG: cout << computer << endl << endl;;
		//DEBUG: cout << "Here is a test: " << select << endl;
		
		
		// Applying the computer's selection to the appropriate value in enum.
		choice cpu_select;
		
		if (computer == 0){
			cpu_select = ROCK;
			}
		
		else if (computer == 1){
			cpu_select = PAPER; 
			}
			
		else if (computer == 2){
			cpu_select = SCISSORS;
			}
		
		// In the event of a tie. [This creates an infinite loop if there is ever a tie for some reason.]
		
		
		/*if (cpu_select == select){
			tie = true;
			}
			
		if(cpu_select == select){
			cout << endl << "It's a tie, Pick again please: ";
			cin >> user;
			}
		*/
			
		// Displays each respective selection.
		cout << endl << endl << "Your selection is " << select << endl;
		cout << "The computer selects " << cpu_select << endl;
		
		bool you_win = false;
		
		
		// Win conditions

		if ((select == ROCK && cpu_select == SCISSORS) || (select == PAPER && cpu_select == ROCK) || (select == SCISSORS && cpu_select == PAPER)){
				cout << endl << "You win." << endl;
				player_wins ++;
			}

		else if((cpu_select == ROCK && select == SCISSORS) || (cpu_select == PAPER && select == ROCK) || (cpu_select == SCISSORS && select == PAPER)){
			cout << endl << "The computer wins." << endl;
			computer_wins ++;
			}
			
		else if (cpu_select == select){
			num_ties++;
			}
		
		
		// Show the results of all the games.
		cout << endl << "You have won " << player_wins << " games." << endl;
		cout << "The computer has won " << computer_wins << " games." << endl;
		cout << endl << "There have been " << num_ties << " ties." << endl;
		
		
		// Ask if the player would like to play again.
		cout << endl << endl << "Would you like to play again?: ";
		cin >> play;
		
		// Exit the loop if the player chooses no.
		if (play == "n" || play == "N"){
			play_again = false;
			}
		else if (play == "y" || play == "Y"){
			num_games++;
			}
		
	outfile.open("RockPaperScissors.txt");
	outfile << "This is the number of games played: " << num_games << "\n";
	outfile << "This is the number of games the player has won: " << player_wins << "\n";
	outfile << "This is the number of games the computer has won: " << computer_wins << "\n";
	outfile << "This is the number of tie games: " << num_ties << "\n";
	outfile.close();
	}

		
		
	// DEBUG: Shows number of games played.
	cout << endl << "You just played " << num_games << " games of Rock Paper Scissors." << endl << endl;
	
	
	
	system("pause");
	return 0;
	
	}
And finally this was what I turned in. It works correctly.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

int main() {
	enum choice {ROCK, PAPER, SCISSORS};
	
	choice select;
	char user;
	bool play_again;
	string play;
	int num_games = 0;
	int player_wins = 0;
	int computer_wins = 0;
	bool tie = false;
	int num_ties = 0;
	
	ofstream outfile;
	ifstream infile;
	
	
		
		
	// Ask the user if they would like to play.
	cout << "Would you like to play Rock, Paper, Scissors? (y or n): ";
	cin >> play;
	
	// If they answer yes, then play, otherwise no.
	if(play == "y" || play == "Y"){
		play_again = true;
		num_games++;
		infile.open("RockPaperScissors.txt");
		infile >> player_wins >> computer_wins;
		infile.close();
		}
	
	// Start playing the actual game if they want to play.
	while(play_again != false){
		cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
		cin >> user;
		cout << endl;
		
		
		// Taking the input and applying it to constant in enum.
		if (user == 'r' || user == 'R'){
			select = ROCK;
			}
		
		else if (user == 'p' || user == 'P'){
			select = PAPER; 
			}
			
		else if (user == 's' || user == 'S'){
			select = SCISSORS;
			}
		
		else {
			cout << "Come on... That's not an option." << endl;
			cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
			cin >> user;
			}
		
		
		long  seed = time(NULL); // gets current time
		srand(seed);
		
		// Computer's random selection
		int computer = rand();
		
		//DEBUG: cout << computer << endl;
		
		// Computer's random selection
		computer = rand() % 3;
		
		//DEBUG: cout << computer << endl << endl;;
		//DEBUG: cout << "Here is a test: " << select << endl;
		
		// In the event of a tie, make both players make a new selection and re-evaluate them.
		
		while(computer == select){
			cout << endl << "It's a tie, Pick again please: ";
			cin >> user;
			
			// This is repeated... :/
			if (user == 'r' || user == 'R'){
				select = ROCK;
				}
		
			else if (user == 'p' || user == 'P'){
				select = PAPER; 
				}
			
			else if (user == 's' || user == 'S'){
				select = SCISSORS;
				}
		
			else {
				cout << "Come on... That's not an option." << endl;
				cout << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
				cin >> user;
				}
			
			computer = rand() % 3;
			}
		
		
		// Applying the computer's selection to the appropriate value in enum.
		choice cpu_select;
		
		if (computer == 0){
			cpu_select = ROCK;
			}
		
		else if (computer == 1){
			cpu_select = PAPER; 
			}
			
		else if (computer == 2){
			cpu_select = SCISSORS;
			}
		
		
			
		// Displays each respective selection.
		cout << endl << endl << "Your selection is " << select << endl;
		cout << "The computer selects " << cpu_select << endl;
				
		// Win conditions

		if ((select == ROCK && cpu_select == SCISSORS) || (select == PAPER && cpu_select == ROCK) || (select == SCISSORS && cpu_select == PAPER)){
				cout << endl << "You win." << endl;
				player_wins ++;
			}

		else if((cpu_select == ROCK && select == SCISSORS) || (cpu_select == PAPER && select == ROCK) || (cpu_select == SCISSORS && select == PAPER)){
			cout << endl << "The computer wins." << endl;
			computer_wins ++;
			}
		
		// Show the results of all the games.
		cout << "The player has won " << player_wins << " games." << endl;
		cout << "The computer has won " << computer_wins << " games." << endl;
		
		
		// Ask if the player would like to play again.
		cout << endl << endl << "Would you like to play again?: ";
		cin >> play;
		
		// Exit the loop if the player chooses no.
		if (play == "n" || play == "N"){
			play_again = false;
			}
		else if (play == "y" || play == "Y"){
			num_games++;
			}
		
	outfile.open("RockPaperScissors.txt");
	//outfile <<  num_games << "\n";
	outfile << player_wins << "\n";
	outfile << computer_wins << "\n";
	outfile << num_ties << "\n";
	outfile.close();
	}
	
	
	
	system("pause");
	return 0;
	
	}
Glad to hear it works XD
Bahaha... Yea. It's a little long though.
I think it would be cool and not that hard to code in where it says
1
2
your selection was 0
the computer selects 1


if instead of 0 or 1 it defined what was selected or at least say
1
2
your section lost
the computers selection won
@Endl3ss I totally agree, but I just ran out of time. I was just trying to make sure it all worked correctly and I forgot about it. It would have been super easy. I would fix it, but we are working on Hangman now, so I really don't have the time. I think I am about to make a topic for Hangman now for what it's worth.
Topic archived. No new replies allowed.