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 HiddenqueenRules
extends ChessRules
{
6 // Analyse in Hiddenqueen mode makes no sense
7 static get CanAnalyze() {
11 static get HIDDEN_QUEEN() {
16 return ChessRules
.PIECES
.concat(Object
.values(V
.HIDDEN_CODE
));
20 const piece
= this.board
[i
][j
].charAt(1);
22 piece
!= V
.HIDDEN_QUEEN
||
23 // 'side' is used to determine what I see: a pawn or a (hidden)queen?
24 this.getColor(i
, j
) == this.side
31 getPpath(b
, color
, score
) {
32 if (b
[1] == V
.HIDDEN_QUEEN
) {
33 // Supposed to be hidden.
34 if (score
== "*" && (!color
|| color
!= b
[0]))
36 return "Hiddenqueen/" + b
[0] + "t";
41 isValidPawnMove(move) {
42 const color
= move.vanish
[0].c
;
43 const pawnShift
= color
== "w" ? -1 : 1;
44 const startRank
= color
== "w" ? V
.size
.x
- 2 : 1;
47 move.end
.x
- move.start
.x
== pawnShift
&&
51 move.end
.y
== move.start
.y
&&
52 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
57 Math
.abs(move.end
.y
- move.start
.y
) == 1 &&
58 this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
64 // Two-spaces initial jump
65 move.start
.x
== startRank
&&
66 move.end
.y
== move.start
.y
&&
67 move.end
.x
- move.start
.x
== 2 * pawnShift
&&
68 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
73 getPotentialMovesFrom([x
, y
]) {
74 if (this.getPiece(x
, y
) == V
.HIDDEN_QUEEN
) {
75 const pawnMoves
= this.getPotentialPawnMoves([x
, y
]);
76 let queenMoves
= super.getPotentialQueenMoves([x
, y
]);
77 // Remove from queen moves those corresponding to a pawn move:
78 queenMoves
= queenMoves
79 .filter(m
=> !this.isValidPawnMove(m
))
80 // Hidden queen is revealed if moving like a queen:
82 m
.appear
[0].p
= V
.QUEEN
;
85 return pawnMoves
.concat(queenMoves
);
87 return super.getPotentialMovesFrom([x
, y
]);
90 // TODO: find a more general way to describe pawn movements to avoid
91 // re-writing almost the same function for several variants.
92 getPotentialPawnMoves([x
, y
]) {
93 const color
= this.turn
;
94 const piece
= this.getPiece(x
, y
);
96 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
97 const shiftX
= color
== "w" ? -1 : 1;
98 const startRank
= color
== "w" ? sizeX
- 2 : 1;
99 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
102 x
+ shiftX
== lastRank
104 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
105 : [V
.QUEEN
] //hidden queen revealed
107 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
108 // One square forward
109 for (let p
of finalPieces
) {
111 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
119 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
122 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
126 for (let shiftY
of [-1, 1]) {
129 y
+ shiftY
< sizeY
&&
130 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
131 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
133 for (let p
of finalPieces
) {
135 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
145 const Lep
= this.epSquares
.length
;
146 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
149 epSquare
.x
== x
+ shiftX
&&
150 Math
.abs(epSquare
.y
- y
) == 1
152 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
153 enpassantMove
.vanish
.push({
157 c: this.getColor(x
, epSquare
.y
)
159 moves
.push(enpassantMove
);
165 getPossibleMovesFrom(sq
) {
166 this.side
= this.turn
;
167 return this.filterValid(this.getPotentialMovesFrom(sq
));
170 static GenRandInitFen(randomness
) {
171 let fen
= ChessRules
.GenRandInitFen(randomness
);
172 // Place hidden queens at random (always):
173 let hiddenQueenPos
= randInt(8);
174 let pawnRank
= "PPPPPPPP".split("");
175 pawnRank
[hiddenQueenPos
] = "T";
176 fen
= fen
.replace("PPPPPPPP", pawnRank
.join(""));
177 hiddenQueenPos
= randInt(8);
178 pawnRank
= "pppppppp".split("");
179 pawnRank
[hiddenQueenPos
] = "t";
180 fen
= fen
.replace("pppppppp", pawnRank
.join(""));
185 super.postPlay(move);
186 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
187 // We took opponent king
188 this.kingPos
[this.turn
] = [-1, -1];
193 const oppCol
= this.turn
;
194 if (this.kingPos
[oppCol
][0] < 0)
195 // Move takes opponent's king:
196 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
200 const color
= this.turn
;
201 if (this.kingPos
[color
][0] < 0)
203 return color
== "w" ? "0-1" : "1-0";
204 return super.getCurrentScore();
207 // Search is biased, so not really needed to explore deeply
208 static get SEARCH_DEPTH() {
212 static get VALUES() {
213 return Object
.assign(
220 this.side
= this.turn
;
221 return super.getComputerMove();
225 const notation
= super.getNotation(move);
226 if (notation
.charAt(0) == 'T')
227 // Do not reveal hidden queens
228 return notation
.substr(1);