f1607a6a5cb490e74e6df23f3d241815e0d9953d
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class HiddenqueenRules
extends ChessRules
{
6 // Analyse in Hiddenqueen mode makes no sense
7 static get CanAnalyze() {
11 static get HIDDEN_QUEEN() {
15 static get SomeHiddenMoves() {
20 return ChessRules
.PIECES
.concat([V
.HIDDEN_QUEEN
]);
24 const piece
= this.board
[i
][j
].charAt(1);
26 piece
!= V
.HIDDEN_QUEEN
||
27 // 'side' is used to determine what I see: a pawn or a (hidden)queen?
28 this.getColor(i
, j
) == this.side
35 getPpath(b
, color
, score
) {
36 if (b
[1] == V
.HIDDEN_QUEEN
) {
37 // Supposed to be hidden.
38 if (score
== "*" && (!color
|| color
!= b
[0]))
40 return "Hiddenqueen/" + b
[0] + "t";
45 getEpSquare(moveOrSquare
) {
46 if (!moveOrSquare
) return undefined;
47 if (typeof moveOrSquare
=== "string") {
48 const square
= moveOrSquare
;
49 if (square
== "-") return undefined;
50 return V
.SquareToCoords(square
);
52 const move = moveOrSquare
;
55 const color
= move.vanish
[0].c
;
58 Math
.abs(s
.x
- e
.x
) == 2 &&
59 ((color
== 'w' && s
.x
== 6) || (color
== 'b' && s
.x
== 1)) &&
60 [V
.PAWN
, V
.HIDDEN_QUEEN
].includes(move.vanish
[0].p
)
67 return undefined; //default
70 isValidPawnMove(move) {
71 const color
= move.vanish
[0].c
;
72 const pawnShift
= color
== "w" ? -1 : 1;
73 const startRank
= color
== "w" ? V
.size
.x
- 2 : 1;
76 move.end
.x
- move.start
.x
== pawnShift
&&
80 move.end
.y
== move.start
.y
&&
81 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
86 Math
.abs(move.end
.y
- move.start
.y
) == 1 &&
87 this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
93 // Two-spaces initial jump
94 move.start
.x
== startRank
&&
95 move.end
.y
== move.start
.y
&&
96 move.end
.x
- move.start
.x
== 2 * pawnShift
&&
97 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
102 getPotentialMovesFrom([x
, y
]) {
103 if (this.getPiece(x
, y
) == V
.HIDDEN_QUEEN
) {
104 const pawnMoves
= this.getPotentialPawnMoves([x
, y
]);
105 let queenMoves
= super.getPotentialQueenMoves([x
, y
]);
106 // Remove from queen moves those corresponding to a pawn move:
107 queenMoves
= queenMoves
108 .filter(m
=> !this.isValidPawnMove(m
))
109 // Hidden queen is revealed if moving like a queen:
111 m
.appear
[0].p
= V
.QUEEN
;
114 return pawnMoves
.concat(queenMoves
);
116 return super.getPotentialMovesFrom([x
, y
]);
119 getEnpassantCaptures([x
, y
], shiftX
) {
120 const Lep
= this.epSquares
.length
;
121 const epSquare
= this.epSquares
[Lep
- 1];
122 let enpassantMove
= null;
125 epSquare
.x
== x
+ shiftX
&&
126 Math
.abs(epSquare
.y
- y
) == 1
128 enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
129 enpassantMove
.vanish
.push({
132 // Captured piece may be a hidden queen
133 p: this.board
[x
][epSquare
.y
][1],
134 c: this.getColor(x
, epSquare
.y
)
137 return !!enpassantMove
? [enpassantMove
] : [];
140 getPotentialPawnMoves([x
, y
]) {
141 const piece
= this.getPiece(x
, y
);
144 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
145 : [V
.QUEEN
]; //hidden queen revealed
146 return super.getPotentialPawnMoves([x
, y
], promotions
);
149 getPossibleMovesFrom(sq
) {
150 this.side
= this.turn
;
151 return this.filterValid(this.getPotentialMovesFrom(sq
));
154 static GenRandInitFen(randomness
) {
155 let fen
= ChessRules
.GenRandInitFen(randomness
);
156 // Place hidden queens at random (always):
157 let hiddenQueenPos
= randInt(8);
158 let pawnRank
= "PPPPPPPP".split("");
159 pawnRank
[hiddenQueenPos
] = "T";
160 fen
= fen
.replace("PPPPPPPP", pawnRank
.join(""));
161 hiddenQueenPos
= randInt(8);
162 pawnRank
= "pppppppp".split("");
163 pawnRank
[hiddenQueenPos
] = "t";
164 fen
= fen
.replace("pppppppp", pawnRank
.join(""));
169 super.postPlay(move);
170 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
171 // We took opponent king
172 this.kingPos
[this.turn
] = [-1, -1];
177 const oppCol
= this.turn
;
178 if (this.kingPos
[oppCol
][0] < 0)
179 // Move takes opponent's king:
180 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
184 if (this.kingPos
[color
][0] < 0) return false;
185 return super.underCheck(color
);
189 const color
= this.turn
;
190 if (this.kingPos
[color
][0] < 0)
192 return color
== "w" ? "0-1" : "1-0";
193 return super.getCurrentScore();
196 // Search is biased, so not really needed to explore deeply
197 static get SEARCH_DEPTH() {
201 static get VALUES() {
202 return Object
.assign(
209 this.side
= this.turn
;
210 return super.getComputerMove();
214 // Not using getPiece() method because it would transform HQ into pawn:
215 if (this.board
[move.start
.x
][move.start
.y
][1] != V
.HIDDEN_QUEEN
)
216 return super.getNotation(move);
217 const finalSquare
= V
.CoordsToSquare(move.end
);
218 if (move.appear
[0].p
== V
.QUEEN
) {
221 (move.vanish
.length
> move.appear
.length
? "x" : "") +
225 // Do not reveal hidden queens playing as pawns
227 if (move.vanish
.length
== 2)
229 notation
= V
.CoordToColumn(move.start
.y
) + "x" + finalSquare
;
230 else notation
= finalSquare
;