-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
150 lines (114 loc) · 4.27 KB
/
Player.cpp
File metadata and controls
150 lines (114 loc) · 4.27 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "Player.h"
#include <string>
std::vector<std::string> Player::usedColors;
std::vector<char> Player::usedButtons;
Player::Player(Console * terminal, Led * led, uint8_t numOfPlayer) :
score(0), color("none"), keyBoardButton(' '), led(led) {
std::string message = "\nChoose color for player " + std::to_string(numOfPlayer) + "\n";
terminal->write(message.c_str());
char colorOfPlayer;
do {
colorOfPlayer = terminal->customPlayerInit();
colorOfPlayer = std::toupper(colorOfPlayer);
switch (colorOfPlayer) {
case 'R':
if (std::find(usedColors.begin(), usedColors.end(), "RED") == usedColors.end()) {
this->color = "RED";
usedColors.push_back("RED");
led->setBrightness(1, 0, 0);
} else {
this->color = "none";
terminal->write("Color RED is already taken, choose another color!\n");
}
break;
case 'G':
if (std::find(usedColors.begin(), usedColors.end(), "GREEN") == usedColors.end()) {
this->color = "GREEN";
usedColors.push_back("GREEN");
led->setBrightness(0, 1, 0);
} else {
this->color = "none";
terminal->write("Color GREEN is already taken, choose another color!\n");
}
break;
case 'B':
if (std::find(usedColors.begin(), usedColors.end(), "BLUE") == usedColors.end()) {
this->color = "BLUE";
usedColors.push_back("BLUE");
led->setBrightness(0, 0, 1);
} else {
this->color = "none";
terminal->write("Color BLUE is already taken, choose another color!\n");
}
break;
default:
this->color = "none";
terminal->write("BAD INPUT, try again!\n");
}
} while (this->color == "none");
std::string info = "Player " + std::to_string(numOfPlayer) + " chooses color " + this->color + "\n";
terminal->write(info.c_str());
terminal->write("Choose your keyboard button\n");
do {
this->keyBoardButton = terminal->customPlayerInit();
if (std::find(usedButtons.begin(), usedButtons.end(), this->keyBoardButton) == usedButtons.end()) {
usedButtons.push_back(this->keyBoardButton);
break;
} else {
terminal->write("Button is already taken, choose another button!\n");
}
} while (true);
info = "Player " + std::to_string(numOfPlayer) + " chooses " + this->keyBoardButton + " button!\n";
terminal->write(info.c_str());
}
uint8_t Player::getScore(void) const {
return this->score;
}
void Player::setScore(uint8_t &score) {
this->score = score;
}
std::string Player::getColor(void) const {
return this->color;
}
void Player::setColor(std::string &color) {
this->color = color;
}
char Player::getKeyBoardButton(void) {
return this->keyBoardButton;
}
void Player::fromStringToLed(const std::string& color) {
if (color == "RED") {
this->led->setBrightness(1, 0, 0);
ThisThread::sleep_for(2s);
this->led->off();
} else if (color == "GREEN") {
this->led->setBrightness(0, 1, 0);
ThisThread::sleep_for(2s);
this->led->off();
} else {
this->led->setBrightness(0, 0, 1);
ThisThread::sleep_for(2s);
this->led->off();
}
}
void Player::playerFun(Console* terminal, struct Data* data) {
std::string info;
while (!data->gameWon) {
data->mutex.lock();
char input = data->lastInput;
data->mutex.unlock();
if (input != 0 && input == this->getKeyBoardButton()) {
data->mutex.lock();
if (!data->gameWon) {
info = "Player with " + this->color + " won!\n";
this->score++;
terminal->write(info.c_str());
this->fromStringToLed(this->color);
data->gameWon = true;
}
data->mutex.unlock();
break;
}
ThisThread::sleep_for(10ms);
}
}