-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
62 lines (57 loc) · 1.72 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
class Game {
constructor() {
this.leftPlayer = new Player('O', '🙆');
this.rightPlayer = new Player('X', '🙅');
this.currentPlayer = this.leftPlayer;
this.tie = false;
this.board = [
0,1,2,
3,4,5,
6,7,8,
];
this.winPositions = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]];
}
checkForWins() {
for (var i = 0; i < this.winPositions.length; i++) {
if (this.currentPlayer.positions.includes(this.winPositions[i][0]) &&
this.currentPlayer.positions.includes(this.winPositions[i][1]) &&
this.currentPlayer.positions.includes(this.winPositions[i][2])) {
this.currentPlayer.increaseWins();
this.resetBoard();
return 'WINNER';
}
}
}
resetBoard() {
this.leftPlayer.positions = [];
this.rightPlayer.positions = [];
this.tie = false;
this.board = [
0,1,2,
3,4,5,
6,7,8,
];
}
updateCurrentPlayer() {
if (this.currentPlayer === this.leftPlayer) {
this.currentPlayer = this.rightPlayer;
} else {
this.currentPlayer = this.leftPlayer;
}
}
checkForTie() {
if (this.currentPlayer.positions.length === 5) {
this.tie = true;
this.resetBoard();
return 'TIE';
}
}
choosePosition(indexNumber) {
if (this.board[indexNumber] === "") {
return
} else {
this.board.splice(indexNumber, 1, "")
this.currentPlayer.positions.unshift(indexNumber);
}
}
}