-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.js
343 lines (306 loc) · 9.07 KB
/
field.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const readline = require('readline')
const puyo = require('./puyo.js')
// ぷよが消えるのに必要な連結個数
const clearRequirement = 4
const defaultIntervalMsec = 500
const height = 10
const width = 6
const footer = process.platform === 'darwin' ? '1️⃣ 2️⃣ 3️⃣ 4️⃣ 5️⃣ 6️⃣ ' : '123456'
const sleep = (msec) => {
return new Promise(resolve => setTimeout(resolve, msec))
}
class Field {
static isValidColumn (column) {
return Number.isInteger(column) && column >= 1 && column <= width
}
constructor () {
this.height = height
this.width = width
this.lastChainCount = 0
// ぷよぷよが置かれている行のうち、最も高い行
this.highestRow = height
this.grid = new Array(height)
for (let row = 0; row < height; row++) {
this.grid[row] = new Array(width)
for (let column = 0; column < width; column++) {
this.grid[row][column] = new puyo.Puyo(0)
}
}
}
outputFooter () {
console.log(footer)
}
outputAll () {
// 空行を出力
const emptyLineCount = 2
for (let i = 0; i < emptyLineCount; i++) {
console.log()
}
// ぷよの存在する行のみ上から順に出力
for (let i = this.highestRow; i < this.height; i++) {
this.outputSingleRow(i)
}
}
outputSingleRow (row) {
this.grid[row].forEach(py => {
py.output()
})
console.log()
}
// 置くぷよの種類と列の入力を促す
async promptToPutPuyo () {
console.log(`Put one puyo(${puyo.puyoTypes.slice(1).join('')}) anywhere and connect four same puyos!`)
const column = await this.questionWhereToPut()
const type = await this.questionWhichTypeToPut()
return {
type: type,
column: column - 1
}
}
async questionWhereToPut () {
let column = await this.question(`Which column? (Type a number between 1 and ${width}) `)
while (!Field.isValidColumn(column)) {
column = await this.question(`Invalid number input! Type a number between 1 and ${width} `)
}
return column
}
async questionWhichTypeToPut () {
const typeQuestion = 'Which puyo? ' +
puyo.puyoTypes.slice(1).map((py, index) => `${index + 1}:${py}`).join(', ') +
` (Type a number between 1 and ${puyo.puyoTypes.length - 1}) `
let type = await this.question(typeQuestion)
while (!puyo.Puyo.isValidPuyoType(type)) {
type = await this.question(`Invalid number input! Type a number between 1 and ${puyo.puyoTypes.length - 1} `)
}
return type
}
question (question) {
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise((resolve) => {
readlineInterface.question(question, number => {
resolve(Number(number))
readlineInterface.close()
})
})
}
// 指定のぷよを指定の列に置く
put (type, column) {
const py = new puyo.Puyo(type)
if (this.isSpace(0, column)) {
this.grid[0][column] = py
this.drop(0, column)
}
this.setHighestRow()
}
// 連鎖を開始する
async trigger (intervalMsec = defaultIntervalMsec) {
this.lastChainCount = 0
// ぷよを消去できた場合は、画面出力とぷよ落下を繰り返す
while (this.clearAllConnectedCells()) {
this.lastChainCount++
await sleep(intervalMsec)
this.outputAll()
if (this.isAllCleared()) {
break
}
await sleep(intervalMsec)
this.dropAll()
this.outputAll()
}
this.setHighestRow()
if (this.isAllCleared()) {
return '🎉Congratulations! You cleared all puyos!!👏'
} else {
return 'Hmm, there seems to remain some puyos in the field. Let\'s try again!'
}
}
// 画面出力なしで連鎖を開始する
triggerWithoutOutput () {
this.lastChainCount = 0
while (this.clearAllConnectedCells()) {
this.lastChainCount++
this.dropAll()
}
this.setHighestRow()
}
// フィールド全体を確認し、連結ぷよ数が clearRequirement 以上のぷよを消去する
clearAllConnectedCells () {
let cleared = false
this.uncheckAll()
for (let row = 0; row < this.height; row++) {
for (let column = 0; column < this.width; column++) {
// 消去確認不要のセルは確認しない
if (!this.needToCheck(row, column)) {
continue
}
this.check(row, column)
const allConnectedCells = this.getAllConnectedCells(row, column)
if (allConnectedCells.length >= clearRequirement) {
this.clearCells(allConnectedCells)
cleared = true
}
}
}
// いずれかのぷよを消去した場合 true を返す
return cleared
}
// 同じ種類のぷよで連結している全てのセルを返す
getAllConnectedCells (row, column) {
const queue = [
[row, column]
]
const allConnectedCells = [
[row, column]
]
// BFS で連結しているセルを探索する
while (queue.length > 0) {
const [currentRow, currentColumn] = queue.shift()
const neighboringConnectedCells = this.getNeighboringConnectedCells(currentRow, currentColumn)
queue.push(...neighboringConnectedCells)
allConnectedCells.push(...neighboringConnectedCells)
}
return allConnectedCells
}
// 上下左右の隣接するセルのうち、未確認かつ同じ種類のぷよがいるセルを返す
getNeighboringConnectedCells (row, column) {
const neighboringConnectedCells = []
const puyoType = this.getPuyoType(row, column)
this.getNeighboringCells(row, column).forEach(neighbor => {
const [neighborRow, neighborColumn] = neighbor
if (this.needToCheck(neighborRow, neighborColumn) && puyoType === this.getPuyoType(neighborRow, neighborColumn)) {
neighboringConnectedCells.push([neighborRow, neighborColumn])
// 連結するセルの場合はチェック済にする
this.check(neighborRow, neighborColumn)
}
})
return neighboringConnectedCells
}
// 上下左右の隣接するセルを返す
getNeighboringCells (row, column) {
const neighboringCells = []
if (row > 0) {
neighboringCells.push([row - 1, column])
}
if (row < this.height - 1) {
neighboringCells.push([row + 1, column])
}
if (column > 0) {
neighboringCells.push([row, column - 1])
}
if (column < this.width - 1) {
neighboringCells.push([row, column + 1])
}
return neighboringCells
}
clearCells (cells) {
cells.forEach(cell => {
const [row, column] = cell
this.clear(row, column)
})
}
uncheckAll () {
for (let i = 0; i < this.height; i++) {
this.uncheckSingleRow(i)
}
}
uncheckSingleRow (row) {
this.grid[row].forEach(py => {
py.uncheck()
})
}
// 下の行から順に落下処理をする
dropAll () {
for (let row = this.height - 1; row >= 0; row--) {
this.dropSingleRow(row)
}
}
dropSingleRow (row) {
for (let column = 0; column < this.width; column++) {
this.drop(row, column)
}
}
// ぷよを落下させる
drop (row, column) {
const landingRow = this.getLandingRow(row, column)
if (landingRow === row) {
return
}
this.grid[landingRow][column].type = this.grid[row][column].type
this.grid[row][column].clear()
}
// ぷよの落下行を返す
getLandingRow (row, column) {
let landingRow = row
while (landingRow + 1 < this.height && this.isSpace(landingRow + 1, column)) {
landingRow++
}
return landingRow
}
// 指定行、列を任意のタイプに変える
changeType (row, column, type) {
this.grid[row][column].type = type
}
isAllCleared () {
return Array.from(Array(this.height).keys()).every(row => {
return this.isSingleRowCleared(row)
})
}
isPuyoRemaining () {
return !this.isAllCleared()
}
// highestRowを更新する
setHighestRow () {
for (let row = 0; row < height; row++) {
if (!this.isSingleRowCleared(row)) {
this.highestRow = row
return
}
}
this.highestRow = this.height
}
isSingleRowCleared (row) {
return this.grid[row].every(py => {
return py.isSpace()
})
}
copy () {
const copy = new Field()
for (let row = 0; row < height; row++) {
for (let column = 0; column < width; column++) {
copy.grid[row][column] = new puyo.Puyo(this.getPuyoType(row, column))
}
}
copy.highestRow = this.highestRow
return copy
}
countSingleRowPuyos (row) {
return this.grid[row].filter(py => {
return !py.isSpace()
}).length
}
getPuyoType (row, column) {
return this.grid[row][column].type
}
needToCheck (row, column) {
return this.grid[row][column].needToCheck()
}
check (row, column) {
this.grid[row][column].check()
}
clear (row, column) {
this.grid[row][column].clear()
}
isSpace (row, column) {
return this.grid[row][column].isSpace()
}
}
module.exports = {
Field: Field,
height: height,
width: width,
clearRequirement: clearRequirement,
sleep: sleep
}