-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
144 lines (130 loc) · 2.68 KB
/
main.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
#include <iostream>
#include <ctime>
char getUserChoice();
char getComputerChoice();
void showTheirChoices(char choice);
char findWinner(char player, char computer);
using namespace std;
int main()
{
char player;
char computer;
int playerWins = 0;
int computerWins = 0;
int playTimes = 0;
while (playerWins < 3 && computerWins < 3)
{
player = getUserChoice();
cout << "Player's choice : ";
showTheirChoices(player);
cout << endl;
computer = getComputerChoice();
cout << "Computer's choice : ";
showTheirChoices(computer);
char result = findWinner(player, computer);
if (result == 'p')
{
cout << "\nPlayer win this round!!!\n"
<< endl;
playerWins++;
}
else if (result == 'c')
{
cout << "\nComputer win this round!!!\n"
<< endl;
computerWins++;
}
else
{
cout << "\nIt'a tie";
}
if (playerWins == 3 && computerWins == 1)
{
cout << "Player beat computer with 3-1";
}
else if (playerWins == 3 && computerWins == 0)
{
cout << "Player beat computer with 3-0";
}
else if (playerWins == 3 && computerWins == 2)
{
cout << "Player beat computer with 3-2";
}
else if (computerWins == 3 && playerWins == 1)
{
cout << "Computer beat you with 3-1";
}
else if (computerWins == 3 && playerWins == 0)
{
cout << "Computer beat you with 3-0";
}
else if (computerWins == 3 && playerWins == 2)
{
cout << "Computer beat you with 3-2";
}
playTimes++;
}
}
char getUserChoice()
{
char choice;
do
{
cout << "\nWelcome to the Rock, Paper and Scissor game!!!\n";
cout << "**********************************************\n";
cout << "Choose one (Rock or r , Paper for p and Scissor for s) : ";
cin >> choice;
} while (choice != 'r' && choice != 'p' && choice != 's');
return choice;
}
char getComputerChoice()
{
srand(time(0));
int randT = rand() % 3 + 1;
switch (randT)
{
case 1:
return 'r';
break;
case 2:
return 'p';
break;
case 3:
return 's';
break;
}
}
void showTheirChoices(char choice)
{
switch (choice)
{
case 'r':
cout << "Rock!";
break;
case 'p':
cout << "Paper!";
break;
case 's':
cout << "Scissor!";
break;
}
}
char findWinner(char player, char computer)
{
if (player == 'r' && computer == 's' ||
player == 'p' && computer == 'r' ||
player == 's' && computer == 'p')
{
return 'p';
}
else if (computer == 'r' && player == 's' ||
computer == 'p' && player == 'r' ||
computer == 's' && player == 'p')
{
return 'c';
}
else
{
return 't';
}
}