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() {
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 getPotentialPawnMoves([x
, y
]) {
91 const piece
= this.getPiece(x
, y
);
94 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
95 : [V
.QUEEN
]; //hidden queen revealed
96 return super.getPotentialPawnMoves([x
, y
], promotions
);
99 getPossibleMovesFrom(sq
) {
100 this.side
= this.turn
;
101 return this.filterValid(this.getPotentialMovesFrom(sq
));
104 static GenRandInitFen(randomness
) {
105 let fen
= ChessRules
.GenRandInitFen(randomness
);
106 // Place hidden queens at random (always):
107 let hiddenQueenPos
= randInt(8);
108 let pawnRank
= "PPPPPPPP".split("");
109 pawnRank
[hiddenQueenPos
] = "T";
110 fen
= fen
.replace("PPPPPPPP", pawnRank
.join(""));
111 hiddenQueenPos
= randInt(8);
112 pawnRank
= "pppppppp".split("");
113 pawnRank
[hiddenQueenPos
] = "t";
114 fen
= fen
.replace("pppppppp", pawnRank
.join(""));
119 super.postPlay(move);
120 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
121 // We took opponent king
122 this.kingPos
[this.turn
] = [-1, -1];
127 const oppCol
= this.turn
;
128 if (this.kingPos
[oppCol
][0] < 0)
129 // Move takes opponent's king:
130 this.kingPos
[oppCol
] = [move.vanish
[1].x
, move.vanish
[1].y
];
134 const color
= this.turn
;
135 if (this.kingPos
[color
][0] < 0)
137 return color
== "w" ? "0-1" : "1-0";
138 return super.getCurrentScore();
141 // Search is biased, so not really needed to explore deeply
142 static get SEARCH_DEPTH() {
146 static get VALUES() {
147 return Object
.assign(
154 this.side
= this.turn
;
155 return super.getComputerMove();
159 const notation
= super.getNotation(move);
160 if (notation
.charAt(0) == 'T')
161 // Do not reveal hidden queens
162 return notation
.substr(1);