-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.cpp
More file actions
126 lines (103 loc) · 2.89 KB
/
Console.cpp
File metadata and controls
126 lines (103 loc) · 2.89 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "Console.h"
#include <cstdint>
#include <cstdlib>
#include <string>
Console::Console(PinName tx, PinName rx, int baudrate)
: uart(tx, rx, baudrate) {
uart.baud(baudrate);
}
void Console::write(const char* message) {
uart.write(message, strlen(message));
}
int Console::read(char* buffer, int length) {
if (uart.readable()) {
return uart.read(buffer, length);
}
return 0;
}
char Console::readChar(void) {
char buffer;
if (uart.readable()) {
uart.read(&buffer, 1);
return buffer;
}
return 0;
}
char Console::customPlayerInit() {
char ch;
while (true) {
if (this->read(&ch, 1) > 0) {
if (ch == '\n' || ch == '\r') {
continue;
}
return ch;
}
}
}
void Console::clear() {
this->write("\e[1;1H\e[2J");
}
uint8_t Console::setNumOfPlayers(void) {
std::string info;
this->write("Chose number of players(min 2, max 3)\n");
char charNumOfPlayers;
uint8_t numOfPlayers;
do {
charNumOfPlayers = this->customPlayerInit();
switch (charNumOfPlayers) {
case '2':
numOfPlayers = 2;
break;
case '3':
numOfPlayers = 3;
break;
default:
charNumOfPlayers = '?';
}
} while (charNumOfPlayers == '?');
info = "Game is initialized for " + std::to_string(numOfPlayers) + " players!\n";
this->write(info.c_str());
return numOfPlayers;
}
uint8_t Console::setNumOfRounds(void) {
this->printName();
this->write("Set number of rounds !\n");
uint8_t rounds = 0;
char buffer = '0';
while (true) {
if (uart.readable()) {
uart.read(&buffer, 1);
rounds = buffer - '0';
if (rounds > 0) {
std::string info = "Num of rounds: " + std::to_string(rounds) + "\n";
this->write(info.c_str());
return rounds;
} else {
this->write("Invalid input. Please enter a number greater than 0.\n");
}
}
}
}
void Console::printName(void) {
std::string info = "~*~*~*~*~*~*~*~*~*~|FAST CLICKER|~*~*~*~*~*~*~*~*~*~\n";
this->write(info.c_str());
}
void Console::restartGame(void) {
char input = ' ';
this->write("Do you want to restart the game? (Y/N): ");
do {
input = this->readChar();
input = std::toupper(input);
if (input != 'Y' && input != 'N' && input != '\0') {
this->write("Invalid input. Please enter 'Y' or 'N'.\n");
ThisThread::sleep_for(200ms);
}
} while (input != 'Y' && input != 'N');
if (input == 'Y') {
this->write("Game will restart.\n");
NVIC_SystemReset();
} else {
this->write("Game will not restart.\n");
exit(0);
}
}