-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
237 lines (205 loc) · 5.89 KB
/
server.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
"use strict";
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(express.static(__dirname + "/client"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/client/index.html");
});
app.post("/", function(req, res) {
var game = JSON.parse(req.body.game);
var bot = req.body.bot;
var bestMove = getBestMove(game, bot);
res.send(bestMove);
});
var server = app.listen(process.env.PORT || 3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log("TicTacToe App running on " + host + port);
});
function Game() {
this.map = [[], [], []];
this.gameLength = 0;
this.isNextMoveX = true;
this.isOver = false;
this.winner = null;
this.score = null;
this.parentGame = null;
this.children = [];
}
Game.prototype = {
constructor: Game,
copyGame: function(game) {
this.map = clone(game.map);
this.gameLength = game.gameLength;
this.isNextMoveX = game.isNextMoveX;
this.isOver = game.isOver;
this.winner = game.winner;
this.score = game.score;
},
setGame: function(game) {
this.map = clone(game.map);
this.gameLength = game.gameLength;
this.isNextMoveX = game.isNextMoveX;
this.isOver = game.isOver;
this.winner = game.winner;
this.score = game.score;
this.parentGame = game;
game.children.push(this);
},
makeMove: function(i, j) {
if (!this.isOver && this.map[i][j] == null) {
this.isNextMoveX ? (this.map[i][j] = "X") : (this.map[i][j] = "O");
this.gameLength++;
this.isNextMoveX = !this.isNextMoveX;
if (this.gameLength >= 5) this.checkGame();
}
},
checkGame: function() {
// check horizontal win
for (var i = 0; i < 3; i++) {
var left = this.map[i][0],
center = this.map[i][1],
right = this.map[i][2];
if (left == center && center == right && left != null) {
this.isOver = true;
this.winner = left;
return;
}
}
// check vertical win
for (var i = 0; i < 3; i++) {
var top = this.map[0][i],
center = this.map[1][i],
bottom = this.map[2][i];
if (top == center && center == bottom && top != null) {
this.isOver = true;
this.winner = top;
return;
}
}
// check diagonal win
var topLeft = this.map[0][0],
topRight = this.map[0][2],
centerCenter = this.map[1][1],
bottomRight = this.map[2][2],
bottomLeft = this.map[2][0];
if (topLeft == centerCenter && centerCenter == bottomRight && topLeft != null) {
this.isOver = true;
this.winner = topLeft;
return;
} else if (topRight == centerCenter && centerCenter == bottomLeft && topRight != null) {
this.isOver = true;
this.winner = topRight;
return;
}
// check draw
if (this.gameLength == 9) {
this.isOver = true;
this.winner = "D";
return;
}
},
areChildrenEvaluated: function() {
if (this.score != null) return true;
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].score == null) return false;
}
return true;
},
getNextUnevaluatedChild: function() {
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].score == null) {
return this.children[i];
}
}
}
}
function getPossibleMoves(game) {
var possibleMoves = [];
var possibleScores = [];
var gameMap = game.map;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (gameMap[i][j] == null) possibleMoves.push({"i": i, "j": j});
if (i == 2 && j == 2) return possibleMoves;
}
}
}
function getBestMove(game, bot) {
var games = [];
var _game = new Game();
_game.copyGame(game);
games.push(_game);
// create move tree
while (games.length > 0) {
var current = games.pop();
if (current.isOver) {
// assign score
if (current.winner == bot) {
current.score = 1;
} else if (current.winner == "D") {
current.score = 0;
} else {
current.score = -1;
}
continue;
}
var possibleMoves = getPossibleMoves(current);
for (var i = 0; i < possibleMoves.length; i++) {
var next = new Game();
next.setGame(current);
next.makeMove(possibleMoves[i].i, possibleMoves[i].j);
games.push(next);
}
}
// get best path
// run while the children are not evaluated OR the parent game is not null
while (!_game.areChildrenEvaluated() || _game.parentGame != null) {
if (_game.areChildrenEvaluated()) {
var childrenScores = [];
for (var i = 0; i < _game.children.length; i++) {
childrenScores.push(_game.children[i].score);
}
if (_game.isNextMoveX == (bot == "X")) {
_game.score = Math.max.apply(null, childrenScores);
} else {
_game.score = Math.min.apply(null, childrenScores);
}
_game = _game.parentGame;
} else {
_game = _game.getNextUnevaluatedChild();
}
}
var scores = [];
for (var i = 0; i < _game.children.length; i++) {
scores.push(_game.children[i].score);
}
var maxScore = Math.max.apply(null, scores);
var maxScoreGameIndices = [];
for (var i = 0; i < _game.children.length; i++) {
if (_game.children[i].score == maxScore) {
maxScoreGameIndices.push(i);
}
}
var bestGame = _game.children[maxScoreGameIndices[random(0, maxScoreGameIndices.length - 1)]];
return getMoveDifference(_game, bestGame);
}
function getMoveDifference(gameA, gameB) {
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (gameA.map[i][j] != gameB.map[i][j]) {
return {"i": i, "j": j};
}
}
}
return null;
}
function clone(a) {
return JSON.parse(JSON.stringify(a));
}
function random(min, max) {
return Math.round(Math.random() * (max - min) + min);
}