-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
111 lines (96 loc) · 2.7 KB
/
game.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
function Game() {
this.board = [[null, null, null], [null, null, null], [null, null, null]]
this.player1 = "X"
this.player2 = "O"
this.turnCount = 1
}
Game.prototype.currentPlayer = function() {
if (this.turnCount % 2 === 1) {
return this.player1
} else {
return this.player2
}
}
Game.prototype.placeMove = function(spaceIndex) {
var rowNum = Math.floor(spaceIndex / 3);
var columnNum = Math.floor(spaceIndex % 3);
this.board[rowNum][columnNum] = this.currentPlayer();
}
Game.prototype.validMove = function(spaceIndex) {
var rowNum = Math.floor(spaceIndex / 3);
var columnNum = Math.floor(spaceIndex % 3);
return !this.board[rowNum][columnNum]
}
Game.prototype.rowWin = function() {
var that = this;
return that.board.some(function(row) {
return that.checkRow(row) === true
});
}
Game.prototype.columnWin = function() {
var that = this;
var transposedBoard = that.transposeBoard();
return transposedBoard.some(function(row) {
return that.checkRow(row) === true
});
}
Game.prototype.diagonalWin = function() {
if (this.checkUpLeft()) {
return true;
} else if (this.checkDownLeft()) {
return true;
} else {
return false;
}
}
Game.prototype.boardWin = function() {
return this.diagonalWin() || this.columnWin() || this.rowWin();
}
Game.prototype.remainingMoves = function() {
var flatBoard = this.board.reduce(function(a, b) {
return a.concat(b);
});
return flatBoard.some(function(element) {
return element === null;
});
}
Game.prototype.over = function() {
return this.boardWin() || !this.remainingMoves();
}
Game.prototype.reset = function() {
this.board = [[null, null, null], [null, null, null], [null, null, null]]
this.turnCount = 1
}
Game.prototype.checkUpLeft = function() {
return (this.board[0][0] === this.board[1][1] && this.board[1][1] === this.board[2][2]) && !!this.board[0][0]
}
Game.prototype.checkDownLeft = function() {
return (this.board[0][2] === this.board[1][1] && this.board[1][1] === this.board[2][0]) && !!this.board[0][2]
}
Game.prototype.checkRow = function(array) {
return (array[0] === array[1] && array[0] === array[2]) && !!array[0]
}
Game.prototype.status = function(spaceIndex) {
if (this.validMove(spaceIndex)) {
this.placeMove(spaceIndex)
if (this.boardWin()) {
return "win"
} else if (this.remainingMoves()) {
return "continue"
} else {
return "draw"
}
} else {
return "invalid"
}
}
Game.prototype.transposeBoard = function() {
var that = this
var transposedBoard = that.board[0].map(function(col, i) {
var newRow = that.board.map(function(row) {
return row[i]
})
return newRow
})
return transposedBoard;
}