-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
236 lines (208 loc) · 5.15 KB
/
script.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
/*Code of Minmax here*/
var board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
var HUMAN = -1;
var COMP = +1;
/* Function to heuristic evaluation of state. */
function evalute(state) {
var score = 0;
if (gameOver(state, COMP)) {
score = +1;
}
else if (gameOver(state, HUMAN)) {
score = -1;
} else {
score = 0;
}
return score;
}
/* This function tests if a specific player wins */
function gameOver(state, player) {
var win_state = [
[state[0][0], state[0][1], state[0][2]],
[state[1][0], state[1][1], state[1][2]],
[state[2][0], state[2][1], state[2][2]],
[state[0][0], state[1][0], state[2][0]],
[state[0][1], state[1][1], state[2][1]],
[state[0][2], state[1][2], state[2][2]],
[state[0][0], state[1][1], state[2][2]],
[state[2][0], state[1][1], state[0][2]],
];
for (var i = 0; i < 8; i++) {
var line = win_state[i];
var filled = 0;
for (var j = 0; j < 3; j++) {
if (line[j] == player)
filled++;
}
if (filled == 3)
return true;
}
return false;
}
/* This function test if the human or computer wins */
function gameOverAll(state) {
return gameOver(state, HUMAN) || gameOver(state, COMP);
}
function emptyCells(state) {
var cells = [];
for (var x = 0; x < 3; x++) {
for (var y = 0; y < 3; y++) {
if (state[x][y] == 0)
cells.push([x, y]);
}
}
return cells;
}
/* A move is valid if the chosen cell is empty */
function validMove(x, y) {
var empties = emptyCells(board);
try {
if (board[x][y] == 0) {
return true;
}
else {
return false;
}
} catch (e) {
return false;
}
}
/* Set the move on board, if the coordinates are valid */
function setMove(x, y, player) {
if (validMove(x, y)) {
board[x][y] = player;
return true;
}
else {
return false;
}
}
/* *** AI function that choice the best move *** */
// Read more on https://github.com/Cledersonbc/tic-tac-toe-minimax/
function minimax(state, depth, player) {
var best;
if (player == COMP) {
best = [-1, -1, -1000];
}
else {
best = [-1, -1, +1000];
}
if (depth == 0 || gameOverAll(state)) {
var score = evalute(state);
return [-1, -1, score];
}
emptyCells(state).forEach(function (cell) {
var x = cell[0];
var y = cell[1];
state[x][y] = player;
var score = minimax(state, depth - 1, -player);
state[x][y] = 0;
score[0] = x;
score[1] = y;
if (player == COMP) {
if (score[2] > best[2])
best = score;
}
else {
if (score[2] < best[2])
best = score;
}
});
return best;
}
/* It calls the minimax function */
function aiTurn() {
var x, y;
var move;
var cell;
if (emptyCells(board).length == 9) {
x = parseInt(Math.random() * 3);
y = parseInt(Math.random() * 3);
}
else {
move = minimax(board, emptyCells(board).length, COMP);
x = move[0];
y = move[1];
}
if (setMove(x, y, COMP)) {
cell = document.getElementById(String(x) + String(y));
cell.innerHTML = "O";
}
}
/* main */
function clickedCell(cell) {
var button = document.getElementById("bnt-restart");
button.disabled = true;
var conditionToContinue = gameOverAll(board) == false && emptyCells(board).length > 0;
if (conditionToContinue == true) {
var x = cell.id.split("")[0];
var y = cell.id.split("")[1];
var move = setMove(x, y, HUMAN);
if (move == true) {
cell.innerHTML = "X";
if (conditionToContinue)
aiTurn();
}
}
if (gameOver(board, COMP)) {
var lines;
var cell;
var msg;
if (board[0][0] == 1 && board[0][1] == 1 && board[0][2] == 1)
lines = [[0, 0], [0, 1], [0, 2]];
else if (board[1][0] == 1 && board[1][1] == 1 && board[1][2] == 1)
lines = [[1, 0], [1, 1], [1, 2]];
else if (board[2][0] == 1 && board[2][1] == 1 && board[2][2] == 1)
lines = [[2, 0], [2, 1], [2, 2]];
else if (board[0][0] == 1 && board[1][0] == 1 && board[2][0] == 1)
lines = [[0, 0], [1, 0], [2, 0]];
else if (board[0][1] == 1 && board[1][1] == 1 && board[2][1] == 1)
lines = [[0, 1], [1, 1], [2, 1]];
else if (board[0][2] == 1 && board[1][2] == 1 && board[2][2] == 1)
lines = [[0, 2], [1, 2], [2, 2]];
else if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1)
lines = [[0, 0], [1, 1], [2, 2]];
else if (board[2][0] == 1 && board[1][1] == 1 && board[0][2] == 1)
lines = [[2, 0], [1, 1], [0, 2]];
for (var i = 0; i < lines.length; i++) {
cell = document.getElementById(String(lines[i][0]) + String(lines[i][1]));
cell.style.color = "red";
}
msg = document.getElementById("message");
msg.innerHTML = "You lose!";
}
if (emptyCells(board).length == 0 && !gameOverAll(board)) {
var msg = document.getElementById("message");
msg.innerHTML = "Draw!";
}
if (gameOverAll(board) == true || emptyCells(board).length == 0) {
button.value = "Restart";
button.disabled = false;
}
}
/* Restart the game*/
function restartBnt(button) {
if (button.value == "Start AI") {
aiTurn();
button.disabled = true;
}
else if (button.value == "Restart") {
var htmlBoard;
var msg;
for (var x = 0; x < 3; x++) {
for (var y = 0; y < 3; y++) {
board[x][y] = 0;
htmlBoard = document.getElementById(String(x) + String(y));
htmlBoard.style.color = "#444";
htmlBoard.innerHTML = "";
}
}
button.value = "Start AI";
msg = document.getElementById("message");
msg.innerHTML = "";
}
}