Skip to content

Commit 071a519

Browse files
authored
Final version - added bot difficulties
1 parent 5ee93d2 commit 071a519

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

Window.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ bool Window::idle_callback(int player1_att, int player2_att, int player1_med, in
192192

193193
// Win conditions
194194
if (env->position_x > x_bounds_p2 || env->position_y > y_bounds_p2) {
195-
std::cout << "\nPLAYER 1 (" + p1_name + ") WINS!" << std::endl;
195+
std::cout << "\nYOU WIN!" << std::endl;
196196
return true;
197197
}
198198
else if (env->position_x < x_bounds_p1 || env->position_y < y_bounds_p1) {
199-
std::cout << "\nPLAYER 2 (" + p2_name + ") WINS!" << std::endl;
199+
std::cout << "\nTHE BOT WINS!" << std::endl;
200200
return true;
201201
}
202202
else {

main.cpp

+106-22
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int main(void)
8787

8888
// ThinkGear variables
8989
char *comPortName = NULL;
90+
char *comPortName2 = NULL;
9091
int dllVersion = 0;
9192
int connectionId = 0;
9293
int connectionId2 = 0;
@@ -115,14 +116,15 @@ int main(void)
115116
}
116117

117118
/* Get a connection ID handle to ThinkGear for player 2 */
119+
/**
118120
connectionId2 = TG_GetNewConnectionId();
119121
if (connectionId2 < 0) {
120122
fprintf(stderr, "ERROR: TG_GetNewConnectionId() returned %d for PLAYER 2.\n",
121123
connectionId);
122124
fprintf(stderr, "Press ENTER to continue...\n", connectionId2);
123125
std::cin.ignore();
124126
//exit(EXIT_FAILURE);
125-
}
127+
}**/
126128

127129

128130
/* Attempt to connect the connection ID handle to serial port "COM5" */
@@ -138,38 +140,67 @@ int main(void)
138140
TG_BAUD_57600,
139141
TG_STREAM_PACKETS);
140142
if (errCode < 0) {
141-
fprintf(stderr, "ERROR: TG_Connect() returned %d for PLAYER 1.\n", errCode);
143+
fprintf(stderr, "ERROR: TG_Connect() returned %d.\n", errCode);
144+
std::cout << "WARNING: The headset has not been detected!" << std::endl;
142145
fprintf(stderr, "Press ENTER to continue...\n", errCode);
143146
std::cin.ignore();
144147
//exit(EXIT_FAILURE);
145148
}
146149

147150
/* PLAYER 2 */
148151
/**
149-
comPortName = "\\\\.\\COM4";
152+
comPortName2 = "\\\\.\\COM5";
150153
errCode = TG_Connect(connectionId2,
151-
comPortName,
152-
TG_BAUD_57600,
154+
comPortName2,
155+
TG_BAUD_9600,
153156
TG_STREAM_PACKETS);
154157
if (errCode < 0) {
155158
fprintf(stderr, "ERROR: TG_Connect() returned %d for PLAYER 2.\n", errCode);
156159
fprintf(stderr, "Press ENTER to continue...\n", errCode);
157160
std::cin.ignore();
158161
//exit(EXIT_FAILURE);
159-
}
160-
**/
162+
}**/
161163

162164
// Get names
163165
std::string p1_name;
164166
std::string p2_name;
165-
std::cout << "\nEnter name for PLAYER 1: ";
166-
std::getline(std::cin, p1_name);
167-
/**
168-
std::cout << "Enter name for PLAYER 2: ";
169-
std::getline(std::cin, p2_name);
170-
**/
167+
//std::cout << "\nEnter name for PLAYER 1: ";
168+
//std::getline(std::cin, p1_name);
169+
170+
//std::cout << "Enter name for PLAYER 2: ";
171+
//std::getline(std::cin, p2_name);
171172
p2_name = "BOT";
172173

