Finish Chakart. Debug now
[vchess.git] / client / src / variants / Hiddenqueen.js
CommitLineData
a97bdbda
BA
1import { ChessRules, PiPo, Move } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class HiddenqueenRules extends ChessRules {
a97bdbda
BA
6 // Analyse in Hiddenqueen mode makes no sense
7 static get CanAnalyze() {
8 return false;
9 }
10
11 static get HIDDEN_QUEEN() {
12 return 't';
13 }
14
15 static get PIECES() {
6f2f9437 16 return ChessRules.PIECES.concat([V.HIDDEN_QUEEN]);
a97bdbda
BA
17 }
18
19 getPiece(i, j) {
20 const piece = this.board[i][j].charAt(1);
21 if (
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
25 ) {
26 return piece;
27 }
28 return V.PAWN;
29 }
30
31 getPpath(b, color, score) {
32 if (b[1] == V.HIDDEN_QUEEN) {
33 // Supposed to be hidden.
34 if (score == "*" && (!color || color != b[0]))
35 return b[0] + "p";
36 return "Hiddenqueen/" + b[0] + "t";
37 }
38 return b;
39 }
40
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;
a97bdbda 45 return (
a97bdbda 46 (
3a2a7b5f 47 move.end.x - move.start.x == pawnShift &&
a97bdbda 48 (
a97bdbda 49 (
3a2a7b5f
BA
50 // Normal move
51 move.end.y == move.start.y &&
52 this.board[move.end.x][move.end.y] == V.EMPTY
53 )
54 ||
55 (
56 // Capture
57 Math.abs(move.end.y - move.start.y) == 1 &&
58 this.board[move.end.x][move.end.y] != V.EMPTY
a97bdbda
BA
59 )
60 )
3a2a7b5f
BA
61 )
62 ||
63 (
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
a97bdbda
BA
69 )
70 );
71 }
72
73 getPotentialMovesFrom([x, y]) {
74 if (this.getPiece(x, y) == V.HIDDEN_QUEEN) {
b627d118 75 const pawnMoves = this.getPotentialPawnMoves([x, y]);
a97bdbda
BA
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:
81 .map(m => {
82 m.appear[0].p = V.QUEEN;
83 return m;
84 });
85 return pawnMoves.concat(queenMoves);
86 }
87 return super.getPotentialMovesFrom([x, y]);
88 }
89
b627d118 90 getPotentialPawnMoves([x, y]) {
b627d118 91 const piece = this.getPiece(x, y);
32f6285e
BA
92 const promotions =
93 piece == V.PAWN
94 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
95 : [V.QUEEN]; //hidden queen revealed
96 return super.getPotentialPawnMoves([x, y], promotions);
b627d118
BA
97 }
98
a97bdbda
BA
99 getPossibleMovesFrom(sq) {
100 this.side = this.turn;
101 return this.filterValid(this.getPotentialMovesFrom(sq));
102 }
103
7ba4a5bc
BA
104 static GenRandInitFen(randomness) {
105 let fen = ChessRules.GenRandInitFen(randomness);
106 // Place hidden queens at random (always):
a97bdbda
BA
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(""));
115 return fen;
116 }
117
3a2a7b5f
BA
118 postPlay(move) {
119 super.postPlay(move);
a97bdbda
BA
120 if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
121 // We took opponent king
122 this.kingPos[this.turn] = [-1, -1];
123 }
124
3a2a7b5f
BA
125 preUndo(move) {
126 super.preUndo(move);
127 const oppCol = this.turn;
a97bdbda 128 if (this.kingPos[oppCol][0] < 0)
3a2a7b5f 129 // Move takes opponent's king:
a97bdbda
BA
130 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
131 }
132
133 getCurrentScore() {
134 const color = this.turn;
135 if (this.kingPos[color][0] < 0)
136 // King disappeared
137 return color == "w" ? "0-1" : "1-0";
138 return super.getCurrentScore();
139 }
140
141 // Search is biased, so not really needed to explore deeply
142 static get SEARCH_DEPTH() {
143 return 2;
144 }
145
146 static get VALUES() {
147 return Object.assign(
148 { t: 9 },
149 ChessRules.VALUES
150 );
151 }
152
153 getComputerMove() {
154 this.side = this.turn;
155 return super.getComputerMove();
156 }
b627d118
BA
157
158 getNotation(move) {
90df90bc
BA
159 if (this.getPiece(move.start.x, move.start.y) != V.HIDDEN_QUEEN)
160 return super.getNotation(move);
161 const finalSquare = V.CoordsToSquare(move.end);
162 if (move.appear[0].p == V.QUEEN) {
163 return (
164 "Q" +
165 (move.vanish.length > move.appear.length ? "x" : "") +
166 finalSquare
167 );
168 }
169 // Do not reveal hidden queens playing as pawns
170 let notation = "";
171 if (move.vanish.length == 2)
172 // Capture
173 notation = V.CoordToColumn(move.start.y) + "x" + finalSquare;
174 else notation = finalSquare;
b627d118
BA
175 return notation;
176 }
a97bdbda 177};