-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
196 lines (177 loc) · 4.65 KB
/
game.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
class Game {
xFirst = true;
board;
constructor(board = null) {
if (!board) this.board = this.createBoard();
else this.board = board;
}
createBoard() {
return [
[null, null, null],
[null, null, null],
[null, null, null],
];
}
player(state) {
// flatten board
const plays = [...state[0], ...state[1], ...state[2]];
// count number of x
const nbX = plays.reduce((prev, curr) => {
if (curr === 'x') prev++;
}, 0);
// count number of o
const nbO = plays.reduce((prev, curr) => {
if (curr === 'o') prev++;
}, 0);
// if round is odd, x goes first, else o
let order = ['x', 'o'];
if (this.xFirst) order = ['o', 'x'];
return order[Number(nbX === nbO)];
}
actions(state) {
const acts = [];
const player = this.player(state);
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (!state[i][j]) {
acts.push({ player, position: [j, i] });
}
}
}
return acts;
}
result(state, action) {
// example of action {player: 'x', position: [x, y]}
if (!action) return state;
const { player, position } = action;
let c_state = [[...state[0]], [...state[1]], [...state[2]]];
c_state[position[1]][position[0]] = player;
return c_state;
}
terminal(state) {
return (
this.playerWon(state, 'x') ||
this.playerWon(state, 'o') ||
this.tieCheck(state)
);
}
utility(state) {
return this.playerWon(state, 'x') ? 1 : this.playerWon(state, 'o') ? -1 : 0;
}
playerWon(state, player) {
return (
this.verticalWinCheck(state, player) ||
this.horizontalWinCheck(state, player) ||
this.diagonalWinCheck(state, player)
);
}
verticalWinCheck(state, player) {
for (let i = 0; i < 3; i++) {
let rowCount = 0;
for (let j = 0; j < 3; j++) {
if (state[j][i] == player) {
rowCount++;
}
}
if (rowCount === 3) return true;
rowCount = 0;
}
return false;
}
horizontalWinCheck(state, player) {
for (let i = 0; i < 3; i++) {
let rowCount = 0;
for (let j = 0; j < 3; j++) {
if (state[i][j] == player) {
rowCount++;
}
}
if (rowCount === 3) return true;
rowCount = 0;
}
return false;
}
diagonalWinCheck(state, player) {
let dia1Count = 0;
let dia2Count = 0;
for (let i = 0; i < 3; i++) {
if (state[i][i] === player) dia1Count++;
if (state[i][2 - i] === player) dia2Count++;
}
if ([dia1Count, dia2Count].includes(3)) return true;
return false;
}
tieCheck(state) {
return (
(function () {
return ![...state[0], ...state[1], ...state[2]].includes(null);
})() && !(this.playerWon(state, 'x') || this.playerWon(state, 'o'))
);
}
nextRound() {
this.xFirst = !this.xFirst;
return this;
}
makeMove(action) {
this.board = this.result(this.board, action);
}
}
class AI {
play(game) {
if (game.player(game.board) === 'x') return this.max(game);
return this.mini(game);
}
max(game, action = null) {
if (!action || !game.terminal(game.result(game.board, action))) {
const board = action ? game.result(game.board, action) : game.board;
let v = -44444444444;
let choice = {};
for (let action of this.shuffle(game.actions(board))) {
const miniChoice = this.mini(new Game(board).nextRound(), action);
const { utility } = miniChoice;
if (utility > v) {
v = utility;
choice = { ...action, utility };
}
}
return choice;
}
return {
...action,
utility: game.utility(game.result(game.board, action)),
};
}
mini(game, action = null) {
if (!action || !game.terminal(game.result(game.board, action))) {
const board = action ? game.result(game.board, action) : game.board;
let v = 44444444444;
let choice = {};
for (let action of this.shuffle(game.actions(board))) {
const maxChoice = this.max(new Game(board), action);
const { utility } = maxChoice;
if (utility < v) {
v = utility;
choice = { ...action, utility };
}
}
return choice;
}
return {
...action,
utility: game.utility(game.result(game.board, action)),
};
}
shuffle(array) {
let c_array = JSON.parse(JSON.stringify(array));
let arrLen = c_array.length;
while (arrLen != 0) {
let randIndex = Math.floor(Math.random() * arrLen);
arrLen--;
[c_array[arrLen], c_array[randIndex]] = [
c_array[randIndex],
c_array[arrLen],
];
}
return c_array;
}
}