1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class MadhouseRules
extends ChessRules
{
6 // Testing move validity results in an infinite update loop.
7 // TODO: find a way to test validity anyway.
8 return (this.subTurn
== 2 && this.board
[x
][y
] == V
.EMPTY
);
11 setOtherVariables(fen
) {
12 super.setOtherVariables(fen
);
17 getPotentialMovesFrom([x
, y
]) {
18 if (this.subTurn
== 1) return super.getPotentialMovesFrom([x
, y
]);
19 // subTurn == 2: a move is a click, not handled here
24 if (this.subTurn
== 2) return super.filterValid(moves
);
25 const color
= this.turn
;
26 return moves
.filter(m
=> {
29 if (m
.vanish
.length
== 1 || m
.appear
.length
== 2)
31 res
= !this.underCheck(color
);
33 // Capture: find landing square not resulting in check
34 const boundary
= (m
.vanish
[1].p
!= V
.PAWN
? [0, 7] : [1, 6]);
36 m
.vanish
[1].p
== V
.BISHOP
37 ? (m
.vanish
[1].x
+ m
.vanish
[1].y
) % 2
39 outerLoop: for (let i
= boundary
[0]; i
<= boundary
[1]; i
++) {
40 for (let j
=0; j
<8; j
++) {
42 this.board
[i
][j
] == V
.EMPTY
&&
43 (!sqColor
|| (i
+ j
) % 2 == sqColor
)
45 const tMove
= new Move({
55 start: { x: -1, y: -1 }
58 const moveOk
= !this.underCheck(color
);
74 if (this.subTurn
== 1) return super.getAllValidMoves();
75 // Subturn == 2: only replacements
77 const L
= this.firstMove
.length
;
78 const fm
= this.firstMove
[L
- 1];
79 const color
= this.turn
;
80 const oppCol
= V
.GetOppCol(color
);
81 const boundary
= (fm
.vanish
[1].p
!= V
.PAWN
? [0, 7] : [1, 6]);
83 fm
.vanish
[1].p
== V
.BISHOP
84 ? (fm
.vanish
[1].x
+ fm
.vanish
[1].y
) % 2
86 for (let i
= boundary
[0]; i
< boundary
[1]; i
++) {
87 for (let j
=0; j
<8; j
++) {
89 this.board
[i
][j
] == V
.EMPTY
&&
90 (!sqColor
|| (i
+ j
) % 2 == sqColor
)
92 const tMove
= new Move({
102 start: { x: -1, y: -1 }
105 const moveOk
= !this.underCheck(color
);
107 if (moveOk
) moves
.push(tMove
);
115 if (isNaN(square
[0])) return null;
116 // If subTurn == 2 && square is empty && !underCheck, then replacement
117 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
118 const L
= this.firstMove
.length
;
119 const fm
= this.firstMove
[L
- 1];
120 const color
= this.turn
;
121 const oppCol
= V
.GetOppCol(color
);
123 (fm
.vanish
[1].p
== V
.PAWN
&& [0, 7].includes(square
[0])) ||
125 fm
.vanish
[1].p
== V
.BISHOP
&&
126 (square
[0] + square
[1] + fm
.vanish
[1].x
+ fm
.vanish
[1].y
) % 2 != 0
129 // Pawns cannot be replaced on first or last rank,
130 // bishops must be replaced on same square color.
133 const tMove
= new Move({
143 start: { x: -1, y: -1 }
146 const moveOk
= !this.underCheck(color
);
148 if (moveOk
) return tMove
;
154 move.flags
= JSON
.stringify(this.aggregateFlags());
155 if (move.vanish
.length
> 0) {
156 this.epSquares
.push(this.getEpSquare(move));
157 this.firstMove
.push(move);
159 V
.PlayOnBoard(this.board
, move);
162 move.vanish
.length
== 1 ||
163 move.appear
.length
== 2
165 this.turn
= V
.GetOppCol(this.turn
);
169 else this.subTurn
= 2;
170 if (move.vanish
.length
> 0) this.postPlay(move);
174 if (move.appear
[0].p
== V
.KING
)
175 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
176 this.updateCastleFlags(move, move.appear
[0].p
, move.appear
[0].c
);
180 this.disaggregateFlags(JSON
.parse(move.flags
));
181 if (move.vanish
.length
> 0) {
182 this.epSquares
.pop();
183 this.firstMove
.pop();
185 V
.UndoOnBoard(this.board
, move);
186 if (this.subTurn
== 2) this.subTurn
= 1;
188 this.turn
= V
.GetOppCol(this.turn
);
190 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
192 if (move.vanish
.length
> 0) super.postUndo(move);
196 let moves
= this.getAllValidMoves();
197 if (moves
.length
== 0) return null;
198 // Custom "search" at depth 1 (for now. TODO?)
199 const maxeval
= V
.INFINITY
;
200 const color
= this.turn
;
201 const initEval
= this.evalPosition();
204 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
205 if (m
.vanish
.length
== 2 && m
.appear
.length
== 1) {
206 const moves2
= this.getAllValidMoves();
208 moves2
.forEach(m2
=> {
210 const score
= this.getCurrentScore();
212 if (["1-0", "0-1"].includes(score
))
213 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
214 else if (score
== "*")
215 // Add small fluctuations to avoid dropping pieces always on the
216 // first square available.
217 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
219 (color
== 'w' && mvEval
> m
.eval
) ||
220 (color
== 'b' && mvEval
< m
.eval
)
229 const score
= this.getCurrentScore();
230 if (score
!= "1/2") {
231 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
232 else m
.eval
= this.evalPosition();
237 moves
.sort((a
, b
) => {
238 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
240 let candidates
= [0];
241 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
243 const mIdx
= candidates
[randInt(candidates
.length
)];
244 if (!moves
[mIdx
].next
) return moves
[mIdx
];
245 const move2
= moves
[mIdx
].next
;
246 delete moves
[mIdx
]["next"];
247 return [moves
[mIdx
], move2
];
251 if (move.vanish
.length
> 0) return super.getNotation(move);
254 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
255 return piece
+ "@" + V
.CoordsToSquare(move.end
);