-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.js
122 lines (110 loc) · 2.99 KB
/
tic-tac-toe.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
112
113
114
115
116
117
118
119
120
121
122
export const marks = {
P1: "X",
P2: "O",
Empty: ".",
};
export const players = {
P1: "player1",
P2: "player2",
};
export const states = {
open: "open",
P1: "player1won",
P2: "player2won",
draw: "draw",
};
export class TicTacToe {
// Saninn's comment: Usually the functions should be defined AFTER the constructor... but ok...
resetGame() {
this.field = [
new Array(3).fill(marks.Empty),
new Array(3).fill(marks.Empty),
new Array(3).fill(marks.Empty),
];
this.currentPlayer = players.P1;
this.currentState = states.open;
console.table(this.field);
}
constructor() {
this.resetGame();
}
_playerHasWon(userMark) {
// returns boolean
if (
(this.field[0][0] === userMark) &
(this.field[0][1] === userMark) &
(this.field[0][2] === userMark) ||
(this.field[1][0] === userMark) &
(this.field[1][1] === userMark) &
(this.field[1][2] === userMark) ||
(this.field[2][0] === userMark) &
(this.field[2][1] === userMark) &
(this.field[2][2] === userMark) ||
(this.field[0][0] === userMark) &
(this.field[1][0] === userMark) &
(this.field[2][0] === userMark) ||
(this.field[0][1] === userMark) &
(this.field[1][1] === userMark) &
(this.field[2][1] === userMark) ||
(this.field[0][2] === userMark) &
(this.field[1][2] === userMark) &
(this.field[2][2] === userMark) ||
(this.field[0][0] === userMark) &
(this.field[1][1] === userMark) &
(this.field[2][2] === userMark) ||
(this.field[2][0] === userMark) &
(this.field[1][1] === userMark) &
(this.field[0][2] === userMark)
) {
return true;
}
return false;
}
_boardIsFull() {
return (
!this.field[0].includes(marks.Empty) &
!this.field[1].includes(marks.Empty) &
!this.field[2].includes(marks.Empty)
);
}
_gameState() {
if (this._playerHasWon(marks.P1)) {
this.currentState = states.P1;
console.log("Player 1 has won.");
return;
}
if (this._playerHasWon(marks.P2)) {
this.currentState = states.P2;
console.log("Player 2 has won.");
return;
}
if (this._boardIsFull()) {
this.currentState = states.draw;
console.log("Draw.");
return;
}
}
play(coord1, coord2) {
const newState = this._move(coord1, coord2);
this._gameState();
return newState;
}
_move(a, b) {
if (this.field[a][b] !== marks.Empty) {
console.log("This square is already occupied");
console.log(this.currentPlayer);
return "This square is already occupied";
}
if (this.currentPlayer == players.P1) {
this.field[a][b] = marks.P1;
this.currentPlayer = players.P2;
console.table(this.field);
console.log(this.currentPlayer);
} else {
this.field[a][b] = marks.P2;
this.currentPlayer = players.P1;
console.table(this.field);
console.log(this.currentPlayer);
}
}
}