1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class MadhouseRules
extends ChessRules
{
7 // Testing move validity results in an infinite update loop.
8 // TODO: find a way to test validity anyway.
9 return (this.subTurn
== 2 && this.board
[x
][y
] == V
.EMPTY
);
12 setOtherVariables(fen
) {
13 super.setOtherVariables(fen
);
18 getPotentialMovesFrom([x
, y
]) {
19 if (this.subTurn
== 1) return super.getPotentialMovesFrom([x
, y
]);
20 // subTurn == 2: a move is a click, not handled here
25 if (this.subTurn
== 2) return super.filterValid(moves
);
26 const color
= this.turn
;
27 return moves
.filter(m
=> {
30 if (m
.vanish
.length
== 1 || m
.appear
.length
== 2)
32 res
= !this.underCheck(color
);
34 // Capture: find landing square not resulting in check
35 const boundary
= (m
.vanish
[1].p
!= V
.PAWN
? [0, 7] : [1, 6]);
37 m
.vanish
[1].p
== V
.BISHOP
38 ? (m
.vanish
[1].x
+ m
.vanish
[1].y
) % 2
40 outerLoop: for (let i
= boundary
[0]; i
<= boundary
[1]; i
++) {
41 for (let j
=0; j
<8; j
++) {
43 this.board
[i
][j
] == V
.EMPTY
&&
44 (!sqColor
|| (i
+ j
) % 2 == sqColor
)
46 const tMove
= new Move({
56 start: { x: -1, y: -1 }
59 const moveOk
= !this.underCheck(color
);
75 if (this.subTurn
== 1) return super.getAllValidMoves();
76 // Subturn == 2: only replacements
78 const L
= this.firstMove
.length
;
79 const fm
= this.firstMove
[L
- 1];
80 const color
= this.turn
;
81 const oppCol
= V
.GetOppCol(color
);
82 const boundary
= (fm
.vanish
[1].p
!= V
.PAWN
? [0, 7] : [1, 6]);
84 fm
.vanish
[1].p
== V
.BISHOP
85 ? (fm
.vanish
[1].x
+ fm
.vanish
[1].y
) % 2
87 for (let i
= boundary
[0]; i
< boundary
[1]; i
++) {
88 for (let j
=0; j
<8; j
++) {
90 this.board
[i
][j
] == V
.EMPTY
&&
91 (!sqColor
|| (i
+ j
) % 2 == sqColor
)
93 const tMove
= new Move({
103 start: { x: -1, y: -1 }
106 const moveOk
= !this.underCheck(color
);
108 if (moveOk
) moves
.push(tMove
);
116 if (isNaN(square
[0])) return null;
117 // If subTurn == 2 && square is empty && !underCheck, then replacement
118 if (this.subTurn
== 2 && this.board
[square
[0]][square
[1]] == V
.EMPTY
) {
119 const L
= this.firstMove
.length
;
120 const fm
= this.firstMove
[L
- 1];
121 const color
= this.turn
;
122 const oppCol
= V
.GetOppCol(color
);
124 (fm
.vanish
[1].p
== V
.PAWN
&& [0, 7].includes(square
[0])) ||
126 fm
.vanish
[1].p
== V
.BISHOP
&&
127 (square
[0] + square
[1] + fm
.vanish
[1].x
+ fm
.vanish
[1].y
) % 2 != 0
130 // Pawns cannot be replaced on first or last rank,
131 // bishops must be replaced on same square color.
134 const tMove
= new Move({
144 start: { x: -1, y: -1 }
147 const moveOk
= !this.underCheck(color
);
149 if (moveOk
) return tMove
;
155 move.flags
= JSON
.stringify(this.aggregateFlags());
156 if (move.vanish
.length
> 0) {
157 this.epSquares
.push(this.getEpSquare(move));
158 this.firstMove
.push(move);
160 V
.PlayOnBoard(this.board
, move);
163 move.vanish
.length
== 1 ||
164 move.appear
.length
== 2
166 this.turn
= V
.GetOppCol(this.turn
);
170 else this.subTurn
= 2;
171 if (move.vanish
.length
> 0) this.postPlay(move);
175 if (move.appear
[0].p
== V
.KING
)
176 this.kingPos
[move.appear
[0].c
] = [move.appear
[0].x
, move.appear
[0].y
];
177 this.updateCastleFlags(move, move.appear
[0].p
, move.appear
[0].c
);
181 this.disaggregateFlags(JSON
.parse(move.flags
));
182 if (move.vanish
.length
> 0) {
183 this.epSquares
.pop();
184 this.firstMove
.pop();
186 V
.UndoOnBoard(this.board
, move);
187 if (this.subTurn
== 2) this.subTurn
= 1;
189 this.turn
= V
.GetOppCol(this.turn
);
191 this.subTurn
= (move.vanish
.length
> 0 ? 1 : 2);
193 if (move.vanish
.length
> 0) super.postUndo(move);
197 let moves
= this.getAllValidMoves();
198 if (moves
.length
== 0) return null;
199 // Custom "search" at depth 1 (for now. TODO?)
200 const maxeval
= V
.INFINITY
;
201 const color
= this.turn
;
202 const initEval
= this.evalPosition();
205 m
.eval
= (color
== "w" ? -1 : 1) * maxeval
;
206 if (m
.vanish
.length
== 2 && m
.appear
.length
== 1) {
207 const moves2
= this.getAllValidMoves();
209 moves2
.forEach(m2
=> {
211 const score
= this.getCurrentScore();
213 if (["1-0", "0-1"].includes(score
))
214 mvEval
= (score
== "1-0" ? 1 : -1) * maxeval
;
215 else if (score
== "*")
216 // Add small fluctuations to avoid dropping pieces always on the
217 // first square available.
218 mvEval
= initEval
+ 0.05 - Math
.random() / 10;
220 (color
== 'w' && mvEval
> m
.eval
) ||
221 (color
== 'b' && mvEval
< m
.eval
)
230 const score
= this.getCurrentScore();
231 if (score
!= "1/2") {
232 if (score
!= "*") m
.eval
= (score
== "1-0" ? 1 : -1) * maxeval
;
233 else m
.eval
= this.evalPosition();
238 moves
.sort((a
, b
) => {
239 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
241 let candidates
= [0];
242 for (let i
= 1; i
< moves
.length
&& moves
[i
].eval
== moves
[0].eval
; i
++)
244 const mIdx
= candidates
[randInt(candidates
.length
)];
245 if (!moves
[mIdx
].next
) return moves
[mIdx
];
246 const move2
= moves
[mIdx
].next
;
247 delete moves
[mIdx
]["next"];
248 return [moves
[mIdx
], move2
];
252 if (move.vanish
.length
> 0) return super.getNotation(move);
255 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
256 return piece
+ "@" + V
.CoordsToSquare(move.end
);