-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
219 lines (169 loc) · 6.38 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
const restartButton = document.getElementById('btn-restart');
const difficultyInput = document.getElementById('difficulty');
const playerSign = 'x';
const robotSign = 'o';
const robotTurnEvent = new CustomEvent('robotTurn');
const tableFull = new CustomEvent('tableFull');
const game = new CustomEvent('game');
let robotTurn = Boolean(Math.floor(Math.random() * 2));
let winner = null;
let difficultyIndex = difficultyInput.value;
const difficultyValues = [1, 10, 1000, 10000];
class Table {
constructor(baseTable = Array(9).fill('')) {
this.squares = document.querySelectorAll('td');
this.tableElement = document.querySelector('table');
this.table = baseTable;
this.squares.forEach(square => {
square.addEventListener('click', () => {
this.check(square.dataset.index, playerSign)
});
})
}
check(squareIndex, sign) {
if (robotTurn && sign !== robotSign) return ;
if (this.squares[squareIndex].classList.contains('checked')) return;
for (let square of this.squares) {
if (square.dataset.index == squareIndex) {
square.innerHTML = sign;
square.classList.add('checked');
this.table[squareIndex] = sign;
if (Table.verifyWinner(this.table, sign)) {
winner = sign;
window.dispatchEvent(new CustomEvent('endOfGame', { detail: { winner } }));
return;
}
if (Table.getAvailableSquares(this.table).length === 0) {
window.dispatchEvent(tableFull);
return;
}
if (!robotTurn) window.dispatchEvent(robotTurnEvent);
}
}
}
static getAvailableSquares(table) {
const free = table.map((square, index) => {
if (square === '') {
return index;
}
});
return free.filter(position => typeof position === 'number');
}
block() {
document.querySelector('.block').style.display = 'flex';
}
unblock() {
document.querySelector('.block').style.display = 'none';
}
reset() {
this.squares.forEach(square => {
square.innerHTML = '';
square.classList.remove('checked');
});
this.table.fill('');
}
static verifyWinner(table, sign) {
// lines
if (table[0] == sign && table[1] == sign && table[2] == sign) {
return true;
} else if (table[3] == sign && table[4] == sign && table[5] == sign) {
return true;
} else if (table[6] == sign && table[7] == sign && table[8] == sign) {
return true;
}
// columns
else if (table[0] == sign && table[3] == sign && table[6] == sign) {
return true;
} else if (table[1] == sign && table[4] == sign && table[7] == sign) {
return true;
} else if (table[2] == sign && table[5] == sign && table[8] == sign) {
return true;
}
// Crosses
else if (table[0] == sign && table[4] == sign && table[8] == sign) {
return true;
} else if (table[2] == sign && table[4] == sign && table[6] == sign) {
return true;
} else {
return false
}
}
}
class Robot {
play(table) {
robotTurn = true;
table.block();
const bestPosition = Robot.getBestSquareToCheck(table.table, robotSign);
table.check(bestPosition, robotSign);
setTimeout(() => {
robotTurn = false;
table.unblock();
}, 0);
}
static getBestSquareToCheck(table) {
const availableSquares = Table.getAvailableSquares(table);
const availableSquaresRates = Array(availableSquares.length).fill(0);
for (let i in availableSquares) {
const copyTable = [...table];
copyTable[availableSquares[i]] = robotTurn ? robotSign : playerSign;
for (let j = 0; j < difficultyValues[difficultyIndex]; j++) {
let testTable = [...copyTable];
let robotTurnTest = !!robotTurn;
let weight = availableSquares.length;
while (true) {
weight -= 1;
robotTurnTest = !robotTurnTest;
const availableSquaresTest = Table.getAvailableSquares(testTable);
if (availableSquaresTest.length === 0) break;
const randomSquare = availableSquaresTest[Math.floor(Math.random() * availableSquaresTest.length)]
if (robotTurnTest) {
testTable[randomSquare] = robotSign;
if (Table.verifyWinner(testTable, robotSign)) {
availableSquaresRates[i] += weight;
break;
}
} else {
testTable[randomSquare] = playerSign;
if (Table.verifyWinner(testTable, playerSign)) {
availableSquaresRates[i] -= weight;
break;
}
}
}
}
}
const best = availableSquaresRates.reduce((previous, current) => {
return Math.max(previous, current);
});
const bestAvailableSquareIndex = availableSquaresRates.indexOf(best);
return availableSquares[bestAvailableSquareIndex];
}
}
const table = new Table();
const robot = new Robot();
restartButton.addEventListener('click', () => {
window.dispatchEvent(game);
});
difficultyInput.addEventListener('change', () => difficultyIndex = difficultyInput.value);
window.addEventListener('robotTurn', () => {
robot.play(table, robotSign)
});
window.addEventListener('endOfGame', ({ detail }) => {
alert((detail.winner === 'o' ? 'Robot' : 'You') + ' won!!!');
table.block()
setTimeout(() => {
table.reset();
table.unblock();
}, 1000);
});
window.addEventListener('tableFull', () => {
setTimeout(() => {
table.reset();
}, 1000);
});
window.addEventListener('game', () => {
robotTurn = Boolean(Math.floor(Math.random() * 2));
table.reset();
if (robotTurn) window.dispatchEvent(robotTurnEvent);
})
window.dispatchEvent(game);