-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrock-paper-scissors.cpp
More file actions
executable file
·58 lines (45 loc) · 1.43 KB
/
rock-paper-scissors.cpp
File metadata and controls
executable file
·58 lines (45 loc) · 1.43 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
#include <iostream>
#include <unordered_map>
enum RPS : unsigned char {
UNDEFINED = 0b00000000,
ROCK = 0b01000001,
PAPER = 0b00010010,
SCISSORS = 0b00100100
};
std::unordered_map<char, RPS> inMap = {
{'r', RPS::ROCK}, {'R', RPS::ROCK},
{'p', RPS::PAPER}, {'P', RPS::PAPER},
{'s', RPS::SCISSORS}, {'S', RPS::SCISSORS}
};
std::unordered_map<RPS, const char*> outMap = {
{RPS::ROCK, "Rock"},
{RPS::PAPER, "Paper"},
{RPS::SCISSORS, "Scissors"}
};
const RPS randMap[] = {RPS::ROCK, RPS::PAPER, RPS::SCISSORS};
int main() {
RPS player;
RPS rand;
char in;
std::srand(time(NULL));
while (true) {
std::cout << "Rock Paper Scissors \n Shoot! [R/P/S] " << std::flush;
std::cin >> in;
player = inMap[in];
while (player == RPS::UNDEFINED) {
std::cout << "Invalid input!" << std::endl;
std::cin >> in;
player = inMap[in];
}
rand = randMap[std::rand() % 3];
std::cout << '\n' << "I choose " << outMap[rand] << std::endl;
if (player == rand) std::cout << "Draw! ";
else if ((player >> 4) & (rand & 0b00001111)) std::cout << "You win! ";
else std::cout << "I win! ";
std::cout << "Again? [Y/N]" << std::endl;
std::cin >> in;
std::cout << "\n";
if (in == 'y' or in == 'Y') continue;
break;
}
}