1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class HiddenRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
14 // Analyse in Hidden mode makes no sense
15 static get CanAnalyze() {
19 // Moves are revealed only when game ends, but are highlighted on board
20 static get ShowMoves() {
24 static get HIDDEN_DECODE() {
34 static get HIDDEN_CODE() {
45 // Turn a hidden piece or revealed piece into revealed piece:
47 if (Object
.keys(V
.HIDDEN_DECODE
).includes(p
))
48 return V
.HIDDEN_DECODE
[p
];
53 return ChessRules
.PIECES
.concat(Object
.keys(V
.HIDDEN_DECODE
));
56 // Pieces can be hidden :)
58 const piece
= this.board
[i
][j
].charAt(1);
59 if (Object
.keys(V
.HIDDEN_DECODE
).includes(piece
))
60 return V
.HIDDEN_DECODE
[piece
];
64 getPpath(b
, color
, score
) {
65 if (Object
.keys(V
.HIDDEN_DECODE
).includes(b
[1])) {
66 // Supposed to be hidden.
67 if (score
== "*" && (!color
|| color
!= b
[0]))
68 return "Hidden/" + b
[0] + "p";
69 // Else: condition OK to show the piece
70 return b
[0] + V
.HIDDEN_DECODE
[b
[1]];
72 // The piece is already not supposed to be hidden:
76 // Scan board for kings positions (no castling)
78 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
79 const fenRows
= V
.ParseFen(fen
).position
.split("/");
80 for (let i
= 0; i
< fenRows
.length
; i
++) {
81 let k
= 0; //column index on board
82 for (let j
= 0; j
< fenRows
[i
].length
; j
++) {
83 switch (fenRows
[i
].charAt(j
)) {
86 this.kingPos
["b"] = [i
, k
];
90 this.kingPos
["w"] = [i
, k
];
93 const num
= parseInt(fenRows
[i
].charAt(j
));
94 if (!isNaN(num
)) k
+= num
- 1;
102 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
105 Object
.keys(V
.HIDDEN_DECODE
).includes(this.board
[sx
][sy
].charAt(1))
107 // The transformed piece is a priori hidden
108 tr
.p
= V
.HIDDEN_CODE
[tr
.p
];
115 c: tr
? tr
.c : this.getColor(sx
, sy
),
116 p: tr
? tr
.p : this.board
[sx
][sy
].charAt(1)
123 c: this.getColor(sx
, sy
),
124 p: this.board
[sx
][sy
].charAt(1)
129 // The opponent piece disappears if we take it
130 if (this.board
[ex
][ey
] != V
.EMPTY
) {
135 c: this.getColor(ex
, ey
),
136 p: this.board
[ex
][ey
].charAt(1)
139 // Pieces are revealed when they capture
140 mv
.appear
[0].p
= V
.Decode(mv
.appear
[0].p
);
150 // Ignore randomness here: placement is always random asymmetric
151 static GenRandInitFen() {
152 let pieces
= { w: new Array(8), b: new Array(8) };
153 // Shuffle pieces + pawns on two first ranks
154 for (let c
of ["w", "b"]) {
155 let positions
= ArrayFun
.range(16);
157 // Get random squares for bishops
158 let randIndex
= 2 * randInt(8);
159 const bishop1Pos
= positions
[randIndex
];
160 // The second bishop must be on a square of different color
161 let randIndex_tmp
= 2 * randInt(8) + 1;
162 const bishop2Pos
= positions
[randIndex_tmp
];
163 // Remove chosen squares
164 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
165 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
167 // Get random squares for knights
168 randIndex
= randInt(14);
169 const knight1Pos
= positions
[randIndex
];
170 positions
.splice(randIndex
, 1);
171 randIndex
= randInt(13);
172 const knight2Pos
= positions
[randIndex
];
173 positions
.splice(randIndex
, 1);
175 // Get random squares for rooks
176 randIndex
= randInt(12);
177 const rook1Pos
= positions
[randIndex
];
178 positions
.splice(randIndex
, 1);
179 randIndex
= randInt(11);
180 const rook2Pos
= positions
[randIndex
];
181 positions
.splice(randIndex
, 1);
183 // Get random square for queen
184 randIndex
= randInt(10);
185 const queenPos
= positions
[randIndex
];
186 positions
.splice(randIndex
, 1);
188 // Get random square for king
189 randIndex
= randInt(9);
190 const kingPos
= positions
[randIndex
];
191 positions
.splice(randIndex
, 1);
193 // Pawns position are all remaining slots:
194 for (let p
of positions
)
197 // Finally put the shuffled pieces in the board array
198 pieces
[c
][rook1Pos
] = "u";
199 pieces
[c
][knight1Pos
] = "o";
200 pieces
[c
][bishop1Pos
] = "c";
201 pieces
[c
][queenPos
] = "t";
202 pieces
[c
][kingPos
] = "l";
203 pieces
[c
][bishop2Pos
] = "c";
204 pieces
[c
][knight2Pos
] = "o";
205 pieces
[c
][rook2Pos
] = "u";
207 let upFen
= pieces
["b"].join("");
208 upFen
= upFen
.substr(0,8) + "/" + upFen
.substr(8).split("").reverse().join("");
209 let downFen
= pieces
["b"].join("").toUpperCase();
210 downFen
= downFen
.substr(0,8) + "/" + downFen
.substr(8).split("").reverse().join("");
211 return upFen
+ "/8/8/8/8/" + downFen
+ " w 0";
219 super.postPlay(move);
221 move.vanish
.length
>= 2 &&
222 [V
.KING
,V
.HIDDEN_CODE
[V
.KING
]].includes(move.vanish
[1].p
)
224 // We took opponent king
225 this.kingPos
[this.turn
] = [-1, -1];
230 super.postUndo(move);
231 const c
= move.vanish
[0].c
;
232 const oppCol
= V
.GetOppCol(c
);
233 if (this.kingPos
[oppCol
][0] < 0)
234 // Last move took opponent's king:
235 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
239 const color
= this.turn
;
240 const kp
= this.kingPos
[color
];
243 return color
== "w" ? "0-1" : "1-0";
244 // Assume that stalemate is impossible:
249 const color
= this.turn
;
250 let moves
= this.getAllValidMoves();
251 for (let move of moves
) {
252 move.eval
= 0; //a priori...
254 // Can I take something ? If yes, do it with some probability
255 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= color
) {
256 // OK this isn't a castling move
257 const myPieceVal
= V
.VALUES
[move.appear
[0].p
];
258 const hisPieceVal
= Object
.keys(V
.HIDDEN_DECODE
).includes(move.vanish
[1].p
)
260 : V
.VALUES
[move.vanish
[1].p
];
262 // Opponent's piece is unknown: do not take too much risk
263 move.eval
= -myPieceVal
+ 1.5; //so that pawns always take
266 else if (myPieceVal
<= hisPieceVal
)
267 move.eval
= hisPieceVal
- myPieceVal
+ 1;
269 // Taking a pawn with minor piece,
270 // or minor piece or pawn with a rook,
271 // or anything but a queen with a queen,
272 // or anything with a king.
273 move.eval
= hisPieceVal
- myPieceVal
;
276 // If no capture, favor small step moves,
277 // but sometimes move the knight anyway
278 const penalty
= V
.Decode(move.vanish
[0].p
) != V
.KNIGHT
279 ? Math
.abs(move.end
.x
- move.start
.x
) + Math
.abs(move.end
.y
- move.start
.y
)
280 : (Math
.random() < 0.5 ? 3 : 1);
281 move.eval
-= penalty
/ (V
.size
.x
+ V
.size
.y
- 1);
284 // TODO: also favor movements toward the center?
287 moves
.sort((a
, b
) => b
.eval
- a
.eval
);
288 let candidates
= [0];
289 for (let j
= 1; j
< moves
.length
&& moves
[j
].eval
== moves
[0].eval
; j
++)
291 return moves
[candidates
[randInt(candidates
.length
)]];
295 // Translate final square
296 const finalSquare
= V
.CoordsToSquare(move.end
);
298 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
299 if (piece
== V
.PAWN
) {
302 if (move.vanish
.length
> move.appear
.length
) {
304 const startColumn
= V
.CoordToColumn(move.start
.y
);
305 notation
= startColumn
+ "x" + finalSquare
;
307 else notation
= finalSquare
;
308 if (move.appear
.length
> 0 && !["p","s"].includes(move.appear
[0].p
)) {
310 const appearPiece
= V
.Decode(move.appear
[0].p
);
311 notation
+= "=" + appearPiece
.toUpperCase();
317 piece
.toUpperCase() +
318 (move.vanish
.length
> move.appear
.length
? "x" : "") +