-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI_Bot.cpp
392 lines (282 loc) · 12.5 KB
/
AI_Bot.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
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <ctime>
#include "AI_Bot.h"
using namespace std;
// this file holds the Artificially intelligent Bot and Random Bot
void initialize_ai_bot(map<string, vector<vector<char>>> &monoid_values) {
// what it does?
// this function loads the values into the map monoid_values. These values are a set of possible arrangements of the Notakto grids
// monoid values will allow the Artificial Intelligent Bot to find the most efficient move
// monoid from what I know is some mathematical word used for sets.
// we have implemented this algorithm using following research paper
// https://arxiv.org/pdf/1301.1672.pdf
// What the inputs are?
// input is simply the map monoid_values passed by reference so that changes are reflected in the main program as well
ifstream fin;
fin.open("q_values.txt"); // this file stores the monoid values
if (fin.fail()) {
cout << "Error!"<<endl;
exit(1);
}
string q_value;
int value_count = 0;
while(fin >> q_value){
fin >> value_count;
char temp_chr;
for (int i = 0; i < value_count; i++) {
monoid_values[q_value].push_back({});
for (int j = 0; j < 9; j++) {
fin >> temp_chr;
monoid_values[q_value][i].push_back(temp_chr); // all the values are loaded to the monoid_values map
// we have used a map since it will allow storage of values as a dictionary
// each monoid values had a corresponding value such as 'c^2' or 'ad'
// these values are then used by algorithm to make the best move
}
}
}
fin.close();
}
vector<char> get_sorted(vector<char> grid) {
// this function will return the sorted vector grid
// this is used to sort an possible arrangment of grid after going through reflection of rotation
// this changes vector from e.g. {'8', '6', 'X', '7', '3', 'X', '1', '0', '4'} to {'0', '1', 'X', '3', '4', 'X', '6', '7', '8'}
vector<char> sorted_grid;
int counter = 0;
for (int i = 0; i < 9; i++) {
if (grid[i] == 'X'){
sorted_grid.push_back('X');
}else {
sorted_grid.push_back(i + '0');
}
}
return sorted_grid;
}
vector<char> get_rotation(vector<char> grid, int degrees) {
// this function rotates the given grid arrangement inside grid by 90 degrees
// and then, return the new rotated arrangement
// this is for the purpose of comparison used by the algorithm to get the best possible move
// this function also uses recursion if e.g. degrees parameter is 180, 270, or 360.
vector<char> rotated_grid;
rotated_grid = {grid[6], grid[3], grid[0], grid[7], grid[4], grid[1], grid[8], grid[5], grid[2]};
rotated_grid = get_sorted(rotated_grid);
if (degrees == 90)
return rotated_grid;
else
return get_rotation(rotated_grid, degrees - 90); // here we can see how it uses recursion. it first rotate 90 degrees and then, calls again for further rotation
}
vector<char> get_reflection(vector<char> grid, char axis) {
// this function is used to reflect the arrangment inside grid about x-axis or y-axis
// return the new reflected arrangement
vector<char> reflected_grid;
if (axis == 'y') {
reflected_grid = {grid[2], grid[1], grid[0], grid[5], grid[4], grid[3], grid[8], grid[7], grid[6]};
} else if (axis == 'x') {
reflected_grid = {grid[6], grid[7], grid[8], grid[3], grid[4], grid[5], grid[0], grid[1], grid[2]};
}
return get_sorted(reflected_grid);
}
string get_q_value(vector<char> current_table, map<string, vector<vector<char>>> monoid_values) {
// this function return the special value associated with the grid arrangement
// it runs through the map monoid values and compares the current grid arrangement with each arrangment in map monoid_values
// once it has found the match then it returns the key associated with that vector inside vector in map
// key can be 'a', 'ad', 'b' etc
// this is in accordance to the research paper
//https://arxiv.org/pdf/1301.1672.pdf
map<string, vector<vector<char>>>::iterator itr;
vector<char> temp_grid;
for(itr = monoid_values.begin(); itr != monoid_values.end(); itr++) {
// cout << (*itr).first << endl;
for (int i = 0; i < monoid_values[(*itr).first].size(); i++){
temp_grid = (*itr).second[i];
if (current_table == temp_grid
|| current_table == get_reflection(temp_grid, 'x')
|| current_table == get_reflection(temp_grid, 'y')
|| current_table == get_rotation(temp_grid, 90)
|| current_table == get_rotation(temp_grid, 180)
|| current_table == get_rotation(temp_grid, 270)){
return (*itr).first;
}
}
}
return ""; // there are going to be grid arrangments which have no associated value
// for those grids we simply return nothing or you can say empty string
}
string simplify_monoid(string monoid) {
// this function takes in the monoid string which can look something like following "aaacccadad"
// then, this string is simplified to something like "acc" and returned
// this simplification happens on the basis of method given in the below paper
// https://arxiv.org/pdf/1301.1672.pdf
bool no_change = true;
string simplified_monoid = "";
int count_a = count(monoid.begin(), monoid.end(), 'a');
int count_b = count(monoid.begin(), monoid.end(), 'b');
int count_c = count(monoid.begin(), monoid.end(), 'c');
int count_d = count(monoid.begin(), monoid.end(), 'd');
if (count_a >= 2) {
no_change = false;
count_a = count_a - 2;
}
if (count_b >= 3) {
no_change = false;
count_b = count_b - 2;
}
if (count_b >= 2 && count_c >= 1) {
no_change = false;
count_b = count_b - 2;
}
if (count_c >= 3) {
no_change = false;
count_c = count_c - 1;
count_a = count_a + 1;
}
if (count_b >= 2 && count_d >= 1) {
no_change = false;
count_b = count_b - 2;
}
if (count_c >= 1 && count_d >= 1) {
no_change = false;
count_c = count_c - 1;
count_a = count_a + 1;
}
if (count_d >= 2) {
no_change = false;
count_d = count_d - 2;
count_c = count_c + 2;
}
if(no_change) {
return monoid;
}else {
for (int i = 0; i < count_a; i++)
simplified_monoid = simplified_monoid + "a";
for (int i = 0; i < count_b; i++)
simplified_monoid = simplified_monoid + "b";
for (int i = 0; i < count_c; i++)
simplified_monoid = simplified_monoid + "c";
for (int i = 0; i < count_d; i++)
simplified_monoid = simplified_monoid + "d";
return simplify_monoid(simplified_monoid); // it uses recursion to simplify the monoid as much as possible.
// the recursion ends when there are no changes made to the monoid string passed to the function
}
}
string get_monoid(map<char, vector<char>> test_tables, map<string, vector<vector<char>>> monoid_values) {
// since our game has multiple grids during the game and those all are dynamically arranged
// therefore, the combined monoid value associated with the current grids changes after every move
// this function is used to iterate through each grid and then, find the monoid value of each grid and
// combine those monoid values and concatenate them into a string called monoid
// this string is then returned
string monoid = "";
map<char, vector<char>>::iterator itr;
for(itr = test_tables.begin(); itr != test_tables.end(); itr++)
monoid += get_q_value((*itr).second, monoid_values);
return simplify_monoid(monoid); // once all monoid values are combined into monoid then it is passed to function called simplify_monoid
// then, output of simplify_monoid will be returned
}
bool check_ai_move(map<char, vector<char>> test_tables, map<string, vector<vector<char>>> monoid_values) {
// this function takes in the grids of the game stored inside test_tables along with monoid values initialized
// then, these values are passed to get_monoid function to get a simplified monoid value e.g. "bc"
// this function will tell whether the recent move made is the best possible move or not
// it will return true if move is best
// it will return false if move is not best
string monoid = get_monoid(test_tables, monoid_values);
// below is the comparison. if simplified monoid value is one of the following: "a", "bc", "cb", "bb", "cc" then the move is best
if (monoid == "a"
|| monoid == "bc"
|| monoid == "cb"
|| monoid == "bb"
|| monoid == "cc")
return true;
return false;
}
void get_ai_move(coordinates &move, map<char, vector<char>> tables, map<string, vector<vector<char>>> monoid_values) {
map<char, vector<char>>::iterator itr;
// this function takes in the struct coordinates move variable to store the best move or any move
// it iterates through all of grids and makes changes and uses new moves once it has found the best move it is stored inside move variable
// function is returned (stopped)
for (itr = tables.begin(); itr != tables.end(); itr++) {
map<char, vector<char>> test_tables = tables;
for (int i = 0; i < 9; i++) {
if (test_tables[(*itr).first][i] != 'X'){
test_tables[(*itr).first][i] = 'X';
}else {
continue;
}
if (check_ai_move(test_tables, monoid_values)) {
move.grid_name = (*itr).first;
move.position = i;
// return move;
return;
}else {
test_tables[(*itr).first][i] = i + '0';
}
}
}
// there can be scenario where there are no best possible moves remaining
// for that situation below iteration allows for move to take place on the first empty cell so if A3 if first empty cell in grids
// then, it is returned
for (itr = tables.begin(); itr != tables.end(); itr++) {
for (int i = 0; i < 9; i++) {
if (tables[(*itr).first][i] != 'X'){
move.grid_name = (*itr).first;
move.position = i;
return;
}
}
}
return;
}
// above code is written by Jam Kabeer Ali Khan (UID: 3035918749)
// below code is written by Yuanto
coordinates get_random_move(map<char, vector<char>> tables)
{
srand(time(0));
map<char, vector<char>>::iterator itr;
char board;
int pos;
int map_size = tables.size();
int rand_board;
int rand_pos;
//randomize board
rand_board = rand() % map_size;
rand_pos = rand() % 9;
while (true)
{
int board_count = -1;
for (itr = tables.begin();itr != tables.end();itr++)
{
board_count++;
if (board_count == rand_board)
{
board = itr->first;
vector<char> vect_temp = itr->second;
vector<char>::iterator itr2;
int board_pos = 0;
for (itr2 = vect_temp.begin();itr2 != vect_temp.end();itr2++)
{
if (board_pos == rand_pos)
{
if (*itr2 == 'X')
{
rand_pos = rand() % 9;
}
else
{
coordinates coordinate;
coordinate.grid_name = board;
coordinate.position = rand_pos;
return coordinate;
}
}
board_pos++;
}
}
}
}
}