forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhac-the-mole.cpp
96 lines (80 loc) · 2.63 KB
/
whac-the-mole.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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <thread>
using namespace std;
// Function to generate a random number between min and max (inclusive)
int get_random_number(int min, int max)
{
static bool first = true;
if (first)
{
srand(time(NULL));
first = false;
}
return min + rand() % ((max + 1) - min);
}
int main()
{
// Initialize game variables
const int NUM_HOLES = 9;
const int NUM_ROUNDS = 3;
int score = 0;
// Print welcome message
cout << "Welcome to CLI Whac-a-Mole!" << endl;
// Play game for NUM_ROUNDS rounds
for (int round = 1; round <= NUM_ROUNDS; round++)
{
cout << "Round " << round << endl;
// Initialize round variables
int current_score = 0;
bool holes[NUM_HOLES] = {false};
// Play round for 30 seconds
auto start_time = std::chrono::system_clock::now();
while (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_time).count() < 30)
{
// Choose a random hole
int hole_number = get_random_number(0, NUM_HOLES - 1);
// If hole is not already active, activate it
if (!holes[hole_number])
{
holes[hole_number] = true;
// Print board with active hole
cout << "+---+---+---+" << endl;
for (int i = 0; i < NUM_HOLES; i++)
{
if (holes[i])
{
cout << "| X ";
}
else
{
cout << "| ";
}
}
cout << "|" << endl;
cout << "+---+---+---+" << endl;
// Wait for player to hit ENTER
cout << "Hit ENTER when ready to whac!" << endl;
cin.ignore();
// If player hit ENTER while the hole was active, add to score
if (holes[hole_number])
{
current_score++;
holes[hole_number] = false;
cout << "WHAC!" << endl;
}
// Wait for 1 second before clearing board
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
// Print round score
cout << "Round " << round << " score: " << current_score << endl;
// Add round score to total score
score += current_score;
}
// Print total score
cout << "Total score: " << score << endl;
return 0;
}