-
Notifications
You must be signed in to change notification settings - Fork 3
/
tictactoe.js
90 lines (80 loc) · 2.04 KB
/
tictactoe.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
// Given a tic tac toe game (in the form of a 2D array), it checks for
// winners
// if there is a winner (could have multiple winning rows), it returns an array of all the winning rows and the player that one
// if there is not a winner, it sends back an empty array
// if there is a tie, it sends back { tied: true }
function checkForWinner(game) {
var xPiece = "X";
var oPiece = "O";
var winningMoves = [];
for (var row = 0; row < game.length; row++) {
var isX = game[row].every(function(val) {
return val.value === xPiece;
});
if (isX) {
winningMoves.push({
player: xPiece,
type: "row",
index: row
});
};
var isO = game[row].every(function(val) {
return val.value === oPiece;
});
if (isO) {
winningMoves.push({
player: oPiece,
type: "row",
index: row
});
};
}
// check columns
for (var col = 0; col < game.length; col++) {
var isX = game.every(function(row) {
return row[col].value === xPiece;
});
if (isX) {
winningMoves.push({
player: xPiece,
type: "column",
index: col
});
}
var isO = game.every(function(row) {
return row[col].value === oPiece;
});
if (isO) {
winningMoves.push({
player: oPiece,
type: "column",
index: col
});
}
}
// check diagonals
var game = game;
if (game[0][0].value != null && game[0][0].value === game[1][1].value && game[1][1].value === game[2][2].value) {
winningMoves.push({
player: game[1][1].value,
type: "diagonal",
index: 0
});
} else if (game[2][0].value != null && game[2][0].value === game[1][1].value && game[1][1].value === game[0][2].value) {
winningMoves.push({
player: game[1][1].value,
type: "diagonal",
index: 1
});
}
var tied = game.every(function(row) {
return row.every(function(cell) {
return cell.value != null;
});
})
if (tied) {
return { tied: true };
} else {
return winningMoves;
}
}