-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
376 lines (326 loc) · 11.5 KB
/
game.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const socketProtocol = location.protocol === "https:" ? "wss" : "ws";
const socketUrl = `${socketProtocol}://${location.host}`
const playerTypeChooserDiv = document.querySelector(".player-type-chooser");
const usernameInput = document.querySelector("#username");
const randomPlayerButton = document.querySelector("#random-player");
const customPlayerButton = document.querySelector("#custom-player");
const customPlayerChooserDiv = document.querySelector(".custom-player-chooser");
const clientIdArea = document.querySelector("#client-id");
const customPlayerIdInput = document.querySelector("#custom-player-id");
const customPlayerConnectButton = document.querySelector(
"#custom-player-connect",
);
const spinner = document.querySelector(".spinning-animation");
const gameDetails = document.querySelector("#game-details");
const gameTitleArea = document.querySelector("#game-title");
const redCountArea = document.querySelector("#red-count");
const greenCountArea = document.querySelector("#green-count");
const turnArea = document.querySelector("#turn");
const gameBoard = document.querySelector(".board");
const boardButtons = document.querySelectorAll(".board button");
const buttonsDiv = document.querySelector("#buttons");
const resultDiv = document.querySelector(".game-result");
function hide(...elems) {
for (const elem of elems) elem.classList.add("hidden");
}
function show(...elems) {
for (const elem of elems) elem.classList.remove("hidden");
}
const socket = new WebSocket(socketUrl);
const colors = ["red", "green"];
let clientId;
let opponentId;
let usernames = {};
let players;
let board = [];
let round = -1;
let validMoves = [];
socket.onopen = () => console.log("Connection opened!");
socket.onclose = () => console.log("Connection closed!");
socket.onmessage = (message) => {
const data = JSON.parse(message.data);
console.log(data);
switch (data.type) {
// When new connection is made with server
case "connection":
clientId = data.clientId;
clientIdArea.innerHTML = clientId;
break;
// Initialize the game after pairing with opponent
case "pairSuccess":
hide(spinner, playerTypeChooserDiv, customPlayerChooserDiv);
show(gameBoard, gameDetails);
opponentId = data.opponentId;
usernames[opponentId] = data.opponentName;
gameTitleArea.innerHTML = `${usernames[clientId]} v/s ${usernames[opponentId]}`;
players =
data.first === clientId
? [clientId, opponentId]
: [opponentId, clientId];
console.log(players);
boardInitialize();
break;
// Could not pair with opponent
case "pairError":
alert("Could not pair!");
hide(spinner, gameDetails, gameBoard);
show(playerTypeChooserDiv);
break;
// Somebody disconnects
case "unpair":
alert("Opponent disconnected");
boardReset();
hide(spinner, gameDetails, gameBoard);
show(playerTypeChooserDiv);
break;
// Someone (opponent or us) has made a requested a move
case "move":
if (!board) {
console.error("Cannot move board is undefined");
return;
}
const moved = makeMove(data.row, data.col);
moved && boardUpdate();
break;
// Game has ended
case "gameEnd":
endGame();
break;
default:
console.error("Message type unknown!");
}
};
// Enable buttons after typing some username
function enableButtons() {
randomPlayerButton.disabled = false;
customPlayerButton.disabled = false;
usernameInput.removeEventListener("input", enableButtons);
}
usernameInput.addEventListener("input", enableButtons);
// Notify the socket server to connect with random opponent
randomPlayerButton.onclick = () => {
const name = usernameInput.value.trim();
if (!name) return;
usernames[clientId] = name;
hide(playerTypeChooserDiv);
show(spinner);
socket.send(
JSON.stringify({
type: "connectRandomly",
username: name,
}),
);
};
// Send the opponent id to server for custom connection
customPlayerButton.onclick = () => {
const name = usernameInput.value.trim();
if (!name) return;
usernames[clientId] = name;
hide(playerTypeChooserDiv);
show(customPlayerChooserDiv);
socket.send(
JSON.stringify({
type: "prepareCustomConnection",
username: name,
}),
);
};
// Connect when clicked on connect button (id is sent)
customPlayerConnectButton.onclick = () => {
const id = customPlayerIdInput.value.trim();
if (!id) return;
console.log(`Connecting to custom player of id ${id}`);
hide(customPlayerChooserDiv);
show(spinner);
socket.send(
JSON.stringify({
type: "connectViaOpponentId",
opponentId: id,
}),
);
};
// Initializes the game board with initial 4 pieces
// game board is represented by global `board`
function boardInitialize() {
const btnArr = Array.from(boardButtons);
console.log(btnArr);
for (let i = 0; i < 8 * 8; i += 8) board.push(btnArr.slice(i, i + 8));
console.log(board);
board[3][3].className = colors[0];
board[4][4].className = colors[0];
board[3][4].className = colors[1];
board[4][3].className = colors[1];
boardUpdate();
}
// Reset the game board (useful when oppoent disconnects)
function boardReset() {
// Reset colors
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
board[i][j].className = "";
board[i][j].innerHTML = "";
}
}
// Remove eventlisteners on valid moves
while (validMoves.length > 0) {
const [a, b, handler] = validMoves.pop();
console.log("Removing listener for", board[a][b]);
board[a][b].removeEventListener("click", handler);
board[a][b].innerHTML = "";
}
// Reset variables
board = [];
round = -1;
players = undefined;
delete usernames[opponentId];
opponentId = undefined;
// Hide result (opponent may have quit after ending the game)
resultDiv.innerHTML = "";
hide(resultDiv);
}
// Updates game info on webpage and changes board states
function boardUpdate() {
++round;
const movingPlayer = players[round % 2];
console.log("players[gameRound % 2]:", movingPlayer);
turnArea.innerHTML = usernames[movingPlayer];
turnArea.className = colors[round % 2];
redCountArea.innerHTML = colorCount("red");
greenCountArea.innerHTML = colorCount("green");
// No need to check valid moves for us if its opponent's turn
if (movingPlayer === opponentId) return;
// Check valid moves and add click event listeners for them
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
const button = board[i][j];
const clickHandler = () => {
console.log("Broadcasting ...");
broadcastMove(i, j);
while (validMoves.length > 0) {
const [a, b, handler] = validMoves.pop();
console.log("Removing listener for", board[a][b]);
board[a][b].removeEventListener("click", handler);
board[a][b].innerHTML = "";
}
};
if (isValidMove(i, j)) {
button.innerHTML = "<span style='color: white;'>.</span>";
button.addEventListener("click", clickHandler);
validMoves.push([i, j, clickHandler]);
}
}
}
// End the game if no valid moves remaining
if (validMoves.length === 0) broadcastGameEnd();
}
function colorCount(color) {
let count = 0;
for (let i = 0; i < board.length; i++)
for (let j = 0; j < board[0].length; j++)
if (board[i][j].className === color) count++;
return count;
}
function isValidMove(i, j) {
const supportingColor = colors[round % 2];
// Invalid if piece already taken
if (!isEmpty(i, j)) return false;
// Loop thorough neighboring points to check validtity of move
for (let x = i - 1; x <= i + 1; x++) {
for (let y = j - 1; y <= j + 1; y++) {
// Skip pieces not in board array
if (outOfBounds(x, y)) continue;
// Skip empty neighbors
if (isEmpty(x, y)) continue;
// Piece adjacent to moving place cannot be same
if (board[x][y].className == supportingColor) continue;
// Check if supporting piece with same color is on other side
// if yes this move will be valid
// Check along the line stretched by (i, j) and neighbor (x, y)
// Move to next point along the line using a movement vector
const mvector = { x: x - i, y: y - j };
let a = x + mvector.x;
let b = y + mvector.y;
while (!outOfBounds(a, b) && !isEmpty(a, b)) {
if (board[a][b].className === supportingColor) return true;
a += mvector.x;
b += mvector.y;
}
}
}
}
function makeMove(i, j) {
const supportingColor = colors[round % 2];
// Opponent's pieces that this move will consume
let consumables = [];
// Loop thorough neighboring points to check validtity of move
for (let x = i - 1; x <= i + 1; x++) {
for (let y = j - 1; y <= j + 1; y++) {
// Skip invalid pieces
if (outOfBounds(x, y)) continue;
if (isEmpty(x, y)) continue;
if (board[x][y].className == supportingColor) continue;
const mvec = {
x: x - i,
y: y - j,
};
let a = i + mvec.x;
let b = j + mvec.y;
let pieces = [];
while (!outOfBounds(a, b) && !isEmpty(a, b)) {
const piece = board[a][b];
if (piece.className === supportingColor) {
consumables.push(...pieces);
break;
}
pieces.push(piece);
a += mvec.x;
b += mvec.y;
}
}
}
console.log("Consumes: ", consumables);
if (consumables.length === 0) {
console.error("No pieces could be consumed, move invalid!");
return false;
}
consumables.push(board[i][j]);
for (const consumable of consumables)
consumable.className = supportingColor;
return true;
}
function broadcastMove(i, j) {
socket.send(
JSON.stringify({
type: "move",
row: i,
col: j,
}),
);
}
function broadcastGameEnd() {
socket.send(
JSON.stringify({
type: "gameEnd",
}),
);
}
function endGame() {
const redCount = colorCount("red");
const greenCount = colorCount("green");
if (redCount === greenCount) {
resultDiv.innerHTML = "TIE!";
show(resultDiv);
return;
}
const winnerIdx = redCount > greenCount ? 0 : 1;
const winner = usernames[players[winnerIdx]];
resultDiv.innerHTML = `Winner: ${winner}`;
show(resultDiv);
}
function isEmpty(i, j) {
let color = board[i][j].className;
return !colors.includes(color);
}
function outOfBounds(i, j) {
return i < 0 || j < 0 || i > board.length - 1 || j > board[0].length - 1;
}