1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export const VariantRules
= 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
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
.values(V
.HIDDEN_CODE
));
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 // Scan board for kings positions (no castling)
66 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
67 const fenRows
= V
.ParseFen(fen
).position
.split("/");
68 for (let i
= 0; i
< fenRows
.length
; i
++) {
69 let k
= 0; //column index on board
70 for (let j
= 0; j
< fenRows
[i
].length
; j
++) {
71 switch (fenRows
[i
].charAt(j
)) {
74 this.kingPos
["b"] = [i
, k
];
78 this.kingPos
["w"] = [i
, k
];
81 const num
= parseInt(fenRows
[i
].charAt(j
));
82 if (!isNaN(num
)) k
+= num
- 1;
90 getPpath(b
, color
, score
) {
91 if (Object
.keys(V
.HIDDEN_DECODE
).includes(b
[1])) {
92 // Supposed to be hidden.
93 if (score
== "*" && (!color
|| color
!= b
[0]))
94 return "Hidden/" + b
[0] + "p";
95 // Else: condition OK to show the piece
96 return b
[0] + V
.HIDDEN_DECODE
[b
[1]];
98 // The piece is already not supposed to be hidden:
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
);
146 // What are the king moves from square x,y ?
147 getPotentialKingMoves(sq
) {
149 return this.getSlideNJumpMoves(
151 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
160 static GenRandInitFen() {
161 let pieces
= { w: new Array(8), b: new Array(8) };
162 // Shuffle pieces + pawns on two first ranks
163 for (let c
of ["w", "b"]) {
164 let positions
= ArrayFun
.range(16);
166 // Get random squares for bishops
167 let randIndex
= 2 * randInt(8);
168 const bishop1Pos
= positions
[randIndex
];
169 // The second bishop must be on a square of different color
170 let randIndex_tmp
= 2 * randInt(8) + 1;
171 const bishop2Pos
= positions
[randIndex_tmp
];
172 // Remove chosen squares
173 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
174 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
176 // Get random squares for knights
177 randIndex
= randInt(14);
178 const knight1Pos
= positions
[randIndex
];
179 positions
.splice(randIndex
, 1);
180 randIndex
= randInt(13);
181 const knight2Pos
= positions
[randIndex
];
182 positions
.splice(randIndex
, 1);
184 // Get random squares for rooks
185 randIndex
= randInt(12);
186 const rook1Pos
= positions
[randIndex
];
187 positions
.splice(randIndex
, 1);
188 randIndex
= randInt(11);
189 const rook2Pos
= positions
[randIndex
];
190 positions
.splice(randIndex
, 1);
192 // Get random square for queen
193 randIndex
= randInt(10);
194 const queenPos
= positions
[randIndex
];
195 positions
.splice(randIndex
, 1);
197 // Get random square for king
198 randIndex
= randInt(9);
199 const kingPos
= positions
[randIndex
];
200 positions
.splice(randIndex
, 1);
202 // Pawns position are all remaining slots:
203 for (let p
of positions
)
206 // Finally put the shuffled pieces in the board array
207 pieces
[c
][rook1Pos
] = "u";
208 pieces
[c
][knight1Pos
] = "o";
209 pieces
[c
][bishop1Pos
] = "c";
210 pieces
[c
][queenPos
] = "t";
211 pieces
[c
][kingPos
] = "l";
212 pieces
[c
][bishop2Pos
] = "c";
213 pieces
[c
][knight2Pos
] = "o";
214 pieces
[c
][rook2Pos
] = "u";
216 let upFen
= pieces
["b"].join("");
217 upFen
= upFen
.substr(0,8) + "/" + upFen
.substr(8).split("").reverse().join("");
218 let downFen
= pieces
["b"].join("").toUpperCase();
219 downFen
= downFen
.substr(0,8) + "/" + downFen
.substr(8).split("").reverse().join("");
220 return upFen
+ "/8/8/8/8/" + downFen
+ " w 0";
227 updateVariables(move) {
228 super.updateVariables(move);
230 move.vanish
.length
>= 2 &&
231 [V
.KING
,V
.HIDDEN_CODE
[V
.KING
]].includes(move.vanish
[1].p
)
233 // We took opponent king
234 this.kingPos
[this.turn
] = [-1, -1];
238 unupdateVariables(move) {
239 super.unupdateVariables(move);
240 const c
= move.vanish
[0].c
;
241 const oppCol
= V
.GetOppCol(c
);
242 if (this.kingPos
[oppCol
][0] < 0)
243 // Last move took opponent's king:
244 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
248 const color
= this.turn
;
249 const kp
= this.kingPos
[color
];
252 return color
== "w" ? "0-1" : "1-0";
253 // Assume that stalemate is impossible:
258 const color
= this.turn
;
259 let moves
= this.getAllValidMoves();
260 for (let move of moves
) {
261 move.eval
= 0; //a priori...
263 // Can I take something ? If yes, do it with some probability
264 if (move.vanish
.length
== 2 && move.vanish
[1].c
!= color
) {
265 // OK this isn't a castling move
266 const myPieceVal
= V
.VALUES
[move.appear
[0].p
];
267 const hisPieceVal
= Object
.keys(V
.HIDDEN_DECODE
).includes(move.vanish
[1].p
)
269 : V
.VALUES
[move.vanish
[1].p
];
271 // Opponent's piece is unknown: do not take too much risk
272 move.eval
= -myPieceVal
+ 1.5; //so that pawns always take
275 else if (myPieceVal
<= hisPieceVal
)
276 move.eval
= hisPieceVal
- myPieceVal
+ 1;
278 // Taking a pawn with minor piece,
279 // or minor piece or pawn with a rook,
280 // or anything but a queen with a queen,
281 // or anything with a king.
282 move.eval
= hisPieceVal
- myPieceVal
;
285 // If no capture, favor small step moves,
286 // but sometimes move the knight anyway
287 const penalty
= V
.Decode(move.vanish
[0].p
) != V
.KNIGHT
288 ? Math
.abs(move.end
.x
- move.start
.x
) + Math
.abs(move.end
.y
- move.start
.y
)
289 : (Math
.random() < 0.5 ? 3 : 1);
290 move.eval
-= penalty
/ (V
.size
.x
+ V
.size
.y
- 1);
293 // TODO: also favor movements toward the center?
296 moves
.sort((a
, b
) => b
.eval
- a
.eval
);
297 let candidates
= [0];
298 for (let j
= 1; j
< moves
.length
&& moves
[j
].eval
== moves
[0].eval
; j
++)
300 return moves
[candidates
[randInt(candidates
.length
)]];
304 // Translate final square
305 const finalSquare
= V
.CoordsToSquare(move.end
);
307 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
308 if (piece
== V
.PAWN
) {
311 if (move.vanish
.length
> move.appear
.length
) {
313 const startColumn
= V
.CoordToColumn(move.start
.y
);
314 notation
= startColumn
+ "x" + finalSquare
;
316 else notation
= finalSquare
;
317 if (move.appear
.length
> 0 && !["p","s"].includes(move.appear
[0].p
)) {
319 const appearPiece
= V
.Decode(move.appear
[0].p
);
320 notation
+= "=" + appearPiece
.toUpperCase();
326 piece
.toUpperCase() +
327 (move.vanish
.length
> move.appear
.length
? "x" : "") +