-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathRock_Paper_Scissors_Game.cpp
147 lines (134 loc) · 4.44 KB
/
Rock_Paper_Scissors_Game.cpp
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
string userInputChecker();
int* gameLogic(string computerChoice, string userChoice, int userScore, int computerScore);
int main() {
srand(time(NULL));
string choices[] = { "rock", "paper", "scissors" };
cout << "Welcome to the game!" << endl;
cout << "Enter:" << endl;
cout << "r for rock" << endl;
cout << "p for paper" << endl;
cout << "s for scissors" << endl;
cout << "Enter your name: ";
string playerName;
getline(cin, playerName);
int userScoreTotal = 0;
int computerScoreTotal = 0;
int i = 1;
while (i == 1) {
string userChoice = userInputChecker();
while (userChoice == "") {
userChoice = userInputChecker();
}
string computerChoice = choices[rand() % 3];
cout << "Computer chooses: " << computerChoice << endl;
int* scores = gameLogic(computerChoice, userChoice, userScoreTotal, computerScoreTotal);
i = scores[0];
userScoreTotal = scores[1];
computerScoreTotal = scores[2];
if (i == 0) {
cout << "Scores for this match are as follows:" << endl;
cout << playerName << "'s score: " << userScoreTotal << endl;
cout << "Computer's score: " << computerScoreTotal << endl;
cout << "Thank you for playing the game." << endl;
cout << "Have a nice day!" << endl;
}
else if (i != 0 && i != 1) {
cout << "Invalid Input!" << endl;
cout << "Please enter 1 to continue or 0 to leave the game: ";
cin >> i;
cin.ignore();
}
}
return 0;
}
string userInputChecker() {
cout << "Enter your choice: ";
string userChoice;
getline(cin, userChoice);
if (userChoice == "r" || userChoice == "p" || userChoice == "s") {
return userChoice;
}
else {
cout << "Wrong Input!!" << endl;
return "";
}
}
int* gameLogic(string computerChoice, string userChoice, int userScore, int computerScore) {
static int scores[3];
if (computerChoice == "rock" && userChoice == "p") {
cout << "Player Wins" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
userScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
else if (computerChoice == "rock" && userChoice == "s") {
cout << "Computer Wins" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
computerScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
else if (computerChoice == "paper" && userChoice == "r") {
cout << "Computer Wins" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
computerScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
else if (computerChoice == "paper" && userChoice == "s") {
cout << "Player Wins" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
userScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
else if (computerChoice == "scissors" && userChoice == "r") {
cout << "Player Wins" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
userScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
else if (computerChoice == "scissors" && userChoice == "p") {
cout << "Computer Wins" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
computerScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
else if (computerChoice == userChoice) {
cout << "Draw" << endl;
cout << "Enter 1 to continue and 0 to leave the game" << endl;
userScore += 1;
computerScore += 1;
cin >> scores[0];
cin.ignore();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
return scores;
}