1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class FanoronaRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
14 static get Monochrome() {
20 // Draw all inter-squares lines, shifted:
21 for (let i
= 0; i
< V
.size
.x
; i
++)
22 lines
.push([[i
+0.5, 0.5], [i
+0.5, V
.size
.y
-0.5]]);
23 for (let j
= 0; j
< V
.size
.y
; j
++)
24 lines
.push([[0.5, j
+0.5], [V
.size
.x
-0.5, j
+0.5]]);
26 [[0.5, 0.5], [2.5, 2.5]],
27 [[0.5, 2.5], [2.5, 0.5]],
28 [[2.5, 0.5], [4.5, 2.5]],
29 [[4.5, 0.5], [2.5, 2.5]]
31 for (let j
of [0, 2, 4, 6]) {
33 columnDiags
.map(L
=> [[L
[0][0], L
[0][1] + j
], [L
[1][0], L
[1][1] + j
]])
39 static get Notoodark() {
43 static GenRandInitFen() {
44 return "ppppppppp/ppppppppp/pPpP1pPpP/PPPPPPPPP/PPPPPPPPP w 0";
47 setOtherVariables(fen
) {
48 // Local stack of captures during a turn (squares + directions)
49 this.captures
= [ [] ];
53 return { x: 5, y: 9 };
60 static IsGoodPosition(position
) {
61 if (position
.length
== 0) return false;
62 const rows
= position
.split("/");
63 if (rows
.length
!= V
.size
.x
) return false;
64 for (let row
of rows
) {
66 for (let i
= 0; i
< row
.length
; i
++) {
67 if (row
[i
].toLowerCase() == V
.PAWN
) sumElts
++;
69 const num
= parseInt(row
[i
], 10);
70 if (isNaN(num
) || num
<= 0) return false;
74 if (sumElts
!= V
.size
.y
) return false;
80 return "Fanorona/" + b
;
84 // m.vanish.length >= 2, first capture gives direction
85 const ref
= (Math
.abs(m
.vanish
[1].x
- m
.start
.x
) == 1 ? m
.start : m
.end
);
86 const step
= [m
.vanish
[1].x
- ref
.x
, m
.vanish
[1].y
- ref
.y
];
87 const normalizedStep
= [
88 step
[0] / Math
.abs(step
[0]),
89 step
[1] / Math
.abs(step
[1])
93 (normalizedStep
[0] || 0) + "_" + (normalizedStep
[1] || 0)
97 // After moving, add stones captured in "step" direction from new location
98 // [x, y] to mv.vanish (if any captured stone!)
99 addCapture([x
, y
], step
, move) {
100 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
101 const oppCol
= V
.GetOppCol(move.vanish
[0].c
);
104 this.board
[i
][j
] != V
.EMPTY
&&
105 this.getColor(i
, j
) == oppCol
107 move.vanish
.push(new PiPo({ x: i
, y: j
, c: oppCol
, p: V
.PAWN
}));
111 return (move.vanish
.length
>= 2);
114 getPotentialMovesFrom([x
, y
]) {
115 const L0
= this.captures
.length
;
116 const captures
= this.captures
[L0
- 1];
117 const L
= captures
.length
;
119 var c
= captures
[L
-1];
120 if (x
!= c
.square
.x
+ c
.step
[0] || y
!= c
.square
.y
+ c
.step
[1])
123 const oppCol
= V
.GetOppCol(this.turn
);
124 let steps
= V
.steps
[V
.ROOK
];
125 if ((x
+ y
) % 2 == 0) steps
= steps
.concat(V
.steps
[V
.BISHOP
]);
127 for (let s
of steps
) {
128 if (L
> 0 && c
.step
[0] == s
[0] && c
.step
[1] == s
[1]) {
129 // Add a move to say "I'm done capturing"
134 start: { x: x
, y: y
},
135 end: { x: x
- s
[0], y: y
- s
[1] }
140 let [i
, j
] = [x
+ s
[0], y
+ s
[1]];
141 if (captures
.some(c
=> c
.square
.x
== i
&& c
.square
.y
== j
)) continue;
142 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
143 // The move is potentially allowed. Might lead to 2 different captures
144 let mv
= super.getBasicMove([x
, y
], [i
, j
]);
145 const capt
= this.addCapture([i
, j
], s
, mv
);
148 mv
= super.getBasicMove([x
, y
], [i
, j
]);
150 const capt_bw
= this.addCapture([x
, y
], [-s
[0], -s
[1]], mv
);
151 if (capt_bw
) moves
.push(mv
);
152 // Captures take priority (if available)
153 if (!capt
&& !capt_bw
&& L
== 0) moves
.push(mv
);
159 atLeastOneCapture() {
160 const color
= this.turn
;
161 const oppCol
= V
.GetOppCol(color
);
162 const L0
= this.captures
.length
;
163 const captures
= this.captures
[L0
- 1];
164 const L
= captures
.length
;
166 // If some adjacent enemy stone, with free space to capture it,
167 // toward a square not already visited, through a different step
168 // from last one: then yes.
169 const c
= captures
[L
-1];
170 const [x
, y
] = [c
.square
.x
+ c
.step
[0], c
.square
.y
+ c
.step
[1]];
171 let steps
= V
.steps
[V
.ROOK
];
172 if ((x
+ y
) % 2 == 0) steps
= steps
.concat(V
.steps
[V
.BISHOP
]);
173 // TODO: half of the steps explored are redundant
174 for (let s
of steps
) {
175 if (s
[0] == c
.step
[0] && s
[1] == c
.step
[1]) continue;
176 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
179 this.board
[i
][j
] != V
.EMPTY
||
180 captures
.some(c
=> c
.square
.x
== i
&& c
.square
.y
== j
)
185 V
.OnBoard(i
+ s
[0], j
+ s
[1]) &&
186 this.board
[i
+ s
[0]][j
+ s
[1]] != V
.EMPTY
&&
187 this.getColor(i
+ s
[0], j
+ s
[1]) == oppCol
192 V
.OnBoard(x
- s
[0], y
- s
[1]) &&
193 this.board
[x
- s
[0]][y
- s
[1]] != V
.EMPTY
&&
194 this.getColor(x
- s
[0], y
- s
[1]) == oppCol
201 for (let i
= 0; i
< V
.size
.x
; i
++) {
202 for (let j
= 0; j
< V
.size
.y
; j
++) {
204 this.board
[i
][j
] != V
.EMPTY
&&
205 this.getColor(i
, j
) == color
&&
206 // TODO: this could be more efficient
207 this.getPotentialMovesFrom([i
, j
]).some(m
=> m
.vanish
.length
>= 2)
216 static KeepCaptures(moves
) {
217 return moves
.filter(m
=> m
.vanish
.length
>= 2);
220 getPossibleMovesFrom(sq
) {
221 let moves
= this.getPotentialMovesFrom(sq
);
222 const L0
= this.captures
.length
;
223 const captures
= this.captures
[L0
- 1];
224 if (captures
.length
> 0) return this.getPotentialMovesFrom(sq
);
225 const captureMoves
= V
.KeepCaptures(moves
);
226 if (captureMoves
.length
> 0) return captureMoves
;
227 if (this.atLeastOneCapture()) return [];
232 const moves
= super.getAllValidMoves();
233 if (moves
.some(m
=> m
.vanish
.length
>= 2)) return V
.KeepCaptures(moves
);
246 const color
= this.turn
;
247 move.turn
= color
; //for undo
248 V
.PlayOnBoard(this.board
, move);
249 const L0
= this.captures
.length
;
250 let captures
= this.captures
[L0
- 1];
251 if (move.vanish
.length
>= 2) {
254 step: [move.end
.x
- move.start
.x
, move.end
.y
- move.start
.y
]
256 if (this.atLeastOneCapture())
257 // There could be other captures (optional)
258 // This field is mostly useful for computer play.
259 move.notTheEnd
= true;
260 else captures
.pop(); //useless now
262 if (!move.notTheEnd
) {
263 this.turn
= V
.GetOppCol(color
);
265 this.captures
.push([]);
270 V
.UndoOnBoard(this.board
, move);
271 if (move.turn
!= this.turn
) {
272 this.turn
= move.turn
;
277 const L0
= this.captures
.length
;
278 let captures
= this.captures
[L0
- 1];
284 const color
= this.turn
;
285 // If no stones on board, I lose
287 this.board
.every(b
=> {
288 return b
.every(cell
=> {
289 return (cell
== "" || cell
[0] != color
);
293 return (color
== 'w' ? "0-1" : "1-0");
299 const moves
= this.getAllValidMoves();
300 if (moves
.length
== 0) return null;
301 const color
= this.turn
;
302 // Capture available? If yes, play it
303 let captures
= moves
.filter(m
=> m
.vanish
.length
>= 2);
305 while (captures
.length
>= 1) {
306 // Then just pick random captures (trying to maximize)
307 let candidates
= captures
.filter(c
=> !!c
.notTheEnd
);
309 if (candidates
.length
>= 1) mv
= candidates
[randInt(candidates
.length
)];
310 else mv
= captures
[randInt(captures
.length
)];
313 captures
= (this.turn
== color
? this.getAllValidMoves() : []);
315 if (mvArray
.length
>= 1) {
316 for (let i
= mvArray
.length
- 1; i
>= 0; i
--) this.undo(mvArray
[i
]);
319 // Just play a random move, which if possible does not let a capture
321 for (let m
of moves
) {
323 if (!this.atLeastOneCapture()) candidates
.push(m
);
326 if (candidates
.length
>= 1) return candidates
[randInt(candidates
.length
)];
327 return moves
[randInt(moves
.length
)];
331 if (move.appear
.length
== 0) return "stop";
333 V
.CoordsToSquare(move.start
) +
334 V
.CoordsToSquare(move.end
) +
335 (move.vanish
.length
>= 2 ? "X" : "")