-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.js
164 lines (146 loc) · 5.61 KB
/
Board.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const clone2D = require("./helpers/clone2D")
const X = 'X'
const O = 'O'
const E = ''
const SIZE = 19
const emptyBoard = [
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
[E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,],
]
function isValidPosition(n) {
return n >= 0 && n < SIZE
}
function clampPosition(n) {
return Math.max(0, Math.min(n, SIZE - 1))
}
class Board {
constructor({ player1, player2 }) {
this.player1 = player1
this.player2 = player2
this.data = clone2D(emptyBoard)
const randomNumber = Math.floor(Math.random() * 2)
this.xPlayer = randomNumber === 1 ? player1 : player2
this.oPlayer = randomNumber === 0 ? player1 : player2
this.currentTurn = this.xPlayer
this.winner = null
this.loser = null
this.moveCount = 0
this.timeoutHandler = null
}
addMove({ playerId, r, c }) {
if (this.currentTurn !== playerId) return false
if (this.data[r][c] !== E) return false
const stone = this.getCurrentStone()
this.data[r][c] = stone
this.moveCount++
if (this.isDraw()) return true
this.checkWinner({ stone, r, c })
// console.log(`Winner ${this.winner}`)
if (this.winner) return true
this.switchTurn()
return true
}
isDraw() {
return this.moveCount === SIZE * SIZE
}
checkWinner({ stone, r, c }) {
if (!this.hasWin({ stone, originR: r, originC: c })) return
this.winner = this.currentTurn
this.loser = this.winner === this.player1 ? this.player2 : this.player1
}
getCurrentStone() {
return this.currentTurn === this.xPlayer ? X : O
}
switchTurn() {
this.currentTurn = this.currentTurn === this.xPlayer ? this.oPlayer : this.xPlayer
}
hasWin({ originR, originC, stone }) {
return this.hasWinByRow({ originR, originC, stone }) || this.hasWinByColumn({ originR, originC, stone }) || this.hasWinByMainDiagonal({ originR, originC, stone }) || this.hasWinByCounterDiagonal({ originR, originC, stone })
}
hasWinByRow({ originR, originC, stone }) {
const colStart = clampPosition(originC - 4)
const colEnd = clampPosition(originC + 4)
let currentStreak = 0
let maxStreak = 0
for (let c = colStart; c <= colEnd; c++) {
// Reset to 0 if the cell is not the desired stone
if (this.data[originR][c] !== stone) {
maxStreak = Math.max(currentStreak, maxStreak)
currentStreak = 0
continue
}
currentStreak++
}
maxStreak = Math.max(currentStreak, maxStreak)
return maxStreak === 5
}
hasWinByColumn({ originR, originC, stone }) {
const rowStart = clampPosition(originR - 4)
const rowEnd = clampPosition(originR + 4)
let currentStreak = 0
let maxStreak = 0
for (let r = rowStart; r <= rowEnd; r++) {
// Reset to 0 if the cell is not the desired stone
if (this.data[r][originC] !== stone) {
maxStreak = Math.max(currentStreak, maxStreak)
currentStreak = 0
continue
}
currentStreak++
}
maxStreak = Math.max(currentStreak, maxStreak)
return maxStreak === 5
}
hasWinByMainDiagonal({ originR, originC, stone }) {
let currentStreak = 0
let maxStreak = 0
for (let i = -4; i <= 4; i++) {
const r = originR + i
const c = originC + i
if (!isValidPosition(r) || !isValidPosition(c)) continue
if (this.data[r][c] !== stone) {
maxStreak = Math.max(currentStreak, maxStreak)
currentStreak = 0
continue
}
currentStreak++
}
maxStreak = Math.max(currentStreak, maxStreak)
return maxStreak === 5
}
hasWinByCounterDiagonal({ originR, originC, stone }) {
let currentStreak = 0
let maxStreak = 0
for (let i = -4; i <= 4; i++) {
const r = originR + i
const c = originC - i
if (!isValidPosition(r) || !isValidPosition(c)) continue
if (this.data[r][c] !== stone) {
maxStreak = Math.max(currentStreak, maxStreak)
currentStreak = 0
continue
}
currentStreak++
}
maxStreak = Math.max(currentStreak, maxStreak)
return maxStreak === 5
}
}
module.exports = { Board, X, O }