Commit | Line | Data |
---|---|---|
a97bdbda BA |
1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
2 | import { ArrayFun } from "@/utils/array"; | |
3 | import { randInt } from "@/utils/alea"; | |
4 | ||
5 | export const VariantRules = class HiddenqueenRules extends ChessRules { | |
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() { | |
16 | return ChessRules.PIECES.concat(Object.values(V.HIDDEN_CODE)); | |
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; | |
45 | const lastRank = color == "w" ? 0 : V.size.x - 1; | |
46 | return ( | |
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 && | |
50 | ( | |
51 | ( | |
52 | move.end.x - move.start.x == pawnShift && | |
53 | ( | |
54 | ( | |
55 | // Normal move | |
56 | move.end.y == move.start.y && | |
57 | this.board[move.end.x][move.end.y] == V.EMPTY | |
58 | ) | |
59 | || | |
60 | ( | |
61 | // Capture | |
62 | Math.abs(move.end.y - move.start.y) == 1 && | |
63 | this.board[move.end.x][move.end.y] != V.EMPTY | |
64 | ) | |
65 | ) | |
66 | ) | |
67 | || | |
68 | ( | |
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 | |
74 | ) | |
75 | ) | |
76 | ); | |
77 | } | |
78 | ||
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: | |
87 | .map(m => { | |
88 | m.appear[0].p = V.QUEEN; | |
89 | return m; | |
90 | }); | |
91 | return pawnMoves.concat(queenMoves); | |
92 | } | |
93 | return super.getPotentialMovesFrom([x, y]); | |
94 | } | |
95 | ||
96 | getPossibleMovesFrom(sq) { | |
97 | this.side = this.turn; | |
98 | return this.filterValid(this.getPotentialMovesFrom(sq)); | |
99 | } | |
100 | ||
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("")); | |
112 | return fen; | |
113 | } | |
114 | ||
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]; | |
120 | } | |
121 | ||
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]; | |
129 | } | |
130 | ||
131 | getCurrentScore() { | |
132 | const color = this.turn; | |
133 | if (this.kingPos[color][0] < 0) | |
134 | // King disappeared | |
135 | return color == "w" ? "0-1" : "1-0"; | |
136 | return super.getCurrentScore(); | |
137 | } | |
138 | ||
139 | // Search is biased, so not really needed to explore deeply | |
140 | static get SEARCH_DEPTH() { | |
141 | return 2; | |
142 | } | |
143 | ||
144 | static get VALUES() { | |
145 | return Object.assign( | |
146 | { t: 9 }, | |
147 | ChessRules.VALUES | |
148 | ); | |
149 | } | |
150 | ||
151 | getComputerMove() { | |
152 | this.side = this.turn; | |
153 | return super.getComputerMove(); | |
154 | } | |
155 | }; |