-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
50 lines (40 loc) · 1.24 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
$(document).ready(function() {
const board = new Board();
const player = new Player(board, 'X');
const computer = new Computer(board, 'O');
function showWinner(result) {
renderBoard(board);
let text = "";
if (result === 'O') {
text = "OOPS! YOU LOST.";
} else if (result === 'X') {
text = "NOT GOING TO HAPPEN"
}
$('.winner-text').text(text);
$('.game-over').css('visibility', 'visible');
}
function showTie() {
renderBoard(board);
$('.winner-text').text("CAT'S GAME");
$('.game-over').css('visibility', 'visible');
}
function renderBoard(board) {
const spaces = board.getSpaces();
for (let row = 0; row < 3; row += 1) {
for (let col = 0; col < 3; col += 1) {
$('.square[data-row=' + row + '][data-col=' + col + ']').html(spaces[row][col]);
}
}
}
$('.square').click(function() {
const row = $(this).data('row');
const col = $(this).data('col');
player.move(row, col);
});
board.addEventListener(Board.MOVE_EVENT, renderBoard.bind(this, board));
board.addEventListener(Board.WINNING_EVENT, showWinner);
board.addEventListener(Board.TIE_EVENT, showTie);
$('#restart').click(function() {
location.reload();
});
});