-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.js
61 lines (55 loc) · 1.42 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
class TicTacToe {
constructor() {
this.board = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}
boardPrint() {
console.log(
`
${this.board[0]} | ${this.board[1]} | ${this.board[2]}
---------
${this.board[3]} | ${this.board[4]} | ${this.board[5]}
---------
${this.board[6]} | ${this.board[7]} | ${this.board[8]}
`);
};
playerMove(position, mark) {
this.board[position - 1] = mark;
};
validMove(position) {
const boardPosition = position - 1;
const equality = this.board[boardPosition];
return parseInt(position) === equality;
};
checkWin(mark) {
const winSequences = [[1, 2, 3], // horizontal
[4, 5, 6],
[7, 8, 9],
[1, 4, 7], // vertical
[2, 5, 8],
[3, 6, 9],
[1, 5, 9], // diagonal
[3, 5, 7]];
let markCount = 0;
for(let i = 0; i < winSequences.length; i++) {
markCount = 0;
for(let j = 0; j < winSequences[i].length; j++) {
if(this.board[winSequences[i][j] - 1] === mark) {
markCount++;
}
if (markCount === 3) {
return true;
}
}
}
return false;
};
catWins() {
for(let i of this.board) {
if (typeof i === "number") {
return false;
}
}
return true;
};
}
module.exports = TicTacToe;