Commit | Line | Data |
---|---|---|
107dc1bd | 1 | import { ChessRules } from "@/base_rules"; |
5c50b18e BA |
2 | import { randInt, shuffle } from "@/utils/alea"; |
3 | import { ArrayFun } from "@/utils/array"; | |
107dc1bd BA |
4 | |
5 | export class FootballRules extends ChessRules { | |
7e8a7ea1 | 6 | |
4f3a0823 BA |
7 | static get HasEnpassant() { |
8 | return false; | |
9 | } | |
10 | ||
107dc1bd BA |
11 | static get HasFlags() { |
12 | return false; | |
13 | } | |
14 | ||
4f3a0823 BA |
15 | static get size() { |
16 | return { x: 9, y: 9 }; | |
107dc1bd BA |
17 | } |
18 | ||
19 | static get Lines() { | |
20 | return [ | |
21 | // White goal: | |
4f3a0823 | 22 | [[0, 4], [0, 5]], |
107dc1bd | 23 | [[0, 5], [1, 5]], |
4f3a0823 | 24 | [[1, 4], [0, 4]], |
107dc1bd | 25 | // Black goal: |
4f3a0823 BA |
26 | [[9, 4], [9, 5]], |
27 | [[9, 5], [8, 5]], | |
28 | [[8, 4], [9, 4]] | |
107dc1bd BA |
29 | ]; |
30 | } | |
31 | ||
4f3a0823 BA |
32 | static get BALL() { |
33 | // 'b' is already taken: | |
34 | return "aa"; | |
35 | } | |
36 | ||
37 | // Check that exactly one ball is on the board | |
38 | // + at least one piece per color. | |
107dc1bd BA |
39 | static IsGoodPosition(position) { |
40 | if (position.length == 0) return false; | |
41 | const rows = position.split("/"); | |
42 | if (rows.length != V.size.x) return false; | |
107dc1bd | 43 | let pieces = { "w": 0, "b": 0 }; |
4f3a0823 | 44 | let ballCount = 0; |
107dc1bd BA |
45 | for (let row of rows) { |
46 | let sumElts = 0; | |
47 | for (let i = 0; i < row.length; i++) { | |
48 | const lowerRi = row[i].toLowerCase(); | |
4f3a0823 BA |
49 | if (!!lowerRi.match(/^[a-z]$/)) { |
50 | if (V.PIECES.includes(lowerRi)) | |
51 | pieces[row[i] == lowerRi ? "b" : "w"]++; | |
52 | else if (lowerRi == 'a') ballCount++; | |
53 | else return false; | |
107dc1bd | 54 | sumElts++; |
0f7762c1 BA |
55 | } |
56 | else { | |
e50a8025 | 57 | const num = parseInt(row[i], 10); |
107dc1bd BA |
58 | if (isNaN(num)) return false; |
59 | sumElts += num; | |
60 | } | |
61 | } | |
62 | if (sumElts != V.size.y) return false; | |
63 | } | |
4f3a0823 BA |
64 | if (ballCount != 1 || Object.values(pieces).some(v => v == 0)) |
65 | return false; | |
107dc1bd BA |
66 | return true; |
67 | } | |
68 | ||
4f3a0823 BA |
69 | static board2fen(b) { |
70 | if (b == V.BALL) return 'a'; | |
71 | return ChessRules.board2fen(b); | |
72 | } | |
107dc1bd | 73 | |
4f3a0823 BA |
74 | static fen2board(f) { |
75 | if (f == 'a') return V.BALL; | |
76 | return ChessRules.fen2board(f); | |
77 | } | |
78 | ||
79 | getPpath(b) { | |
80 | if (b == V.BALL) return "Football/ball"; | |
81 | return b; | |
82 | } | |
83 | ||
84 | canIplay(side, [x, y]) { | |
85 | return ( | |
86 | side == this.turn && | |
87 | (this.board[x][y] == V.BALL || this.getColor(x, y) == side) | |
88 | ); | |
89 | } | |
90 | ||
91 | // No checks or king tracking etc. But, track ball | |
92 | setOtherVariables() { | |
93 | // Stack of "kicked by" coordinates, to avoid infinite loops | |
94 | this.kickedBy = [ {} ]; | |
95 | this.subTurn = 1; | |
96 | this.ballPos = [-1, -1]; | |
97 | for (let i=0; i < V.size.x; i++) { | |
98 | for (let j=0; j< V.size.y; j++) { | |
99 | if (this.board[i][j] == V.BALL) { | |
100 | this.ballPos = [i, j]; | |
101 | return; | |
102 | } | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
4313762d BA |
107 | static GenRandInitFen(options) { |
108 | if (options.randomness == 0) | |
4f3a0823 BA |
109 | return "rnbq1knbr/9/9/9/4a4/9/9/9/RNBQ1KNBR w 0"; |
110 | ||
4f3a0823 BA |
111 | let pieces = { w: new Array(8), b: new Array(8) }; |
112 | for (let c of ["w", "b"]) { | |
4313762d | 113 | if (c == 'b' && options.randomness == 1) { |
4f3a0823 BA |
114 | pieces['b'] = pieces['w']; |
115 | break; | |
116 | } | |
117 | ||
118 | // Get random squares for every piece, totally freely | |
119 | let positions = shuffle(ArrayFun.range(8)); | |
120 | const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; | |
5d74fcea BA |
121 | // Fix bishops (on different colors) |
122 | const realOddity = | |
123 | (pos) => { return (pos <= 3 ? pos % 2 : (pos + 1) % 2); }; | |
124 | const rem2 = realOddity(positions[0]); | |
125 | if (rem2 == realOddity(positions[1])) { | |
4f3a0823 | 126 | for (let i=2; i<8; i++) { |
5d74fcea | 127 | if (realOddity(positions[i]) != rem2) { |
4f3a0823 | 128 | [positions[1], positions[i]] = [positions[i], positions[1]]; |
5d74fcea BA |
129 | break; |
130 | } | |
4f3a0823 BA |
131 | } |
132 | } | |
133 | for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i]; | |
134 | } | |
135 | const piecesB = pieces["b"].join("") ; | |
136 | const piecesW = pieces["w"].join("").toUpperCase(); | |
137 | return ( | |
138 | piecesB.substr(0, 4) + "1" + piecesB.substr(4) + | |
139 | "/9/9/9/4a4/9/9/9/" + | |
140 | piecesW.substr(0, 4) + "1" + piecesW.substr(4) + | |
141 | " w 0" | |
142 | ); | |
143 | } | |
144 | ||
145 | tryKickFrom([x, y]) { | |
146 | const bp = this.ballPos; | |
147 | const emptySquare = (i, j) => { | |
148 | return V.OnBoard(i, j) && this.board[i][j] == V.EMPTY; | |
149 | }; | |
150 | // Kick the (adjacent) ball from x, y with current turn: | |
151 | const step = [bp[0] - x, bp[1] - y]; | |
152 | const piece = this.getPiece(x, y); | |
153 | let moves = []; | |
154 | if (piece == V.KNIGHT) { | |
155 | // The knight case is particular | |
156 | V.steps[V.KNIGHT].forEach(s => { | |
157 | const [i, j] = [bp[0] + s[0], bp[1] + s[1]]; | |
158 | if ( | |
159 | V.OnBoard(i, j) && | |
160 | this.board[i][j] == V.EMPTY && | |
161 | ( | |
162 | // In a corner? The, allow all ball moves | |
163 | ([0, 8].includes(bp[0]) && [0, 8].includes(bp[1])) || | |
164 | // Do not end near the knight | |
165 | (Math.abs(i - x) >= 2 || Math.abs(j - y) >= 2) | |
166 | ) | |
167 | ) { | |
168 | moves.push(super.getBasicMove(bp, [i, j])); | |
169 | } | |
170 | }); | |
171 | } | |
172 | else { | |
173 | let compatible = false, | |
174 | oneStep = false; | |
175 | switch (piece) { | |
176 | case V.ROOK: | |
177 | compatible = (step[0] == 0 || step[1] == 0); | |
178 | break; | |
179 | case V.BISHOP: | |
180 | compatible = (step[0] != 0 && step[1] != 0); | |
181 | break; | |
182 | case V.QUEEN: | |
183 | compatible = true; | |
184 | break; | |
185 | case V.KING: | |
186 | compatible = true; | |
187 | oneStep = true; | |
188 | break; | |
189 | } | |
190 | if (!compatible) return []; | |
191 | let [i, j] = [bp[0] + step[0], bp[1] + step[1]]; | |
192 | const horizontalStepOnGoalRow = | |
193 | ([0, 8].includes(bp[0]) && step.some(s => s == 0)); | |
5c6c52ac BA |
194 | if ( |
195 | emptySquare(i, j) && | |
196 | (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && | |
197 | (!horizontalStepOnGoalRow || j != 4) | |
198 | ) { | |
4f3a0823 BA |
199 | moves.push(super.getBasicMove(bp, [i, j])); |
200 | if (!oneStep) { | |
201 | do { | |
202 | i += step[0]; | |
203 | j += step[1]; | |
204 | if (!emptySquare(i, j)) break; | |
5c6c52ac BA |
205 | if ( |
206 | (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && | |
207 | (!horizontalStepOnGoalRow || j != 4) | |
208 | ) { | |
4f3a0823 | 209 | moves.push(super.getBasicMove(bp, [i, j])); |
5c6c52ac BA |
210 | } |
211 | } while (true); | |
212 | } | |
213 | } | |
214 | // Try the other direction (TODO: experimental) | |
215 | [i, j] = [bp[0] - 2*step[0], bp[1] - 2*step[1]]; | |
216 | if ( | |
217 | emptySquare(i, j) && | |
218 | (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && | |
219 | (!horizontalStepOnGoalRow || j != 4) | |
220 | ) { | |
221 | moves.push(super.getBasicMove(bp, [i, j])); | |
222 | if (!oneStep) { | |
223 | do { | |
224 | i -= step[0]; | |
225 | j -= step[1]; | |
226 | if (!emptySquare(i, j)) break; | |
227 | if ( | |
228 | (this.movesCount >= 2 || j != 4 || ![0, 8].includes(i)) && | |
229 | (!horizontalStepOnGoalRow || j != 4) | |
230 | ) { | |
231 | moves.push(super.getBasicMove(bp, [i, j])); | |
232 | } | |
4f3a0823 BA |
233 | } while (true); |
234 | } | |
235 | } | |
236 | } | |
237 | const kickedFrom = x + "-" + y; | |
de9e4580 | 238 | moves.forEach(m => m.start.by = kickedFrom) |
107dc1bd BA |
239 | return moves; |
240 | } | |
241 | ||
4f3a0823 | 242 | getPotentialMovesFrom([x, y], computer) { |
5c6c52ac BA |
243 | const piece = this.getPiece(x, y); |
244 | if (V.PIECES.includes(piece)) { | |
4f3a0823 | 245 | if (this.subTurn > 1) return []; |
5c6c52ac BA |
246 | const moves = super.getPotentialMovesFrom([x, y]) |
247 | .filter(m => m.end.y != 4 || ![0, 8].includes(m.end.x)); | |
248 | // If bishop stuck in a corner: allow to jump over the next obstacle | |
249 | if (moves.length == 0 && piece == V.BISHOP) { | |
250 | if ( | |
251 | x == 0 && y == 0 && | |
252 | this.board[1][1] != V.EMPTY && | |
253 | this.board[2][2] == V.EMPTY | |
254 | ) { | |
255 | return [super.getBasicMove([x, y], [2, 2])]; | |
256 | } | |
257 | if ( | |
258 | x == 0 && y == 8 && | |
259 | this.board[1][7] != V.EMPTY && | |
260 | this.board[2][6] == V.EMPTY | |
261 | ) { | |
262 | return [super.getBasicMove([x, y], [2, 6])]; | |
263 | } | |
264 | if ( | |
265 | x == 8 && y == 0 && | |
266 | this.board[7][1] != V.EMPTY && | |
267 | this.board[6][2] == V.EMPTY | |
268 | ) { | |
269 | return [super.getBasicMove([x, y], [6, 2])]; | |
270 | } | |
271 | if ( | |
272 | x == 8 && y == 8 && | |
273 | this.board[7][7] != V.EMPTY && | |
274 | this.board[6][6] == V.EMPTY | |
275 | ) { | |
276 | return [super.getBasicMove([x, y], [6, 6])]; | |
277 | } | |
278 | } | |
279 | return moves; | |
4f3a0823 BA |
280 | } |
281 | // Kicking the ball: look for adjacent pieces. | |
282 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
283 | const c = this.turn; | |
284 | let moves = []; | |
2cfc3b91 | 285 | let kicks = {}; |
4f3a0823 BA |
286 | for (let s of steps) { |
287 | const [i, j] = [x + s[0], y + s[1]]; | |
288 | if ( | |
289 | V.OnBoard(i, j) && | |
290 | this.board[i][j] != V.EMPTY && | |
291 | this.getColor(i, j) == c | |
292 | ) { | |
2cfc3b91 BA |
293 | const kmoves = this.tryKickFrom([i, j]); |
294 | kmoves.forEach(km => { | |
295 | const key = V.CoordsToSquare(km.start) + V.CoordsToSquare(km.end); | |
296 | if (!kicks[km]) { | |
297 | moves.push(km); | |
298 | kicks[km] = true; | |
299 | } | |
300 | }); | |
4f3a0823 BA |
301 | } |
302 | } | |
2cfc3b91 | 303 | if (Object.keys(kicks).length > 0) { |
4a3cbf1d BA |
304 | // And, always add the "end" move. For computer, keep only one |
305 | outerLoop: for (let i=0; i < V.size.x; i++) { | |
306 | for (let j=0; j < V.size.y; j++) { | |
307 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == c) { | |
308 | moves.push(super.getBasicMove([x, y], [i, j])); | |
309 | if (!!computer) break outerLoop; | |
310 | } | |
4f3a0823 BA |
311 | } |
312 | } | |
313 | } | |
314 | return moves; | |
315 | } | |
316 | ||
4313762d BA |
317 | canTake() { |
318 | return false; | |
4f3a0823 BA |
319 | } |
320 | ||
321 | // Extra arg "computer" to avoid trimming all redundant pass moves: | |
322 | getAllPotentialMoves(computer) { | |
323 | const color = this.turn; | |
324 | let potentialMoves = []; | |
325 | for (let i = 0; i < V.size.x; i++) { | |
326 | for (let j = 0; j < V.size.y; j++) { | |
327 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { | |
328 | Array.prototype.push.apply( | |
329 | potentialMoves, | |
330 | this.getPotentialMovesFrom([i, j], computer) | |
331 | ); | |
332 | } | |
333 | } | |
334 | } | |
335 | return potentialMoves; | |
336 | } | |
337 | ||
338 | getAllValidMoves() { | |
339 | return this.filterValid(this.getAllPotentialMoves("computer")); | |
340 | } | |
341 | ||
342 | filterValid(moves) { | |
343 | const L = this.kickedBy.length; | |
344 | const kb = this.kickedBy[L-1]; | |
de9e4580 | 345 | return moves.filter(m => !m.start.by || !kb[m.start.by]); |
4f3a0823 BA |
346 | } |
347 | ||
107dc1bd BA |
348 | getCheckSquares() { |
349 | return []; | |
350 | } | |
351 | ||
4f3a0823 BA |
352 | allowAnotherPass(color) { |
353 | // Two cases: a piece moved, or the ball moved. | |
354 | // In both cases, check our pieces and ball proximity, | |
355 | // so the move played doesn't matter (if ball position updated) | |
356 | const bp = this.ballPos; | |
357 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
358 | for (let s of steps) { | |
359 | const [i, j] = [this.ballPos[0] + s[0], this.ballPos[1] + s[1]]; | |
360 | if ( | |
361 | V.OnBoard(i, j) && | |
362 | this.board[i][j] != V.EMPTY && | |
363 | this.getColor(i, j) == color | |
364 | ) { | |
365 | return true; //potentially... | |
366 | } | |
367 | } | |
368 | return false; | |
369 | } | |
370 | ||
371 | prePlay(move) { | |
372 | if (move.appear[0].p == 'a') | |
373 | this.ballPos = [move.appear[0].x, move.appear[0].y]; | |
374 | } | |
375 | ||
376 | play(move) { | |
377 | // Special message saying "passes are over" | |
378 | const passesOver = (move.vanish.length == 2); | |
379 | if (!passesOver) { | |
380 | this.prePlay(move); | |
381 | V.PlayOnBoard(this.board, move); | |
382 | } | |
383 | move.turn = [this.turn, this.subTurn]; //easier undo | |
384 | if (passesOver || !this.allowAnotherPass(this.turn)) { | |
385 | this.turn = V.GetOppCol(this.turn); | |
386 | this.subTurn = 1; | |
387 | this.movesCount++; | |
388 | this.kickedBy.push( {} ); | |
389 | } | |
390 | else { | |
391 | this.subTurn++; | |
de9e4580 | 392 | if (!!move.start.by) { |
4f3a0823 | 393 | const L = this.kickedBy.length; |
de9e4580 | 394 | this.kickedBy[L-1][move.start.by] = true; |
4f3a0823 BA |
395 | } |
396 | } | |
397 | } | |
398 | ||
399 | undo(move) { | |
400 | const passesOver = (move.vanish.length == 2); | |
401 | if (move.turn[0] != this.turn) { | |
402 | [this.turn, this.subTurn] = move.turn; | |
403 | this.movesCount--; | |
404 | this.kickedBy.pop(); | |
405 | } | |
406 | else { | |
407 | this.subTurn--; | |
de9e4580 | 408 | if (!!move.start.by) { |
4f3a0823 | 409 | const L = this.kickedBy.length; |
de9e4580 | 410 | delete this.kickedBy[L-1][move.start.by]; |
4f3a0823 BA |
411 | } |
412 | } | |
413 | if (!passesOver) { | |
414 | V.UndoOnBoard(this.board, move); | |
415 | this.postUndo(move); | |
416 | } | |
417 | } | |
418 | ||
419 | postUndo(move) { | |
420 | if (move.vanish[0].p == 'a') | |
421 | this.ballPos = [move.vanish[0].x, move.vanish[0].y]; | |
422 | } | |
107dc1bd BA |
423 | |
424 | getCurrentScore() { | |
4f3a0823 BA |
425 | if (this.board[0][4] == V.BALL) return "1-0"; |
426 | if (this.board[8][4] == V.BALL) return "0-1"; | |
427 | return "*"; | |
107dc1bd | 428 | } |
b9ce3d0f | 429 | |
4f3a0823 BA |
430 | getComputerMove() { |
431 | let initMoves = this.getAllValidMoves(); | |
432 | if (initMoves.length == 0) return null; | |
433 | let moves = JSON.parse(JSON.stringify(initMoves)); | |
434 | let mvArray = []; | |
435 | let mv = null; | |
436 | // Just play random moves (for now at least. TODO?) | |
437 | const c = this.turn; | |
438 | while (moves.length > 0) { | |
439 | mv = moves[randInt(moves.length)]; | |
440 | mvArray.push(mv); | |
441 | this.play(mv); | |
442 | if (mv.vanish.length == 1 && this.allowAnotherPass(c)) | |
443 | // Potential kick | |
444 | moves = this.getPotentialMovesFrom(this.ballPos); | |
445 | else break; | |
446 | } | |
447 | for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); | |
448 | return (mvArray.length > 1 ? mvArray : mvArray[0]); | |
449 | } | |
450 | ||
451 | // NOTE: evalPosition() is wrong, but unused since bot plays at random | |
452 | ||
453 | getNotation(move) { | |
454 | if (move.vanish.length == 2) return "pass"; | |
455 | if (move.vanish[0].p != 'a') return super.getNotation(move); | |
456 | // Kick: simple notation (TODO?) | |
457 | return V.CoordsToSquare(move.end); | |
b9ce3d0f | 458 | } |
7e8a7ea1 | 459 | |
107dc1bd | 460 | }; |