-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (57 loc) · 2.3 KB
/
Copy pathmain.cpp
File metadata and controls
70 lines (57 loc) · 2.3 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
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <random>
#include <unordered_map>
int main()
{
bool playing = true;
std::string userMoveStr, playAgain;
int playerWins = 0, compWins = 0, compMove, userMove, result;
typedef std::unordered_map<int, std::string> moveMap;
moveMap moves = { {1, "Rock"}, {2, "Paper"}, {3, "Scissors"} };
// Initialize random number generator
std::random_device rd; // obtain a random number from hardware
std::mt19937 eng(rd()); // seed the generator
std::uniform_int_distribution<> distr(1, 3); // define the range
std::cout << "Welcome!" << std::endl;
while(playing)
{
std::cout << "Enter 1 to play Rock, 2 to play Paper, and 3 to play Scissors!: ";
getline(std::cin, userMoveStr);
while(!(userMoveStr == "1" || userMoveStr == "2" || userMoveStr == "3"))
{
std::cout << "Unknown command! Please try that again..." << std::endl;
std::cout << "Enter 1 to play Rock, 2 to play Paper, and 3 to play Scissors!: ";
getline(std::cin, userMoveStr);
}
// Convert the user move from a string to an integer
userMove = std::stoi(userMoveStr);
// Create random integer in range [1,3] to simulate the computer selecting a move
compMove = distr(eng);
result = userMove - compMove;
if(result == 0)
std::cout << "Tie game!" << std::endl;
else if(result == 1 || result == -2)
{
std::cout << "Congratulations, you won!" << std::endl;
playerWins++;
} else
{
std::cout << "Sorry, you lost!" << std::endl;
compWins++;
}
std::cout << "Your move: " << moves[userMove] << " // Computer's move: " << moves[compMove] << std::endl;
std::cout << "(Player: " << playerWins << " | Computer: " << compWins << ")" << std::endl;
std::cout << "Play again? [y/n]: ";
getline(std::cin, playAgain);
while(!(playAgain == "y" || playAgain == "n"))
{
std::cout << "Unknown command! Please try that again..." << std::endl;
std::cout << "Play again? [y/n]: ";
getline(std::cin, playAgain);
}
if(playAgain == "n")
playing = false;
}
return 0;
}