1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class FanoronaRules
extends ChessRules
{
10 static get HasFlags() {
14 static get HasEnpassant() {
18 static get Monochrome() {
24 // Draw all inter-squares lines, shifted:
25 for (let i
= 0; i
< V
.size
.x
; i
++)
26 lines
.push([[i
+0.5, 0.5], [i
+0.5, V
.size
.y
-0.5]]);
27 for (let j
= 0; j
< V
.size
.y
; j
++)
28 lines
.push([[0.5, j
+0.5], [V
.size
.x
-0.5, j
+0.5]]);
30 [[0.5, 0.5], [2.5, 2.5]],
31 [[0.5, 2.5], [2.5, 0.5]],
32 [[2.5, 0.5], [4.5, 2.5]],
33 [[4.5, 0.5], [2.5, 2.5]]
35 for (let j
of [0, 2, 4, 6]) {
37 columnDiags
.map(L
=> [[L
[0][0], L
[0][1] + j
], [L
[1][0], L
[1][1] + j
]])
43 static get Notoodark() {
47 static GenRandInitFen() {
48 return "ppppppppp/ppppppppp/pPpP1pPpP/PPPPPPPPP/PPPPPPPPP w 0";
51 setOtherVariables(fen
) {
52 // Local stack of captures during a turn (squares + directions)
53 this.captures
= [ [] ];
57 return { x: 5, y: 9 };
64 static IsGoodPosition(position
) {
65 if (position
.length
== 0) return false;
66 const rows
= position
.split("/");
67 if (rows
.length
!= V
.size
.x
) return false;
68 for (let row
of rows
) {
70 for (let i
= 0; i
< row
.length
; i
++) {
71 if (row
[i
].toLowerCase() == V
.PAWN
) sumElts
++;
73 const num
= parseInt(row
[i
], 10);
74 if (isNaN(num
) || num
<= 0) return false;
78 if (sumElts
!= V
.size
.y
) return false;
84 return "Fanorona/" + b
;
87 getPPpath(m
, orientation
) {
88 // m.vanish.length >= 2, first capture gives direction
89 const ref
= (Math
.abs(m
.vanish
[1].x
- m
.start
.x
) == 1 ? m
.start : m
.end
);
90 const step
= [m
.vanish
[1].x
- ref
.x
, m
.vanish
[1].y
- ref
.y
];
91 const multStep
= (orientation
== 'w' ? 1 : -1);
92 const normalizedStep
= [
93 multStep
* step
[0] / Math
.abs(step
[0]),
94 multStep
* step
[1] / Math
.abs(step
[1])
98 (normalizedStep
[0] || 0) + "_" + (normalizedStep
[1] || 0)
102 // After moving, add stones captured in "step" direction from new location
103 // [x, y] to mv.vanish (if any captured stone!)
104 addCapture([x
, y
], step
, move) {
105 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
106 const oppCol
= V
.GetOppCol(move.vanish
[0].c
);
109 this.board
[i
][j
] != V
.EMPTY
&&
110 this.getColor(i
, j
) == oppCol
112 move.vanish
.push(new PiPo({ x: i
, y: j
, c: oppCol
, p: V
.PAWN
}));
116 return (move.vanish
.length
>= 2);
119 getPotentialMovesFrom([x
, y
]) {
120 const L0
= this.captures
.length
;
121 const captures
= this.captures
[L0
- 1];
122 const L
= captures
.length
;
124 var c
= captures
[L
-1];
125 if (x
!= c
.square
.x
+ c
.step
[0] || y
!= c
.square
.y
+ c
.step
[1])
128 const oppCol
= V
.GetOppCol(this.turn
);
129 let steps
= V
.steps
[V
.ROOK
];
130 if ((x
+ y
) % 2 == 0) steps
= steps
.concat(V
.steps
[V
.BISHOP
]);
132 for (let s
of steps
) {
133 if (L
> 0 && c
.step
[0] == s
[0] && c
.step
[1] == s
[1]) {
134 // Add a move to say "I'm done capturing"
139 start: { x: x
, y: y
},
140 end: { x: x
- s
[0], y: y
- s
[1] }
145 let [i
, j
] = [x
+ s
[0], y
+ s
[1]];
146 if (captures
.some(c
=> c
.square
.x
== i
&& c
.square
.y
== j
)) continue;
147 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
148 // The move is potentially allowed. Might lead to 2 different captures
149 let mv
= super.getBasicMove([x
, y
], [i
, j
]);
150 const capt
= this.addCapture([i
, j
], s
, mv
);
153 mv
= super.getBasicMove([x
, y
], [i
, j
]);
155 const capt_bw
= this.addCapture([x
, y
], [-s
[0], -s
[1]], mv
);
156 if (capt_bw
) moves
.push(mv
);
157 // Captures take priority (if available)
158 if (!capt
&& !capt_bw
&& L
== 0) moves
.push(mv
);
164 atLeastOneCapture() {
165 const color
= this.turn
;
166 const oppCol
= V
.GetOppCol(color
);
167 const L0
= this.captures
.length
;
168 const captures
= this.captures
[L0
- 1];
169 const L
= captures
.length
;
171 // If some adjacent enemy stone, with free space to capture it,
172 // toward a square not already visited, through a different step
173 // from last one: then yes.
174 const c
= captures
[L
-1];
175 const [x
, y
] = [c
.square
.x
+ c
.step
[0], c
.square
.y
+ c
.step
[1]];
176 let steps
= V
.steps
[V
.ROOK
];
177 if ((x
+ y
) % 2 == 0) steps
= steps
.concat(V
.steps
[V
.BISHOP
]);
178 // TODO: half of the steps explored are redundant
179 for (let s
of steps
) {
180 if (s
[0] == c
.step
[0] && s
[1] == c
.step
[1]) continue;
181 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
184 this.board
[i
][j
] != V
.EMPTY
||
185 captures
.some(c
=> c
.square
.x
== i
&& c
.square
.y
== j
)
190 V
.OnBoard(i
+ s
[0], j
+ s
[1]) &&
191 this.board
[i
+ s
[0]][j
+ s
[1]] != V
.EMPTY
&&
192 this.getColor(i
+ s
[0], j
+ s
[1]) == oppCol
197 V
.OnBoard(x
- s
[0], y
- s
[1]) &&
198 this.board
[x
- s
[0]][y
- s
[1]] != V
.EMPTY
&&
199 this.getColor(x
- s
[0], y
- s
[1]) == oppCol
206 for (let i
= 0; i
< V
.size
.x
; i
++) {
207 for (let j
= 0; j
< V
.size
.y
; j
++) {
209 this.board
[i
][j
] != V
.EMPTY
&&
210 this.getColor(i
, j
) == color
&&
211 // TODO: this could be more efficient
212 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
>= 2)
221 static KeepCaptures(moves
) {
222 return moves
.filter(m
=> m
.vanish
.length
>= 2);
225 getPossibleMovesFrom(sq
) {
226 let moves
= this.getPotentialMovesFrom(sq
);
227 const L0
= this.captures
.length
;
228 const captures
= this.captures
[L0
- 1];
229 if (captures
.length
> 0) return this.getPotentialMovesFrom(sq
);
230 const captureMoves
= V
.KeepCaptures(moves
);
231 if (captureMoves
.length
> 0) return captureMoves
;
232 if (this.atLeastOneCapture()) return [];
237 const moves
= super.getAllValidMoves();
238 if (moves
.some(m
=> m
.vanish
.length
>= 2)) return V
.KeepCaptures(moves
);
251 const color
= this.turn
;
252 move.turn
= color
; //for undo
253 V
.PlayOnBoard(this.board
, move);
254 if (move.vanish
.length
>= 2) {
255 const L0
= this.captures
.length
;
256 let captures
= this.captures
[L0
- 1];
259 step: [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
]
261 if (this.atLeastOneCapture())
262 // There could be other captures (optional)
263 move.notTheEnd
= true;
265 if (!move.notTheEnd
) {
266 this.turn
= V
.GetOppCol(color
);
268 this.captures
.push([]);
273 V
.UndoOnBoard(this.board
, move);
274 if (!move.notTheEnd
) {
275 this.turn
= move.turn
;
279 if (move.vanish
.length
>= 2) {
280 const L0
= this.captures
.length
;
281 let captures
= this.captures
[L0
- 1];
287 const color
= this.turn
;
288 // If no stones on board, I lose
290 this.board
.every(b
=> {
291 return b
.every(cell
=> {
292 return (cell
== "" || cell
[0] != color
);
296 return (color
== 'w' ? "0-1" : "1-0");
302 const moves
= this.getAllValidMoves();
303 if (moves
.length
== 0) return null;
304 const color
= this.turn
;
305 // Capture available? If yes, play it
306 let captures
= moves
.filter(m
=> m
.vanish
.length
>= 2);
308 while (captures
.length
>= 1) {
309 // Then just pick random captures (trying to maximize)
310 let candidates
= captures
.filter(c
=> !!c
.notTheEnd
);
312 if (candidates
.length
>= 1) mv
= candidates
[randInt(candidates
.length
)];
313 else mv
= captures
[randInt(captures
.length
)];
316 captures
= (this.turn
== color
? this.getAllValidMoves() : []);
318 if (mvArray
.length
>= 1) {
319 for (let i
= mvArray
.length
- 1; i
>= 0; i
--) this.undo(mvArray
[i
]);
322 // Just play a random move, which if possible does not let a capture
324 for (let m
of moves
) {
326 if (!this.atLeastOneCapture()) candidates
.push(m
);
329 if (candidates
.length
>= 1) return candidates
[randInt(candidates
.length
)];
330 return moves
[randInt(moves
.length
)];
334 if (move.appear
.length
== 0) return "stop";
336 V
.CoordsToSquare(move.start
) +
337 V
.CoordsToSquare(move.end
) +
338 (move.vanish
.length
>= 2 ? "X" : "")