-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
68 lines (62 loc) · 1.73 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
class Game {
constructor() {
this.player1 = new Player("p1", "🎤");
this.player2 = new Player("p2", "📼");
this.turn = true;
this.isWinner = false;
this.board = [0,1,2,3,4,5,6,7,8];
this.winCombos = [[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[6,4,2]];
this.winner = [];
}
checkGameCoditions(player) {
for (var i = 0; i < this.winCombos.length; i++) {
if (player.move.includes(this.winCombos[i][0]) &&
player.move.includes(this.winCombos[i][1]) &&
player.move.includes(this.winCombos[i][2])
) {
this.declareWinner(player);
}
}
};
declareWinner(player) {
player.wins++
this.winner.push(player);
player.saveWinsToStorage();
this.isWinner = true;
};
placeIconOnOpenSquare(location) {
if (this.board[location] !== this.player1.icon && this.board[location] !== this.player2.icon) {
this.placePlayerIcon(location);
this.changePlayerTurn();
}
};
placePlayerIcon(location) {
if (this.turn === true) {
this.board.splice(location, 1, this.player1.icon);
this.player1.move.push(parseInt(location));
this.checkGameCoditions(this.player1);
}
if (this.turn === false) {
this.board.splice(location, 1, this.player2.icon);
this.player2.move.push(parseInt(location));
this.checkGameCoditions(this.player2);
}
};
changePlayerTurn() {
if (this.turn === true) {
this.turn = false;
} else if (this.turn === false) {
this.turn = true;
}
}
resetGame() {
currentGame = new Game;
}
}