forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock-paper-scissor.cpp
59 lines (50 loc) · 1.3 KB
/
rock-paper-scissor.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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
enum Choice {
ROCK,
PAPER,
SCISSORS
};
int main() {
srand(time(NULL));
while (true) {
int computer = rand() % 3;
int player;
cout << "Enter your choice (0 for rock, 1 for paper, 2 for scissors): ";
cin >> player;
if (player < 0 || player > 2) {
cout << "Invalid choice. Please try again.\n";
continue;
}
cout << "Computer chose ";
switch (computer) {
case ROCK:
cout << "rock.\n";
break;
case PAPER:
cout << "paper.\n";
break;
case SCISSORS:
cout << "scissors.\n";
break;
}
if (player == computer) {
cout << "Tie!\n";
} else if ((player == ROCK && computer == SCISSORS) ||
(player == PAPER && computer == ROCK) ||
(player == SCISSORS && computer == PAPER)) {
cout << "You win!\n";
} else {
cout << "Computer wins!\n";
}
char playAgain;
cout << "Play again? (y/n): ";
cin >> playAgain;
if (playAgain != 'y') {
break;
}
}
return 0;
}