/
tictactoe.js
241 lines (205 loc) · 5.34 KB
/
tictactoe.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
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
function TicTacToe(vars) {
vars = vars || {};
this.vars = vars;
this.board = new Array();
this.size = Number(vars.size) || 3;
this.depth = Number(vars.depth) || Infinity;
this.hardness = Number(vars.hardness) || 1.0;
this.players = Number(vars.players) || 1;
this.turn = vars.turn || 'x';
for (var i = 0; i < this.size; i++) {
this.board[i] = new Array();
for (var j = 0; j < this.size; j++) {
this.board[i][j] = '-';
}
}
};
TicTacToe.prototype.get_board = function() {
return this.board;
};
TicTacToe.prototype.make_player_move = function(i,j) {
if (_.isUndefined(j)){
j = ~~(i/this.size);
i = i % this.size;
}
if (!(0 <= i && i <= this.size-1 && 0 <= j && j <= this.size-1) ||
this.board[i][j] != '-') {
return false;
}
this.board[i][j] = this.turn;
this.turn = this.turn === 'x'? 'o': 'x';
return true;
};
TicTacToe.prototype.make_ai_move = function() {
var available_moves = this.get_moves();
var move = toss_a_coin(this.hardness)? alpha_beta(this): _.sample(available_moves);
this.board[move[0]][move[1]] = 'o';
this.turn = this.turn === 'x'? 'o': 'x';
return move[1]*this.size+move[0];
};
TicTacToe.prototype.get_moves = function(player) {
var moves = new Array();
for (var i = 0; i < this.size; i++) {
for (var j = 0; j < this.size; j++) {
if(this.board[i][j] == '-') {
moves.push([i,j]);
}
}
}
return moves;
};
TicTacToe.prototype.is_terminal = function() {
var no_spaces = !_.chain(this.board).flatten().some(_.equals('-')).value();
return no_spaces || this.get_winner() != false;
};
TicTacToe.prototype.get_score = function() {
var lines = get_lines(this.board), score = 0;
winner = this.get_winner();
if (winner==='x') return Infinity;
else if (winner ==='o') return -Infinity;
else{
for (var i = 0; i < lines.length; i++) {
var xrow = _.chain(lines[i])
.filter( _.equals('x'))
.size()
.value();
var orow = _.chain(lines[i])
.filter( _.equals('o'))
.size()
.value();
score += Math.pow(10, xrow);
score -= Math.pow(10, orow);
}
}
return score;
};
TicTacToe.prototype.get_winner = function() {
var lines = get_lines(this.board);
for (var i = 0; i < lines.length; i++) {
if (_.every(lines[i], _.equals('x'))) {
return 'x';
}
if (_.every(lines[i], _.equals('o'))) {
return 'o';
}
}
return false;
};
TicTacToe.prototype.get_winner_place = function() {
var places = ["ver", "hor", "diag"];
var lines = get_lines(this.board);
for (var i = 0; i < lines.length; i++) {
if (_.every(lines[i], _.equals('x')) || _.every(lines[i], _.equals('o'))) {
var place = places[~~(i/this.size)];
var row = i%this.size;
if (place==="diag" && row === 1){
place = "rev"+place;
}
return {"row":row, "place":place};
}
}
return false;
};
TicTacToe.prototype.get_next = function(move,player) {
if (player == "max") {
player = 'x';
} else {
player = 'o';
}
var next_state = new TicTacToe(this.vars);
next_state.board = copy_board(this.board);
next_state.board[move[0]][move[1]] = player;
return next_state;
};
function alpha_beta(state) {
// this method is called by AI, so we started with min.
return min_value(state,-Infinity,Infinity,true, state.depth);
};
function max_value(state,alpha,beta,is_first, depth) {
var is_first = is_first || false;
if (state.is_terminal()||depth===0) {
return state.get_score();
}
var v = -Infinity, moves = state.get_moves("max"), min, best_move = moves[0];
for (var i = 0; i < moves.length; i++) {
min = min_value(state.get_next(moves[i],"max"),alpha,beta,false, depth-1);
if (min > v) v = min, best_move = moves[i];
if (v >= beta) {
if (is_first) return moves[i];
return v;
}
if (v > alpha) alpha = v;
}
if (is_first) {
return best_move;
} else {
return v;
}
};
function min_value(state,alpha,beta,is_first, depth) {
var is_first = is_first || false;
if (state.is_terminal()||depth===0) {
// this method returns the heuristic value of the state,
// with win = Infinity, and lose = -Infinity
return state.get_score();
}
var v = Infinity, moves = state.get_moves("min"), max, best_move = moves[0];
for (var i = 0; i < moves.length; i++) {
max = max_value(state.get_next(moves[i],"min"),alpha,beta,false, depth-1);
if (max < v) v = max, best_move = moves[i];
if (v <= alpha) {
if (is_first) return moves[i];
return v;
}
if (v < beta) beta = v;
}
if (is_first) {
return best_move;
} else {
return v;
}
};
function copy_board(board) {
var new_board = Array();
for (var i = 0; i < board.length; i++) {
new_board[i] = board[i].slice(0);
}
return new_board;
};
function get_lines(mat){
var lines = new Array();
if (!mat) return lines;
var i, j, line;
for (i = 0; i<mat.length; i++) {
line = Array();
for (j = 0; j<mat[0].length; j++) {
line.push(mat[i][j]);
}
lines.push(line);
}
for (j = 0; j<mat[0].length; j++) {
line = Array();
for (i = 0; i<mat.length; i++) {
line.push(mat[i][j]);
}
lines.push(line);
}
if (mat.length === mat[0].length){
line = Array();
for (i = 0; i<mat.length; i++) {
line.push(mat[i][i]);
}
lines.push(line);
line = Array();
for (i = 0; i<mat.length; i++) {
line.push(mat[i][mat.length-i-1]);
}
lines.push(line);
}
return lines;
}
// returns true with a probability of prob
function toss_a_coin(prob){
return Math.random() < prob;
}
_.mixin({equals:function(eq){return function(val){return val===eq;}; }});