rock paper scissors with loop

How can I easily display what the computer chose and what the user chose in words? i.e. rock paper or scissors. I am thinking an if-else statement but is there anyway to just assign the numbers to the strings inside that function?

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
 #include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstdlib>

using namespace std;

void display_game_details();
void play_game();
void display_choices(int,int);
string find_winner(int,int);
int get_comp_choice();
int get_user_choice();



int main()
{

	display_game_details();

	play_game();

	system("pause");

	return 0;
}

void display_game_details()
{

	cout<<"Hello, this is a rock paper scissors game"<<endl;
	cout<<"The rules are simple:"<<endl;
	cout<<"Rock beats scissors."<<endl;
	cout<<"Scissors beats paper."<<endl;
	cout<<"Paper beats rock"<<endl;
	cout<<"In result of a tie, you will play again until there is a winner."<<endl;


}

int get_comp_choice()
{



	int comp;

	unsigned seed_time = time(0);
	srand(seed_time);

	comp = 1 + rand()%3;


	return comp;

}

void play_game()
{
	int comp_choice;
	int user_choice;
	string game_winner;



	comp_choice = get_comp_choice();
	user_choice = get_user_choice();

	display_choices(comp_choice, user_choice);

	game_winner = find_winner(comp_choice, user_choice);

	if(game_winner != "")
		cout<<"The winner is: "<<game_winner;

}

int get_user_choice()
{



	int user;

	do{

	cout<<"1. Rock \n2. Paper \n3. Scissors \n0. Quit"<<endl;
	cout<<"Please enter your choice: ";
	cin>>user;

	if(user == 0)
		{
			cout<<"Thanks for playing! Goodbye"<<endl;
			exit(0);
		}


	while(user != 1 && user != 2 && user != 3)
	{
		cout<<"Please enter a 1 a 2 a 3 to play or 0 to quit"<<endl;
		break;

	}

	}while(user != 1 && user != 2 && user != 3);



	return user;




}


string find_winner(int comp, int user)
{
	string computer = "computer";
	string user_1 = "user";
	string winner;

	if (comp == 1 && user == 3)
		winner = computer;
	else if(comp == 1 && user == 2)
		winner = user_1;
	else if(comp == 2 && user == 1)
		winner = computer;
	else if(comp == 2 && user == 3)
		winner = user_1;
	else if(comp == 3 && user == 2)
		winner = computer;
	else if (comp == 3 && user == 1)
		winner = user_1;

	else
	{
		cout<<"You tied!"<<endl;
		play_game();


	}

		return winner;

}


void display_choices(int comp, int user)
{


	cout<<"computer choice: "<<comp<<endl;
	cout<<"user choice: "<<user<<endl;

}

Last edited on
Instead of if-else statements you can use a lookup table.
Maybe you want to have a look at my demo.
http://hobbyprogrammer.byethost13.com/downloads/cpp/rock_paper_Scissor/
Topic archived. No new replies allowed.