-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.js
52 lines (47 loc) · 1.43 KB
/
board.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
var Board = function(){
this.board = new Array(9);
};
Board.prototype.isValid = function(space){
return this.board[space] == undefined;
}
Board.prototype.makeMove = function(space, symbol){
this.board[space] = symbol;
console.log(space, symbol);
}
Board.prototype.checkWin = function(){
//Iterate through each 3 consecutive spaces
//Check for vertical
for (var i = 0; i <7; i+=3){
//Check if 3 consecutive horizontal spaces are equal
if (this.board[i]!=undefined && this.board[i]==this.board[i+1] && this.board[i+1]==this.board[i+2]){
//Player at i won with horizontal
return this.board[i];
}
}
//Check for vertical
for (var i = 0; i < 3; i++ ){
//Check if 3 consecutive vertical spaces are equal
if (this.board[i]!=undefined && this.board[i]==this.board[i + 3] && this.board[i + 3]==this.board[i+6]){
//Player at i won with vertical
return this.board[i];
}
}
// \__
// _\_
// __\
//Check if 3 diagonal spaces are equal
if (this.board[0]!=undefined && this.board[0]==this.board[4] && this.board[4]==this.board[8]){
//Player at i won with diagonal
return this.board[0];
}
// __/
// _/_
// /__
//Check if 3 diagonal spaces are equal
if (this.board[6]!=undefined && this.board[6]==this.board[4] && this.board[4]==this.board[2]){
//Player at i won with diagonal
return this.board[6];
}
return undefined;
}
module.exports = Board;