174+
std::string difficulty;
175+
bool valid_input = false;
176+
while (!valid_input) {
177+
std::cout << "\nPlease choose a difficulty: (e)asy, (m)edium, (h)ard, (v)ery hard, (i)mpossible: ";
178+
std::getline(std::cin, difficulty);
179+
if (difficulty == "e" || difficulty == "m" || difficulty == "h" || difficulty == "v" || difficulty == "i") {
180+
valid_input = true;
181+
}
182+
else {
183+
std::cout << "Invalid input! Try again" << std::endl;
184+
}
185+
}
186+
187+
std::string full_difficulty;
188+
if (difficulty == "e") {
189+
full_difficulty = "Easy";
190+
}
191+
else if (difficulty == "m") {
192+
full_difficulty = "Medium";
193+
}
194+
else if (difficulty == "h") {
195+
full_difficulty = "Hard";
196+
}
197+
else if (difficulty == "v") {
198+
full_difficulty = "Very Hard";
199+
}
200+
else if (difficulty == "i") {
201+
full_difficulty = "Impossible";
202+
}
203+
173204
// Set timers
174205
secondsToRun = 3;
175206
int sec_before_start = 10;
@@ -210,17 +241,43 @@ int main(void)
210241
int player1_meditation = (int)TG_GetValue(connectionId, TG_DATA_MEDITATION);
211242
//int player2_attention = (int)TG_GetValue(connectionId2, TG_DATA_ATTENTION);
212243
//int player2_meditation = (int)TG_GetValue(connectionId2, TG_DATA_MEDITATION);
213-
int player2_attention = 0 + (rand() % (int)(100 - 0 + 1));
214-
int player2_meditation = 0 + (rand() % (int)(100 - 0 + 1));
215-
244+
int player2_attention;
245+
int player2_meditation;
246+
if (difficulty == "e") {
247+
player2_attention = 0 + (rand() % (int)(35 - 0 + 1));
248+
player2_meditation = 0 + (rand() % (int)(35 - 0 + 1));
249+
}
250+
else if (difficulty == "m") {
251+
player2_attention = 0 + (rand() % (int)(100 - 0 + 1));
252+
player2_meditation = 0 + (rand() % (int)(100 - 0 + 1));
253+
}
254+
else if (difficulty == "h") {
255+
player2_attention = 50 + (rand() % (int)(100 - 50 + 1));
256+
player2_meditation = 50 + (rand() % (int)(100 - 50 + 1));
257+
}
258+
else if (difficulty == "v") {
259+
player2_attention = 75 + (rand() % (int)(100 - 75 + 1));
260+
player2_meditation = 75 + (rand() % (int)(100 - 75 + 1));
261+
}
262+
else if (difficulty == "i") {
263+
player2_attention = 90 + (rand() % (int)(100 - 90 + 1));
264+
player2_meditation = 90 + (rand() % (int)(100 - 90 + 1));
265+
}
216266

217267
// Print player 1 measures
218-
fprintf(stdout, "%s: P1 (%s)\t ATT: %d\n", currTimeStr, p1_name, player1_attention);
219-
fprintf(stdout, "%s: P1 (%s)\t MED: %d\n", currTimeStr, p1_name, player1_meditation);
268+
std::string tabs;
269+
if (difficulty == "i") {
270+
tabs = "\t\t\t";
271+
}
272+
else {
273+
tabs = "\t\t";
274+
}
275+
fprintf(stdout, "%sPLAYER%s ATT: %d\n", currTimeStr, tabs, player1_attention);
276+
fprintf(stdout, "PLAYER%s MED: %d\n", tabs, player1_meditation);
220277

221278
// Print player 2 measures
222-
fprintf(stdout, "%s: P2 (%s)\t ATT: %d\n", currTimeStr, p2_name, player2_attention);
223-
fprintf(stdout, "%s: P2 (%s)\t MED: %d\n\n", currTimeStr, p2_name, player2_meditation);
279+
fprintf(stdout, "%s (%s)\t ATT: %d\n", p2_name, full_difficulty, player2_attention);
280+
fprintf(stdout, "%s (%s)\t MED: %d\n\n", p2_name, full_difficulty, player2_meditation);
224281
fflush(stdout);
225282

226283
// Idle callback. Updating objects, etc. can be done here.
@@ -230,8 +287,35 @@ int main(void)
230287
// Ball reaches past boundaries
231288
if (game_over) {
232289
std::cout << "GAME OVER, BALL REACHED PAST BOUNDARIES." << std::endl;
233-
std::cout << "\nPress ENTER to reset!" << std::endl;
234-
std::cout << "After pressing ENTER, game will restart immediately." << std::endl;
290+
291+
bool valid_input = false;
292+
while (!valid_input) {
293+
std::cout << "\nPlease choose a difficulty: (e)asy, (m)edium, (h)ard, (v)ery hard, (i)mpossible: ";
294+
std::getline(std::cin, difficulty);
295+
if (difficulty == "e" || difficulty == "m" || difficulty == "h" || difficulty == "v" || difficulty == "i") {
296+
valid_input = true;
297+
}
298+
else {
299+
std::cout << "Invalid input! Try again." << std::endl;
300+
}
301+
}
302+
303+
if (difficulty == "e") {
304+
full_difficulty = "Easy";
305+
}
306+
else if (difficulty == "m") {
307+
full_difficulty = "Medium";
308+
}
309+
else if (difficulty == "h") {
310+
full_difficulty = "Hard";
311+
}
312+
else if (difficulty == "v") {
313+
full_difficulty = "Very Hard";
314+
}
315+
else if (difficulty == "i") {
316+
full_difficulty = "Impossible";
317+
}
318+
std::cout << "\nPress ENTER to restart game." << std::endl;
235319
std::cin.ignore();
236320
Window::reset_ball();
237321
}

0 commit comments

Comments
 (0)