-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_view.js
65 lines (57 loc) · 1.48 KB
/
game_view.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
var coordinates;
function currentPlayerColor(player) {
if (player === "X") {
return "red"
} else if (player === "O") {
return "blue"
}
}
function renderMove(spaceIndex, player) {
$(".turn").eq(spaceIndex).html(player)
$(".turn").eq(spaceIndex).css("background-color", currentPlayerColor(player))
}
function colorWin(coordinates) {
coordinates.forEach(function(element) {
$(".turn").eq(element).css("background-color", "green");
});
}
function renderRowWin(game) {
game.board.forEach(function(row) {
if (game.checkRow(row)) {
spaceIndex = game.board.indexOf(row) * 3
colorWin([spaceIndex, spaceIndex + 1, spaceIndex + 2])
}
});
}
function renderColumnWin(game) {
var transposedBoard = game.transposeBoard();
transposedBoard.forEach(function(column) {
if (game.checkRow(column)) {
spaceIndex = transposedBoard.indexOf(column)
colorWin([spaceIndex, spaceIndex + 3, spaceIndex + 6])
}
});
}
function renderDiagonalWin(game) {
if (game.checkUpLeft()) {
coordinates = [0, 4, 8]
} else if (game.checkDownLeft()) {
coordinates = [2, 4, 6]
}
colorWin(coordinates);
}
function renderWin(game) {
if (game.diagonalWin()) {
renderDiagonalWin(game);
} else if (game.rowWin()) {
renderRowWin(game);
} else if (game.columnWin()) {
renderColumnWin(game);
};
$(".replay").toggle();
};
function resetBoard(game) {
$(".turn").empty();
$(".turn").css("background-color", "white");
$(".replay").toggle();
}