/
board.js
297 lines (250 loc) · 7.24 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
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
function GameScore () {
this.lines = {} // {key: {x0, y0, x1, y1}}
this.pairScore = 5
this.tripletScore = 20
this.setMultiplier = 0.2
this.runMultiplier = 0.2
this.runCnt = 0
this.runScore = 0
this.setCnt = 0
this.setScore = 0
}
// @return bool true: линия добавлена, false: линия уже есть
GameScore.prototype.addLine = function (l) {
var key = [l.x0, l.y0, l.x1, l.y1].join(':')
if (this.lines[key]) {
return false
}
this.lines[key] = l
return true
}
// @return bool true: линия добавлена, false: линия уже есть
GameScore.prototype.scorePair = function (line) {
if (!this.addLine(line)) {
return false
}
this.setCnt++
this.setScore += this.pairScore
return true
}
// @return bool true: линия добавлена, false: линия уже есть
GameScore.prototype.scoreTriplet = function (line) {
if (!this.addLine(line)) {
return false
}
this.setCnt++
this.setScore += this.tripletScore
return true
}
// @return bool true: линия добавлена
GameScore.prototype.scoreLine = function (l) {
var dx = Math.abs(l.x0 - l.x1)
var dy = Math.abs(l.y0 - l.y1)
if (dx * dy != 0) return false
switch (Math.max(dx, dy)) {
case 1: return this.scorePair(l)
case 2: return this.scoreTriplet(l)
default: return false
}
}
GameScore.prototype.scoreLines = function (lines) {
for (var i = 0; i < lines.length; i++) {
this.scoreLine(lines[i])
}
}
GameScore.prototype.newSet = function () {
this.runCnt++
this.runScore += this.setScore
this.setScore = 0
this.lines = {}
}
// @return int общий рекорд с коэффициентами
// портит объект!
GameScore.prototype.total = function () {
if (this.setScore) {
this.newSet()
}
return Math.round(this.runScore * (1 + this.setMultiplier * (this.setCnt - 1) + this.runMultiplier * (this.runCnt - 1)))
}
function GameBoard (model, width, height) {
this.model = model
this.width = width
this.height = height
this.board = new Array(width)
this.reset()
}
GameBoard.prototype.reset = function () {
for (var x = 0; x < this.width; x++) {
this.board[x] = new Array(this.height)
}
}
GameBoard.prototype.line = function (x0, y0, x1, y1) {
if (x0 > x1 || y0 > y1) {
var v = x0
x0 = x1
x1 = v
v = y0
y0 = y1
y1 = v
}
return {x0: x0, y0: y0, x1: x1, y1: y1}
}
GameBoard.prototype.setCell = function (x, y, v) {
this.board[x][y] = v
}
GameBoard.prototype.clearCell = function (x, y) {
this.setCell(x, y)
}
// @return int|null
GameBoard.prototype.getCell = function (x, y) {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return null
}
return this.board[x][y]
}
// @return QuarkConstraint
GameBoard.prototype.getConstraint = function (base, x, y) {
var cons = (base ? base.copy() : this.model.getConstraint())
var cd = [[-1, 0], [1, 0], [0, -1]]
var td = [[-2, 0, -1, 0], [-1, 0, 1, 0], [1, 0, 2, 0], [0, -1, 0, -2]]
for (var i = 0; i < cd.length; i++) {
var v = this.getCell(x + cd[i][0], y + cd[i][1])
if (v != undefined) {
cons.exclude(this.model.getPairQuark(v).index)
}
}
for (i = 0; i < td.length; i++) {
v = this.getCell(x + td[i][0], y + td[i][1])
var w = this.getCell(x + td[i][2], y + td[i][3])
if (v == undefined || w == undefined) {
continue
}
var qs = this.model.getTripletQuarks(v, w)
for (var j = 0; j < qs.length; j++) {
cons.exclude(qs[j].index)
}
}
return cons
}
// @return array int (y-координата нижней заполненной клетки в колонке), null (колонка не изменилась)
GameBoard.prototype.fill = function (baseConstraint) {
var lowestFill = new Array(this.width)
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
if (this.getCell(x, y) == undefined) {
this.board[x][y] = this.getConstraint(baseConstraint, x, y).pickRandom()
if (lowestFill[x] == undefined) {
lowestFill[x] = y
}
}
}
}
return lowestFill
}
// @return array int (y-координата нижней измененной клетки в колонке), null (колонка не изменилась)
GameBoard.prototype.compact = function () {
var lowestUpdate = new Array(this.width)
for (var x = 0; x < this.width; x++) {
var col = this.board[x]
var y = 0, t = 1
while (t < this.height) {
while (col[y] != undefined) {
y++
t = y + 1
}
if (lowestUpdate[x] == undefined) {
lowestUpdate[x] = y
}
col[y] = col[t]
col[t] = undefined
t++
}
}
return lowestUpdate
}
GameBoard.prototype.swap = function (x1, y1, x2, y2) {
var v = this.getCell(x1, y1)
var w = this.getCell(x2, y2)
this.board[x1][y1] = w
this.board[x2][y2] = v
}
// @return int[][4] [{x0, y0, x1, y1}*]
GameBoard.prototype.getPairs = function (x, y) {
var result = []
var d = [[0, 1], [-1, 0], [1, 0], [0, -1]]
var u = this.board[x][y]
for (var i = 0; i < d.length; i++) {
var v = this.getCell(x + d[i][0], y + d[i][1])
if (v != undefined && this.model.isPair(u, v)) {
result.push(this.line(x, y, x + d[i][0], y + d[i][1]))
}
}
return result
}
// @return int[][4] [{x0, y0, x1, y1}*]
GameBoard.prototype.getTriplets = function (x, y) {
var result = []
var d = [[-2, 0, -1, 0], [-1, 0, 1, 0], [1, 0, 2, 0], [0, -2, 0, -1], [0, -1, 0, 1], [0, 1, 0, 2]]
var u = this.board[x][y]
for (var i = 0; i < d.length; i++) {
var x1 = x + d[i][0]
var y1 = y + d[i][1]
var x2 = x + d[i][2]
var y2 = y + d[i][3]
var v = this.getCell(x1, y1)
var w = this.getCell(x2, y2)
if (v != undefined && w != undefined && this.model.isTriplet(u, v, w)) {
result.push(this.line(Math.min(x, x1, x2), Math.min(y, y1, y2), Math.max(x, x1, x2), Math.max(y, y1, y2)))
}
}
return result
}
// @return int[][4] [{x0, y0, x1, y1}*]
GameBoard.prototype.getLines = function (x, y) {
return this.getPairs(x, y).concat(this.getTriplets(x, y))
}
GameBoard.prototype.clearLine = function (l) {
this.clearCell(l.x0, l.y0)
this.clearCell(l.x1, l.y1)
this.clearCell((l.x0 + l.x1) >> 1, (l.y0 + l.y1) >> 1)
}
GameBoard.prototype.updateScoreSet = function (score, x, y) {
var lines = this.getPairs(x, y)
for (var i = 0; i < lines.length; i++) {
score.scorePair(lines[i])
}
lines = this.getTriplets(x, y)
for (var i = 0; i < lines.length; i++) {
score.scoreTriplet(lines[i])
}
}
// @return bool
GameBoard.prototype.isScoringMove = function (x1, y1, x2, y2) {
this.swap(x1, y1, x2, y2)
var result = (
this.getPairs(x1, y1).length > 0 ||
this.getPairs(x2, y2).length > 0 ||
this.getTriplets(x1, y1).length > 0 ||
this.getTriplets(x2, y2).length > 0
)
this.swap(x1, y1, x2, y2)
return result
}
// @return bool
GameBoard.prototype.hasScoringMove = function () {
for (var x = 0; x < this.width - 1; x++) {
for (var y = 0; y < this.height; y++) {
if (this.isScoringMove(x, y, x + 1, y)) {
return true
}
}
}
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height - 1; y++) {
if (this.isScoringMove(x, y, x, y + 1)) {
return true
}
}
}
return false
}