-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTTGame.cpp
417 lines (347 loc) · 13 KB
/
TTTGame.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "TTTGame.hpp"
Game::Game()
{
player1 = new Player();
player2 = new Player();
player1->character = 'x';
player2->character = 'o';
current_player = player1;
board = new Board();
window = new Window();
time = new Time();
input = new Input();
};
Game::~Game()
{
delete player1;
delete player2;
delete board;
delete window;
delete time;
delete input;
}
// Main Menu
void Game::MainMenu()
{
if (game_type == ' ') // Nothing has been selected yet
{
DrawText("Welcome to Tic Tac Toe 5 in a Row!", 500, 200, 14, BLACK);
DrawText("What would you like to do?", 500, 250, 14, BLACK);
DrawText("Type '1' to Play vs a Bot", 500, 300, 14, BLACK);
DrawText("Type '2' to Watch Two Bots Play each other", 500, 350, 14, BLACK);
DrawText("Type '3' to Playback a previous game using a logged file", 500, 400, 14, BLACK);
input->DrawTextBox();
// Checks if the Enter key has been pressed
if(IsKeyPressed(KEY_ENTER))
{
game_type = input->tb.choice[0];
strcpy(input->tb.choice,"\0");
}
}
// HUMAN VS AI GAME
if (game_type == '1')
{
DrawText("Who's Going First? (Type '1' for Human; '2' for Bot):", 500, 300, 14, BLACK);
input->DrawTextBox();
// Checks if the Enter key has been pressed
if(IsKeyPressed(KEY_ENTER))
{
pick_player_turn = input->tb.choice[0];
strcpy(input->tb.choice,"\0");
}
// Initializes the players turns
if (pick_player_turn == '1' || pick_player_turn == '2')
{
if (pick_player_turn == '1')
{
player1->AIflag = false;
player2->AIflag = true;
player2->maximizingplayer = true;
menu = 'g';
}
else
{
player1->AIflag = true;
player1->maximizingplayer = true;
player2->AIflag = false;
menu = 'g';
}
}
else
DrawText("ERROR: Please type in valid choice", 550, 100, 14, BLACK);
}
// AI VS AI GAME
else if (game_type == '2')
{
player1->AIflag = true;
player1->maximizingplayer = true;
player2->AIflag = true;
player2->maximizingplayer = true;
menu = 'g';
}
// WATCH A REPLAY OF A PREVIOUS GAME USING A LOG FILE
else if (game_type == '3')
{
DrawText("Sorry, this didn't get implemented fully...", 500, 250, 14, BLACK);
DrawText("Please exit out of the application", 500, 300, 14, BLACK);
//menu = 'g';
}
}
//My main game loop function:
void Game::MainGameState()
{
////////////////////////////////////////////////////////////////////////////////////
// Main Game Loop //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// HUMAN VS AI GAME //////////////////////////////////////////////
if (game_type == '1')
{
//Draw Board
board->drawBoard();
if (current_player == getHumanPlayer())
{
// Init for tile choices
char board_pos[2];
int row = 0;
int column = 0;
input->tb.textBox.x = 50;
input->tb.textBox.y = 500;
// Game Dialogue
DrawText("It is the Human's turn", 50, 200, 12, BLACK);
DrawText("Please choose where you would like place", 50, 250, 12, BLACK);
DrawText("Format:('12'); where the first number is the column and the second number is the row", 50, 300, 12, BLACK);
DrawText("Numbers range from (1) to (size of board) on both axis (top right is the origin)", 50, 350, 12, BLACK);
input->DrawTextBox();
DrawText("Previous Player's Boards Searched: ", 50, 400, 14, BLACK);
DrawText(std::to_string(boardssearched).c_str(), 325, 400, 14, BLACK);
DrawText("Previous Player's Boards/Sec: ", 50, 450, 14, BLACK);
DrawText(std::to_string(MCTS.boardspersec).c_str(), 325, 450, 14, BLACK);
// Checks if the Enter key has been pressed
if(IsKeyPressed(KEY_ENTER))
{
strcpy(board_pos, input->tb.choice);
strcpy(input->tb.choice,"\0");
row = board_pos[0] - '0';
column = board_pos[1] - '0';
if (row >= 1 && row <= 5 && column >= 1 && column <= 5)
{
if (board->board[row - 1][column - 1].status == ' ')
{
board->board[row - 1][column - 1].status = getHumanPlayer()->character;
current_player = getAIPlayer();
}
else
DrawText("ERROR: Please choose an empty tile", 50, 600, 14, BLACK);
}
else
{
DrawText("ERROR: Please choose a place within the boundaries", 50, 600, 14, BLACK);
}
board->drawBoard();
//SaveMove(row, column);
}
}
else
{
board->drawBoard();
DrawText("It is the Bot's turn", 500, 200, 14, BLACK);
boardssearched = 0;
MCTS.boardspersec = 0;
// Using MCTS as the bot: ///////////////////////////////////////////////////////////////////////////
Player::AiMove hopefully_good_move = MCTS.findNextMove(*board, player1->character, boardssearched);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Using Minimax as the bot: ///////////////////////////////////////////////////////////////////////
//unsigned int start = GetTime();
//Player::AiMove decent_move = player1->Minimax(*board, 5, 2, 2, &boardssearched);
//unsigned int end = GetTime();
//unsigned int time_lapse = start - end;
//DrawText("Boards Searched: ", 50, 250, 14, BLACK);
//DrawText(std::to_string(boardssearched).c_str(), 200, 250, 14, BLACK);
//boardssearched = 0;
//DrawText("Previous Player's Boards/Sec: ", 50, 450, 14, BLACK);
//DrawText(std::to_string(boardssearched/time_lapse).c_str(), 325, 450, 14, BLACK);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Using Random moves as the bot:
//Player::AiMove rand_bot_move = RandomBotMove(game.TicTacToeBoard);
//board->board[decent_move.row][decent_move.column].status = getAIPlayer()->character;
board->board[hopefully_good_move.row][hopefully_good_move.column].status = getAIPlayer()->character;
//SaveMove(rand_bot_move.row, rand_bot_move.column);
current_player = getHumanPlayer();
}
}
// AI VS AI GAME //////////////////////////////////////////////
else if (game_type == '2')
{
//Draw Board
board->drawBoard();
if (current_player == player1)
{
DrawText("It is Player 1's turn", 50, 200, 14, BLACK);
// Using MCTS as the bot: ///////////////////////////////////////////////////////////////////////
// Text to display previous players board statistics
DrawText("Previous Player's Boards Searched: ", 50, 400, 14, BLACK);
DrawText(std::to_string(boardssearched).c_str(), 325, 400, 14, BLACK);
DrawText("Previous Player's Boards/Sec: ", 50, 450, 14, BLACK);
DrawText(std::to_string(MCTS.boardspersec).c_str(), 325, 450, 14, BLACK);
// Resets the stats for next call
boardssearched = 0;
MCTS.boardspersec = 0;
Player::AiMove hopefully_good_move = MCTS.findNextMove(*board, player1->character, boardssearched);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Using Minimax as the bot: ///////////////////////////////////////////////////////////////////////
//unsigned int start = GetTime();
//Player::AiMove decent_move = player1->Minimax(*board, 5, 2, 2, &boardssearched);
//unsigned int end = GetTime();
//unsigned int time_lapse = start - end;
//DrawText("Boards Searched: ", 50, 250, 14, BLACK);
//DrawText(std::to_string(boardssearched).c_str(), 200, 250, 14, BLACK);
//boardssearched = 0;
//DrawText("Previous Player's Boards/Sec: ", 50, 450, 14, BLACK);
//DrawText(std::to_string(boardssearched/time_lapse).c_str(), 325, 450, 14, BLACK);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Using Random moves as the bot:
//Player::AiMove rand_bot_move = RandomBotMove(game.TicTacToeBoard);
board->board[hopefully_good_move.row][hopefully_good_move.column].status = 'x';
//board->board[decent_move.row][decent_move.column].status = 'x';
//board->board[rand_bot_move.row][rand_bot_move.column].status = 'x';
current_player = player2;
//Player::Player tempplayer('x');
//SaveMove(tempplayer, rand_bot_move.row, rand_bot_move.column);
}
else
{
DrawText("It is Player 2's turn", 50, 200, 14, BLACK);
// Using MCTS as the bot: ///////////////////////////////////////////////////////////////////////
// Text to display previous players board statistics
//DrawText("Previous Player's Boards Searched: ", 50, 400, 14, BLACK);
//DrawText(std::to_string(boardssearched).c_str(), 325, 400, 14, BLACK);
//DrawText("Previous Player's Boards/Sec: ", 50, 450, 14, BLACK);
//DrawText(std::to_string(MCTS.boardspersec).c_str(), 325, 450, 14, BLACK);
// Resets the stats for next call
//boardssearched = 0;
//MCTS.boardspersec = 0;
//Player::AiMove hopefully_good_move = MCTS.findNextMove(*board, player2->character, boardssearched);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Using Minimax as the bot: ///////////////////////////////////////////////////////////////////////
unsigned int start = GetTime();
Player::AiMove decent_move = player2->Minimax(*board, 5, 2, 2, &boardssearched);
unsigned int end = GetTime();
unsigned int time_lapse = start - end;
DrawText("Boards Searched: ", 50, 250, 14, BLACK);
DrawText(std::to_string(boardssearched).c_str(), 200, 250, 14, BLACK);
boardssearched = 0;
DrawText("Previous Player's Boards/Sec: ", 50, 450, 14, BLACK);
//DrawText(std::to_string(boardssearched/time_lapse).c_str(), 325, 450, 14, BLACK);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Using Random moves as the bot:
//Player::AiMove rand_bot_move = RandomBotMove(game.TicTacToeBoard);
//board->board[hopefully_good_move.row][hopefully_good_move.column].status = 'o';
board->board[decent_move.row][decent_move.column].status = 'o';
//board->board[rand_bot_move.row][rand_bot_move.column].status = 'o';
current_player = player1;
//Player::Player tempplayer('o');
//SaveMove(tempplayer, rand_bot_move.row, rand_bot_move.column);
}
}
//WATCH A REPLAY OF A PREVIOUS GAME USING A LOG FILE
else if (game_type == '3')
{
//Draw Board
board->drawBoard();
std::string line;
//std::ifstream myfile;
DrawText("Please input the file of the game you would like to playback.", 50, 200, 14, BLACK);
/*std::string input_file;
// INPUT
myfile.open(input_file);
if (myfile.good())
break;
else
DrawText("Invalid File. Please Try Again", 500, 250, 14, BLACK);
//REPLAY PAST GAME USING LOG
if (myfile.is_open())
{
while (getline(myfile, line))
{
std::cout << line << '\n';
}
myfile.close();
}
else
std::cout << "Unable to open file";
*/
}
else
DrawText("Please select a valid option", 50, 200, 14, BLACK);
total_turns++;
// Checks for winner
if (board->ifWinFound() != ' ')
{
if (board->ifWinFound() == player1->character)
winner = &*player1;
else
winner = &*player2;
winfound = true;
}
// Checks for draw found
if (board->ifDrawFound() == true)
{
board->drawBoard();
DrawText("The game ended in a tie!", 50, 400, 14, BLACK);
DrawText("Thanks for playing!", 50, 450, 14, BLACK);
DrawText("Please exit application to start another game", 50, 500, 14, BLACK);
game_type = 'f';
}
// Tells winner and ends game
if (winfound)
{
board->drawBoard();
std::string winnertitle = (winner->AIflag == false) ? ("Human!") : ("Bot...");
DrawText("And the winner is the ", 50, 400, 14, BLACK);
DrawText("Please exit application to start another game", 50, 450, 14, BLACK);
game_type = 'f';
}
}
//Function that combines the players character and their moves to a single string (which will be used in the replay feature that I didn't get to create)
//Then will append three dashes to the string to use as a delimeter for the replay function.
//After the dashes, there will be the log in plain text that people can read back.
void Game::SaveMove(Player player, unsigned int row, unsigned int column)
{
std::string ccharacter = std::to_string(player.character);
std::string crow = std::to_string(row);
std::string ccolumn = std::to_string(column);
std::string tempstring;
tempstring.append(crow);
tempstring.append(ccolumn);
tempstring.append("---");
tempstring.append(ccharacter);
tempstring.append(" played on row ");
tempstring.append(crow);
tempstring.append(" and on column ");
tempstring.append(ccolumn);
gamesave.push_back(tempstring);
}
//My save game function. Not fully implemented yet...
//Will loop through the vector of moves I made and save them to a file
void Game::SaveGame()
{
while (true)
{
std::cout << "Would you like to save this game?" << std::endl;
std::cout << "Type '1' for Yes or '2' for No" << std::endl;
char save_game;
std::cin >> save_game;
if (save_game == '1' || save_game == '2')
{
if (save_game == '1')
{
}
else
{
}
break;
}
else
std::cout << "ERROR: Please select a valid option" << std::endl;
}
}