-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoeAgent.js
214 lines (183 loc) · 6.3 KB
/
tictactoeAgent.js
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
/*
8 1 6
3 5 7
4 9 2
*/
//This agent will go in the order of the choice queue and then choose randomly after.
//If the other player takes one of the queue's choices, then it will choose randomly that turn instead.
var TestAgent = function (queue) {
this.originalQueue = queue;
this.choiceQueue = this.originalQueue.slice(0); //clones array
this.previousFreeCells = 9;
}
TestAgent.prototype.selectMove = function (board) {
//if the game has reset, reset the queue.
var numFreeCells = getNumFreeCells(board);
if (this.previousFreeCells < numFreeCells) {
this.choiceQueue = this.originalQueue.slice(0); //clones array
}
this.previousFreeCells = numFreeCells;
//select move
var queueChoice = this.choiceQueue.shift();
if (undefined != queueChoice && board.cellFree(queueChoice)) {
return queueChoice;
} else {
var freeCells = [];
for (var i = 1; i < 10; i++) {
if (board.cellFree(i)) freeCells.push(i);
}
return freeCells[Math.floor(Math.random() * freeCells.length)];
}
}
//Solved Agent
var Agent = function () {
this.corners = [8, 6, 4, 2];
this.sides = [1, 3, 7, 9];
this.center = [5];
this.oppositeCorners = {
8: 2,
6: 4,
4: 6,
2: 8
};
};
Agent.prototype.selectMove = function (board) {
var playerLetter = this.getPlayerLetter(board);
var myCells = playerLetter == "X" ? board.X : board.O;
var theirCells = playerLetter == "X" ? board.O : board.X;
var choice = -1;
// Win
choice = this.findWinningCell(board, myCells);
if (choice > 0) return choice;
// Block
choice = this.findWinningCell(board, theirCells);
if (choice > 0) return choice;
// Block Fork
choice = this.findForkBlock(board, theirCells, myCells);
if (choice > 0) return choice;
// Center
choice = this.chooseAnyIfFree(board, this.center);
if (choice > 0) return choice;
// Opposite Corner
choice = this.chooseOppositeCorner(board, theirCells);
if (choice > 0) return choice;
// Random Corner
choice = this.chooseAnyIfFree(board, this.corners);
if (choice > 0) return choice;
// Random Side
choice = this.chooseAnyIfFree(board, this.sides);
if (choice > 0) return choice;
console.log("AssertionError: all cells are full but the game has not ended.");
return -1;
}
Agent.prototype.getPlayerLetter = function (board) {
return getNumFreeCells(board) % 2 == 1 ? "X" : "O";
}
Agent.prototype.findWinningCell = function (board, playerCells) {
for (var i = 0; i < playerCells.length; i++) {
for (var j = i + 1; j < playerCells.length; j++) {
var cellToWin = 15 - (playerCells[i] + playerCells[j]);
if (board.cellFree(cellToWin) && 0 < cellToWin && cellToWin < 10) {
return cellToWin;
}
}
}
return -1;
}
Agent.prototype.findForkBlock = function (board, theirCells, myCells) {
// Corner Overload Fork
// Looks for this scenario in all 4 rotations
//
// | XO | Good
// | OX |
// | | | X | X | | | Shuts down opponent and forces a block
// | X | XO | XO | XO |
// | | | | | | X | XX | Bad
// | XO | XO | Normal choice is random corner
// | O | O | Opponent blocks AND sets up a fork
//
// Makes sure to plug this fork
var cornerOverloads = {
3: [9, 1],
1: [3, 7],
7: [1, 9],
9: [7, 3]
};
var overloadChoices = {
"13": 8,
"17": 6,
"39": 4,
"79": 2
}
for (var i = 0; i < theirCells.length; i++) {
var firstCell = theirCells[i];
var options = cornerOverloads[firstCell];
if (undefined != options) {
//firstCell is a side, check if an adjacent side is filled by opponent
for (var j = 0; j < options.length; j++) {
var item = options[j];
var index = theirCells.indexOf(item);
if (index >= 0) {
//corner is overloaded, fill the corner if possible
var secondCell = theirCells[index];
var overloadString = [firstCell, secondCell].sort().join("");
var overloadCorner = overloadChoices[overloadString];
if (board.cellFree(overloadCorner)) {
return overloadCorner;
}
}
}
}
}
// Opposite Corners Fork
// Looks for this scenario in both rotations
//
// | X | Good
// | O |
// | | | X | | XO | Forces opponent to block
// | | O | O |
// | X | X | X | | O X | O X | Bad
// | O | O | Normal choice is random corner
// | X | X X | Opponent blocks AND sets up a fork
//
// Makes sure to plug this fork by picking any side
for (var i = 0; i < theirCells.length; i++) {
var firstCell = theirCells[i];
var oppositeCorner = this.oppositeCorners[firstCell];
if (undefined != oppositeCorner
&& theirCells.indexOf(oppositeCorner) >= 0
&& myCells.indexOf(5) >= 0) {
return this.chooseAnyIfFree(board, this.sides);
}
}
return -1;
}
Agent.prototype.chooseAnyIfFree = function (board, cellChoices) {
var freeCells = [];
for (var i = 0; i < cellChoices.length; i++) {
var choice = cellChoices[i];
if (board.cellFree(choice)) {
freeCells.push(choice);
}
}
return freeCells[Math.floor(Math.random() * freeCells.length)];
}
Agent.prototype.chooseOppositeCorner = function (board, theirCells) {
for (var i = 0; i < theirCells.length; i++) {
var cell = theirCells[i];
var opposite = this.oppositeCorners[cell];
if (undefined != opposite && board.cellFree(opposite)) {
return opposite;
}
}
return -1;
}
var getNumFreeCells = function (board) {
var freeCellCount = 0;
for (var i = 1; i < 10; i++) {
if (board.cellFree(i)) {
freeCellCount++;
}
}
return freeCellCount;
}