Skip to content

Commit 195f99b

Browse files
Initial Commit
0 parents  commit 195f99b

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed

RockPaperScissor.cpp

+242
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
#include <conio.h>
2+
#include <time.h>
3+
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <string>
7+
8+
using namespace std;
9+
10+
// Global variable as it will required at multiple fuctions.
11+
string options[3] = {"Rock", "Paper", "Scissor"};
12+
13+
void game1p();
14+
void game2p();
15+
int isP1Point(string, string);
16+
int p1Won(int, int);
17+
void endMsg(int, int, int);
18+
void endMsg2p(int, int, int, string, string);
19+
void playAgain();
20+
void p1p2();
21+
22+
int main() {
23+
// Game starts here
24+
p1p2();
25+
return 0;
26+
}
27+
28+
// p1p2() will check if the user wants to play 1 player mode or 2p mode, and will call the fuction required respectively
29+
void p1p2() {
30+
int noOfPlayers;
31+
cout << "Enter the number of players (1/2): ";
32+
cin >> noOfPlayers;
33+
cin.ignore(1, '\n');
34+
35+
if (noOfPlayers == 1) {
36+
game1p();
37+
} else if (noOfPlayers == 2) {
38+
game2p();
39+
} else {
40+
p1p2();
41+
}
42+
}
43+
44+
// Function for 1p mode
45+
void game1p() {
46+
int rounds;
47+
int userPoints = 0, cpuPoints = 0;
48+
49+
cout << "Enter no. of rounds: ";
50+
cin >> rounds;
51+
52+
for (int i = 0; i < rounds; i++) {
53+
// Generating random number between 0-2
54+
srand(time(0));
55+
int ranNum = rand() % 3;
56+
string cpu = options[ranNum]; // Use the random number to get random of Rock, Paper or Scissor
57+
58+
int userInp;
59+
cout << "\nCpu has choosen. Now, ";
60+
61+
// The loop will execute till the user choose and of the valid options
62+
bool wrongInp = 1;
63+
while (wrongInp) {
64+
cout << "choose one of the below:-\n'1' for Rock\n'2' for Paper\n'3' for Scissor\n:";
65+
cin >> userInp;
66+
67+
// If the input is not 0,1,
68+
if (userInp != 1 && userInp != 2 && userInp != 3) {
69+
cout << "Please enter valid input!!!\n";
70+
continue;
71+
}
72+
wrongInp = 0; // If the input is valid, `wrongInp = 0` will help to break the loop
73+
}
74+
75+
string user = options[userInp - 1];
76+
77+
int result = isP1Point(user, cpu);
78+
if (result == 0) {
79+
userPoints++;
80+
cpuPoints++;
81+
cout << "It's a draw!! CPU and you both chose " << cpu << ".\nYour points = " << userPoints << "\nCPU's points = " << cpuPoints;
82+
} else if (result == 1) {
83+
userPoints += 1;
84+
cout << "Congrats! You won this round. Your choice - " << user << " CPU Choice - " << cpu << "\nYour points = " << userPoints << "\nCPU's points = " << cpuPoints;
85+
} else {
86+
cpuPoints++;
87+
cout << "Oops! You should have chose something else. Your choice - " << user << " CPU Choice - " << cpu << "\nYour points = " << userPoints << "\nCPU's points = " << cpuPoints;
88+
}
89+
}
90+
int whoWon = p1Won(userPoints, cpuPoints);
91+
endMsg(whoWon, userPoints, cpuPoints);
92+
playAgain();
93+
}
94+
95+
// Fuction for 2p mode
96+
void game2p() {
97+
int p1Points = 0, p2Points = 0;
98+
string p1, p2;
99+
cout << "Player 1, please enter your name: ";
100+
getline(cin, p1);
101+
102+
cout << "Player 2, please enter your name: ";
103+
getline(cin, p2);
104+
105+
int rounds;
106+
cout << "Enter no. of Rounds: ";
107+
cin >> rounds;
108+
109+
for (int i = 0; i < rounds; i++) {
110+
char p1Inp, p2Inp; // We don't want to show what the player enters wo we will get an char using getch() and later store it in int variable.
111+
int p1Int, p2Int; // This is where we will store the input number in int.
112+
string p1opt, p2opt; // This will store the final option (what the player have choosen).
113+
bool wrognInp = 1;
114+
while (wrognInp) {
115+
cout << endl
116+
<< p1 << " type:-\n'1' for Rock\n'2' for Paper\n'3' for Scissor\n:";
117+
p1Inp = getch();
118+
p1Int = (p1Inp - '0') - 1; // {var} - '0' will convert p1Inp from char to int.
119+
p1opt = options[p1Int];
120+
121+
// We are checking for 0,1 and 2 as we already have subtracted 1 from it for getting an option from "options"
122+
if ((p1Int != 0) && (p1Int != 1) && (p1Int != 2)) {
123+
cout << "Please enter valid input!!!\n";
124+
continue;
125+
}
126+
wrognInp = 0;
127+
}
128+
wrognInp = 1;
129+
while (wrognInp) {
130+
cout << "\nNow " << p2 << " its your turn, type:-\n'1' for Rock\n'2' for Paper\n'3' for Scissor\n:";
131+
p2Inp = getch();
132+
p2Int = (p2Inp - '0') - 1; // {var} - '0' will convert p2Inp from char to int.
133+
p2opt = options[p2Int];
134+
if ((p2Int != 0) && (p2Int != 1) && (p2Int != 2)) {
135+
cout << "Please enter valid input!!!\n";
136+
continue;
137+
}
138+
wrognInp = 0;
139+
}
140+
141+
int whosePoint = isP1Point(p1opt, p2opt);
142+
if (whosePoint == 1) {
143+
p1Points++;
144+
cout << endl
145+
<< p1 << " chose " << p1opt << " and " << p2 << " chose " << p2opt << ", and it's " << p1 << "'s point.\nPoints:-\n"
146+
<< p1 << "'s points = " << p1Points << endl
147+
<< p2 << "'s points = " << p2Points << endl;
148+
} else if (whosePoint == 0) {
149+
p1Points++;
150+
p2Points++;
151+
cout << endl
152+
<< "Oh you both chose " << p1opt << ".\nPoints:-\n"
153+
<< p1 << "'s points = " << p1Points << endl
154+
<< p2 << "'s points = " << p2Points << endl;
155+
} else {
156+
p2Points++;
157+
cout << endl
158+
<< p2 << " beats " << p1 << " by choosing " << p2opt << " over " << p1opt << ".\nPoints:-\n"
159+
<< p1 << "'s points = " << p1Points << endl
160+
<< p2 << "'s points = " << p2Points << endl;
161+
}
162+
}
163+
int whoWon = p1Won(p1Points, p2Points);
164+
endMsg2p(whoWon, p1Points, p2Points, p1, p2);
165+
playAgain();
166+
}
167+
168+
// Function to check if its player1's point, player2/cpu's points or a draw
169+
int isP1Point(string p1, string p2) {
170+
if (p1 == "Rock" && p2 == "Scissor") {
171+
return 1;
172+
} else if (p1 == "Rock" && p2 == "Paper") {
173+
return -1;
174+
} else if (p1 == "Paper" && p2 == "Rock") {
175+
return 1;
176+
} else if (p1 == "Paper" && p2 == "Scissor") {
177+
return -1;
178+
} else if (p1 == "Scissor" && p2 == "Paper") {
179+
return 1;
180+
} else if (p1 == "Scissor" && p2 == "Rock") {
181+
return -1;
182+
}
183+
// Will return 0 if its a draw
184+
return 0;
185+
}
186+
187+
// Function to check if player1 won, player2/cpu won or its a draw
188+
int p1Won(int p1Points, int p2Points) {
189+
if (p1Points > p2Points) {
190+
return 1;
191+
} else if (p1Points < p2Points) {
192+
return -1;
193+
}
194+
return 0; // if tied
195+
}
196+
197+
// It willl print the final score and the winner
198+
void endMsg(int whoWon, int userPoints, int cpuPoints) {
199+
if (whoWon == 0) {
200+
cout << "\n\nGG boii, both ended up with " << userPoints << " points." << endl;
201+
} else if (whoWon == 1) {
202+
cout << "\n\nYou deserve this win man!\nFinal Points:-\nYour points = " << userPoints << "\nCPU's points = " << cpuPoints << endl;
203+
} else {
204+
cout << "\n\nBetter luck next time.\nYou ended with " << userPoints << " and I ended up with " << cpuPoints << " points." << endl;
205+
}
206+
}
207+
208+
// Same as "endMsg but for 2p mode"
209+
void endMsg2p(int whoWon, int p1Points, int p2Points, string p1, string p2) {
210+
if (whoWon == 0) {
211+
cout << "\n\nGG boii, both ended up with " << p1Points << " points." << endl;
212+
} else if (whoWon == 1) {
213+
cout << endl
214+
<< endl
215+
<< p1 << ", you deserve this win man!\nFinal Points:-\n"
216+
<< p1 << "'s points = " << p1Points << endl
217+
<< p2 << "'s points = " << p2Points << endl;
218+
} else {
219+
cout << endl
220+
<< endl
221+
<< p1 << ", better luck next time.\nYou ended with " << p1Points << " and " << p2 << " ended up with " << p2Points << " points." << endl;
222+
}
223+
}
224+
225+
// Function to check if the user wants to play again
226+
void playAgain() {
227+
string again;
228+
cout << "\nDo you want to play again? (y/n): ";
229+
cin >> again;
230+
231+
transform(again.begin(), again.end(), again.begin(), ::tolower); // It will transform the user input to lower case
232+
233+
if (again == "y" || again == "yes") {
234+
p1p2();
235+
} else if (again == "n" || again == "no") {
236+
cout << "\n\nThank you for playing the game. See you again.\n(Press any key to exit)";
237+
getch();
238+
} else {
239+
cout << "!!!Invalid Input!!!";
240+
playAgain();
241+
}
242+
}

0 commit comments

Comments
 (0)