-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
107 lines (83 loc) · 2.92 KB
/
main.cpp
File metadata and controls
107 lines (83 loc) · 2.92 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
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
#include "Led.h"
#include "Button.h"
#include "Console.h"
#include "Player.h"
#include "Random.h"
int main() {
Random random;
Console* console = new Console(USBTX, USBRX);
console->clear();
Button button;
uint8_t rounds = console->setNumOfRounds();
uint8_t numOfPlayers = console->setNumOfPlayers();
Data data(console, numOfPlayers);
std::vector<std::unique_ptr<Thread>> playerThreads;
std::string info;
for (uint8_t round = 1; round <= rounds; ++round) {
info = "Starting Round " + std::to_string(round) + "!\n";
console->write(info.c_str());
delete console;
console = nullptr;
uint16_t randomDelay = random.randomNumberOnInterval(1, 10);
uint16_t elapsedTime = 0;
while (elapsedTime < randomDelay * 1000) {
for (auto& led : data.leds) {
led->trickyLeds(&random);
}
if (button.isGamePaused()) {
console = new Console(USBTX, USBRX);
console->write("Game is paused. Press the button to continue...\n");
while (button.isGamePaused()) {
ThisThread::sleep_for(100ms);
}
console->write("Game resumed.\n");
delete console;
console = nullptr;
}
ThisThread::sleep_for(100ms);
elapsedTime += 100;
}
console = new Console(USBTX, USBRX);
console->write(" \nGAME START!\n");
for (auto& led : data.leds) {
led->off();
}
for (size_t i = 0; i < data.players.size(); i++) {
playerThreads.emplace_back(std::make_unique<Thread>());
playerThreads.back()->start([player = &data.players[i], console, &data]() {
player->playerFun(console, &data);
});
}
while (!data.gameWon) {
if (button.isGamePaused()) {
console->write("Game is paused. Press the button to continue...\n");
while (button.isGamePaused()) {
ThisThread::sleep_for(100ms);
}
console->write("Game resumed.\n");
}
char input = console->readChar();
if (input != 0) {
data.mutex.lock();
data.lastInput = input;
data.mutex.unlock();
}
ThisThread::sleep_for(10ms);
}
for (auto& thread : playerThreads) {
thread->join();
}
info = "Round " + std::to_string(round) + " Over!\n";
console->write(info.c_str());
data.gameWon = false;
data.lastInput = 0;
playerThreads.clear();
for (auto& led : data.leds) {
led->off();
}
}
console->write("Game Over! All Rounds Completed.\n");
data.displayScoreboard(console);
console->restartGame();
delete console;
}