-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
269 lines (203 loc) · 5.35 KB
/
index.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
class View {
tilesView = [];
boardView = document.querySelector('#board');
currentPlayerView = document.querySelector('#current-player');
newGameBtn = document.querySelector('#new-game');
constructor() {
this.newGameBtn.addEventListener('click', () => {
game.resetGame();
game.startGame();
});
this.createBoard();
}
createBoard() {
// Create a 20x20 grid
for (let i = 1; i < 21; i++) {
for (let j = 1; j < 21; j++) {
const tile = document.createElement('div');
tile.classList.add('tile');
tile.dataset.row = i;
tile.dataset.column = j;
this.boardView.appendChild(tile);
this.tilesView.push(tile);
this.prepareTile(tile);
}
}
}
resetBoard() {
this.tilesView.forEach((tile) => {
while (tile.firstChild) {
tile.removeChild(tile.firstChild);
}
});
}
prepareTile(tile) {
tile.addEventListener('click', (e) => {
if (!game.state.isGameActive || !!tile.firstChild) {
return;
}
const currentPlayer = game.state.currentPlayer;
const marker = document.createElement('img');
marker.src = `./assets/5-in-a-row-marker-${currentPlayer.color}.svg`;
tile.appendChild(marker);
currentPlayer.addTile({
row: parseInt(tile.dataset.row),
column: parseInt(tile.dataset.column)
});
game.engine.endTurn();
});
}
showAlert(msg) {
const alertBox = document.querySelector('.alert-box');
alertBox.classList.add('active');
alertBox.textContent = msg;
setTimeout(() => {
alertBox.classList.remove('active');
}, 2000);
}
updateCurrentPlayer() {
const {name, color} = game.state.currentPlayer;
this.currentPlayerView.textContent = `${name} (${color})`;
}
}
class Engine {
nextTurn() {
if (game.state.currentPlayer === game.players.one) {
game.state.currentPlayer = game.players.two;
}
else {
game.state.currentPlayer = game.players.one;
}
game.view.updateCurrentPlayer();
}
endTurn() {
const populatedTiles = game.state.currentPlayer.populatedTiles;
if (populatedTiles.length < 5) {
this.nextTurn();
}
else {
this.checkForWinner();
}
}
checkForWinner() {
function check(condition) {
let inARow = 0;
for (let i = 0; i < 5; i++) {
if (condition(i)) {
inARow++;
}
}
// Since i doesn't go higher than 5 we don't
// have to do a >= 5 check here
if (inARow === 5) {
return true;
}
}
const populatedTiles = game.state.currentPlayer.populatedTiles;
const hasWinner = populatedTiles.some((tile) => {
const horizontalCondition = check((i) => {
return !!populatedTiles.find((o) => {
return (o.column === tile.column + i) && o.row === tile.row;
});
});
// Horizontally
if (horizontalCondition) {
return true;
}
const diagonalRightCondition = check((i) => {
return !!populatedTiles.find((o) => {
return (o.column === tile.column + i) && (o.row === tile.row + i);
});
});
// Diagonally (right)
if (diagonalRightCondition) {
return true;
}
const diagonalLeftCondition = check((i) => {
return !!populatedTiles.find((o) => {
return (o.column === tile.column - i) && (o.row === tile.row + i);
});
});
// Diagonally (left)
if (diagonalLeftCondition) {
return true;
}
const verticalCondition = check((i) => {
return !!populatedTiles.find((o) => {
return (o.column === tile.column) && (o.row === tile.row + i);
});
});
// Vertically
if (verticalCondition) {
return true;
}
});
if (!hasWinner && game.players.two.populatedTiles.length < 200) {
this.nextTurn();
}
else {
game.endGame(true);
}
}
}
class Player {
populatedTiles = [];
constructor(id, name, color) {
this.id = id;
this.name = name;
this.color = color;
}
addTile(info) {
this.populatedTiles.push(info);
this.populatedTiles.sort((a, b) => (a.row - b.row) || (a.column - b.column)); // Asc order
console.log(this.populatedTiles);
}
reset() {
this.populatedTiles = [];
}
}
class State {
isGameActive = false;
currentPlayer;
freeze() {
this.isGameActive = false;
}
resume() {
this.isGameActive = true;
}
}
class Game {
players;
constructor() {
this.engine = new Engine();
this.state = new State();
this.view = new View();
this.players = {
one: new Player(1, 'Snakeboi229', 'black'),
two: new Player(2, '360noscopingyourmom', 'white')
}
this.state.currentPlayer = this.players.one;
}
resetGame() {
this.view.resetBoard();
this.players.one.reset();
this.players.two.reset();
this.state.currentPlayer = this.players.one;
this.view.updateCurrentPlayer();
}
startGame() {
this.state.resume();
this.view.updateCurrentPlayer();
}
endGame(hasWinner) {
if (hasWinner) {
this.view.showAlert(`${game.state.currentPlayer.name} wins!`);
}
else {
this.view.showAlert('It ends in a draw!');
}
game.state.freeze();
}
}
const game = new Game();
game.startGame();