-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp-2.cpp
More file actions
62 lines (58 loc) · 1.9 KB
/
exp-2.cpp
File metadata and controls
62 lines (58 loc) · 1.9 KB
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
#include<iostream>
#include<string>
#include<random>
using namespace std;
class RockPaperScissors {
private:
int userscore=0;
int compscore=0;
int getComputerChoice(){
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(1, 3);
return dist(gen);
}
string choiceToString(int choice){
if(choice==1) return "Rock";
else if(choice==2) return "Paper";
else return "Scissors";
}
int decideWinner(int userChoice, int compChoice)
if(userChoice==compChoice) return 0;
else if((userChoice==1 && compChoice==3) ||
(userChoice==2 && compChoice==1) ||
(userChoice==3 && compChoice==2)) return 1;
else return -1;
}
public:
void playGame(){
int rounds;
cout<<"ENter number of rounds to play: ";
cin>>rounds;
for(int i=0;i<rounds;i++){
cout<<"\n----Round " << i+1 << "----";
cout<<"\nEnter your choice (1-Rock, 2-Paper, 3-Scissors): ";
int userChoice;
cin>>userChoice;
int compChoice = getComputerChoice();
cout<<"Computer chose: " << choiceToString(compChoice) << endl;
int result = decideWinner(userChoice, compChoice);
if(result==1){
cout<<"You win this round!" << endl;
userscore++;
}
else if(result==-1){
cout<<"Computer wins this round!" << endl;
compscore++;
}
else{
cout<<"This round is a tie!" << endl;
}
}
}
};
int main() {
RockPaperScissors game;
game.playGame();
return 0;
}