-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.js
executable file
·275 lines (240 loc) · 7.47 KB
/
main.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
(function init() {
const P1 = 'X';
const P2 = 'O';
let player;
let game;
// const socket = io.connect('http://tic-tac-toe-realtime.herokuapp.com'),
const socket = io.connect('http://localhost:5000');
class Player {
constructor(name, type) {
this.name = name;
this.type = type;
this.currentTurn = true;
this.playsArr = 0;
}
static get wins() {
return [7, 56, 448, 73, 146, 292, 273, 84];
}
// Set the bit of the move played by the player
// tileValue - Bitmask used to set the recently played move.
updatePlaysArr(tileValue) {
this.playsArr += tileValue;
}
getPlaysArr() {
return this.playsArr;
}
// Set the currentTurn for player to turn and update UI to reflect the same.
setCurrentTurn(turn) {
this.currentTurn = turn;
const message = turn ? 'Your turn' : 'Waiting for Opponent';
$('#turn').text(message);
}
getPlayerName() {
return this.name;
}
getPlayerType() {
return this.type;
}
getCurrentTurn() {
return this.currentTurn;
}
}
// roomId Id of the room in which the game is running on the server.
class Game {
constructor(roomId) {
this.roomId = roomId;
this.board = [];
this.moves = 0;
}
// Create the Game board by attaching event listeners to the buttons.
createGameBoard() {
function tileClickHandler() {
const row = parseInt(this.id.split('_')[1][0], 10);
const col = parseInt(this.id.split('_')[1][1], 10);
if (!player.getCurrentTurn() || !game) {
alert('Its not your turn!');
return;
}
if ($(this).prop('disabled')) {
alert('This tile has already been played on!');
return;
}
// Update board after your turn.
game.playTurn(this);
game.updateBoard(player.getPlayerType(), row, col, this.id);
player.setCurrentTurn(false);
player.updatePlaysArr(1 << ((row * 3) + col));
game.checkWinner();
}
for (let i = 0; i < 3; i++) {
this.board.push(['', '', '']);
for (let j = 0; j < 3; j++) {
$(`#button_${i}${j}`).on('click', tileClickHandler);
}
}
}
// Remove the menu from DOM, display the gameboard and greet the player.
displayBoard(message) {
$('.menu').css('display', 'none');
$('.gameBoard').css('display', 'block');
$('#userHello').html(message);
this.createGameBoard();
}
/**
* Update game board UI
*
* @param {string} type Type of player(X or O)
* @param {int} row Row in which move was played
* @param {int} col Col in which move was played
* @param {string} tile Id of the the that was clicked
*/
updateBoard(type, row, col, tile) {
$(`#${tile}`).text(type).prop('disabled', true);
this.board[row][col] = type;
this.moves++;
}
getRoomId() {
return this.roomId;
}
// Send an update to the opponent to update their UI's tile
playTurn(tile) {
const clickedTile = $(tile).attr('id');
// Emit an event to update other player that you've played your turn.
socket.emit('playTurn', {
tile: clickedTile,
room: this.getRoomId(),
});
}
/**
*
* To determine a win condition, each square is "tagged" from left
* to right, top to bottom, with successive powers of 2. Each cell
* thus represents an individual bit in a 9-bit string, and a
* player's squares at any given time can be represented as a
* unique 9-bit value. A winner can thus be easily determined by
* checking whether the player's current 9 bits have covered any
* of the eight "three-in-a-row" combinations.
*
* 273 84
* \ /
* 1 | 2 | 4 = 7
* -----+-----+-----
* 8 | 16 | 32 = 56
* -----+-----+-----
* 64 | 128 | 256 = 448
* =================
* 73 146 292
*
* We have these numbers in the Player.wins array and for the current
* player, we've stored this information in playsArr.
*/
checkWinner() {
const currentPlayerPositions = player.getPlaysArr();
Player.wins.forEach((winningPosition) => {
if ((winningPosition & currentPlayerPositions) === winningPosition) {
game.announceWinner();
}
});
const tieMessage = 'Game Tied :(';
if (this.checkTie()) {
socket.emit('gameEnded', {
room: this.getRoomId(),
message: tieMessage,
});
alert(tieMessage);
location.reload();
}
}
checkTie() {
return this.moves >= 9;
}
// Announce the winner if the current client has won.
// Broadcast this on the room to let the opponent know.
announceWinner() {
const message = `${player.getPlayerName()} wins!`;
socket.emit('gameEnded', {
room: this.getRoomId(),
message,
});
alert(message);
location.reload();
}
// End the game if the other player won.
endGame(message) {
alert(message);
location.reload();
}
}
// Create a new game. Emit newGame event.
$('#new').on('click', () => {
const name = $('#nameNew').val();
if (!name) {
alert('Please enter your name.');
return;
}
socket.emit('createGame', { name });
player = new Player(name, P1);
});
// Join an existing game on the entered roomId. Emit the joinGame event.
$('#join').on('click', () => {
const name = $('#nameJoin').val();
const roomID = $('#room').val();
if (!name || !roomID) {
alert('Please enter your name and game ID.');
return;
}
socket.emit('joinGame', { name, room: roomID });
player = new Player(name, P2);
});
// New Game created by current client. Update the UI and create new Game var.
socket.on('newGame', (data) => {
const message =
`Hello, ${data.name}. Please ask your friend to enter Game ID:
${data.room}. Waiting for player 2...`;
// Create game for player 1
game = new Game(data.room);
game.displayBoard(message);
});
/**
* If player creates the game, he'll be P1(X) and has the first turn.
* This event is received when opponent connects to the room.
*/
socket.on('player1', (data) => {
const message = `Hello, ${player.getPlayerName()}`;
$('#userHello').html(message);
player.setCurrentTurn(true);
});
/**
* Joined the game, so player is P2(O).
* This event is received when P2 successfully joins the game room.
*/
socket.on('player2', (data) => {
const message = `Hello, ${data.name}`;
// Create game for player 2
game = new Game(data.room);
game.displayBoard(message);
player.setCurrentTurn(false);
});
/**
* Opponent played his turn. Update UI.
* Allow the current player to play now.
*/
socket.on('turnPlayed', (data) => {
const row = data.tile.split('_')[1][0];
const col = data.tile.split('_')[1][1];
const opponentType = player.getPlayerType() === P1 ? P2 : P1;
game.updateBoard(opponentType, row, col, data.tile);
player.setCurrentTurn(true);
});
// If the other player wins, this event is received. Notify user game has ended.
socket.on('gameEnd', (data) => {
game.endGame(data.message);
socket.leave(data.room);
});
/**
* End the game on any err event.
*/
socket.on('err', (data) => {
game.endGame(data.message);
});
}());