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
;
57 Math
.abs(s
.x
- e
.x
) == 2 &&
58 [V
.PAWN
, V
.HIDDEN_QUEEN
].includes(move.vanish
[0].p
)
65 return undefined; //default
68 isValidPawnMove(move) {
69 const color
= move.vanish
[0].c
;
70 const pawnShift
= color
== "w" ? -1 : 1;
71 const startRank
= color
== "w" ? V
.size
.x
- 2 : 1;
74 move.end
.x
- move.start
.x
== pawnShift
&&
78 move.end
.y
== move.start
.y
&&
79 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
84 Math
.abs(move.end
.y
- move.start
.y
) == 1 &&
85 this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
91 // Two-spaces initial jump
92 move.start
.x
== startRank
&&
93 move.end
.y
== move.start
.y
&&
94 move.end
.x
- move.start
.x
== 2 * pawnShift
&&
95 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
100 getPotentialMovesFrom([x
, y
]) {
101 if (this.getPiece(x
, y
) == V
.HIDDEN_QUEEN
) {
102 const pawnMoves
= this.getPotentialPawnMoves([x
, y
]);
103 let queenMoves
= super.getPotentialQueenMoves([x
, y
]);
104 // Remove from queen moves those corresponding to a pawn move:
105 queenMoves
= queenMoves
106 .filter(m
=> !this.isValidPawnMove(m
))
107 // Hidden queen is revealed if moving like a queen:
109 m
.appear
[0].p
= V
.QUEEN
;
112 return pawnMoves
.concat(queenMoves
);
114 return super.getPotentialMovesFrom([x
, y
]);
117 getEnpassantCaptures([x
, y
], shiftX
) {
118 const Lep
= this.epSquares
.length
;
119 const epSquare
= this.epSquares
[Lep
- 1];
120 let enpassantMove
= null;
123 epSquare
.x
== x
+ shiftX
&&
124 Math
.abs(epSquare
.y
- y
) == 1
126 enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
127 enpassantMove
.vanish
.push({
130 // Captured piece may be a hidden queen
131 p: this.board
[x
][epSquare
.y
][1],
132 c: this.getColor(x
, epSquare
.y
)
135 return !!enpassantMove
? [enpassantMove
] : [];
138 getPotentialPawnMoves([x
, y
]) {
139 const piece
= this.getPiece(x
, y
);
142 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
143 : [V
.QUEEN
]; //hidden queen revealed
144 return super.getPotentialPawnMoves([x
, y
], promotions
);
147 getPossibleMovesFrom(sq
) {
148 this.side
= this.turn
;
149 return this.filterValid(this.getPotentialMovesFrom(sq
));
152 static GenRandInitFen(randomness
) {
153 let fen
= ChessRules
.GenRandInitFen(randomness
);
154 // Place hidden queens at random (always):
155 let hiddenQueenPos
= randInt(8);
156 let pawnRank
= "PPPPPPPP".split("");
157 pawnRank
[hiddenQueenPos
] = "T";
158 fen
= fen
.replace("PPPPPPPP", pawnRank
.join(""));
159 hiddenQueenPos
= randInt(8);
160 pawnRank
= "pppppppp".split("");
161 pawnRank
[hiddenQueenPos
] = "t";
162 fen
= fen
.replace("pppppppp", pawnRank
.join(""));
167 super.postPlay(move);
168 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
169 // We took opponent king
170 this.kingPos
[this.turn
] = [-1, -1];
175 const oppCol
= this.turn
;
176 if (this.kingPos
[oppCol
][0] < 0)
177 // Move takes opponent's king:
178 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
182 const color
= this.turn
;
183 if (this.kingPos
[color
][0] < 0)
185 return color
== "w" ? "0-1" : "1-0";
186 return super.getCurrentScore();
189 // Search is biased, so not really needed to explore deeply
190 static get SEARCH_DEPTH() {
194 static get VALUES() {
195 return Object
.assign(
202 this.side
= this.turn
;
203 return super.getComputerMove();
207 // Not using getPiece() method because it would transform HQ into pawn:
208 if (this.board
[move.start
.x
][move.start
.y
][1] != V
.HIDDEN_QUEEN
)
209 return super.getNotation(move);
210 const finalSquare
= V
.CoordsToSquare(move.end
);
211 if (move.appear
[0].p
== V
.QUEEN
) {
214 (move.vanish
.length
> move.appear
.length
? "x" : "") +
218 // Do not reveal hidden queens playing as pawns
220 if (move.vanish
.length
== 2)
222 notation
= V
.CoordToColumn(move.start
.y
) + "x" + finalSquare
;
223 else notation
= finalSquare
;