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;
45 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
47 // The queen is discovered if she reaches the 8th rank,
48 // even if this would be a technically valid pawn move.
49 move.end
.x
!= lastRank
&&
52 move.end
.x
- move.start
.x
== pawnShift
&&
56 move.end
.y
== move.start
.y
&&
57 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
62 Math
.abs(move.end
.y
- move.start
.y
) == 1 &&
63 this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
69 // Two-spaces initial jump
70 move.start
.x
== startRank
&&
71 move.end
.y
== move.start
.y
&&
72 move.end
.x
- move.start
.x
== 2 * pawnShift
&&
73 this.board
[move.end
.x
][move.end
.y
] == V
.EMPTY
79 getPotentialMovesFrom([x
, y
]) {
80 if (this.getPiece(x
, y
) == V
.HIDDEN_QUEEN
) {
81 const pawnMoves
= super.getPotentialPawnMoves([x
, y
]);
82 let queenMoves
= super.getPotentialQueenMoves([x
, y
]);
83 // Remove from queen moves those corresponding to a pawn move:
84 queenMoves
= queenMoves
85 .filter(m
=> !this.isValidPawnMove(m
))
86 // Hidden queen is revealed if moving like a queen:
88 m
.appear
[0].p
= V
.QUEEN
;
91 return pawnMoves
.concat(queenMoves
);
93 return super.getPotentialMovesFrom([x
, y
]);
96 getPossibleMovesFrom(sq
) {
97 this.side
= this.turn
;
98 return this.filterValid(this.getPotentialMovesFrom(sq
));
101 static GenRandInitFen() {
102 let fen
= ChessRules
.GenRandInitFen();
103 // Place hidden queens at random:
104 let hiddenQueenPos
= randInt(8);
105 let pawnRank
= "PPPPPPPP".split("");
106 pawnRank
[hiddenQueenPos
] = "T";
107 fen
= fen
.replace("PPPPPPPP", pawnRank
.join(""));
108 hiddenQueenPos
= randInt(8);
109 pawnRank
= "pppppppp".split("");
110 pawnRank
[hiddenQueenPos
] = "t";
111 fen
= fen
.replace("pppppppp", pawnRank
.join(""));
115 updateVariables(move) {
116 super.updateVariables(move);
117 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
118 // We took opponent king
119 this.kingPos
[this.turn
] = [-1, -1];
122 unupdateVariables(move) {
123 super.unupdateVariables(move);
124 const c
= move.vanish
[0].c
;
125 const oppCol
= V
.GetOppCol(c
);
126 if (this.kingPos
[oppCol
][0] < 0)
127 // Last move took opponent's king:
128 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
132 const color
= this.turn
;
133 if (this.kingPos
[color
][0] < 0)
135 return color
== "w" ? "0-1" : "1-0";
136 return super.getCurrentScore();
139 // Search is biased, so not really needed to explore deeply
140 static get SEARCH_DEPTH() {
144 static get VALUES() {
145 return Object
.assign(
152 this.side
= this.turn
;
153 return super.getComputerMove();