Commit | Line | Data |
---|---|---|
39fe711a BA |
1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
2 | import { randInt } from "@/utils/alea"; | |
3 | import { ArrayFun } from "@/utils/array"; | |
4 | ||
5 | export class ScreenRules extends ChessRules { | |
6 | ||
7 | static get HasFlags() { | |
8 | return false; | |
9 | } | |
10 | ||
11 | static get HasEnpassant() { | |
12 | return false; | |
13 | } | |
14 | ||
15 | get showFirstTurn() { | |
16 | return true; | |
17 | } | |
18 | ||
19 | get canAnalyze() { | |
20 | return this.movesCount >= 2; | |
21 | } | |
22 | ||
23 | get someHiddenMoves() { | |
24 | return this.movesCount <= 1; | |
25 | } | |
26 | ||
27 | static GenRandInitFen() { | |
28 | // Empty board | |
29 | return "8/8/8/8/8/8/8/8 w 0"; | |
30 | } | |
31 | ||
32 | re_setReserve(subTurn) { | |
33 | const mc = this.movesCount; | |
34 | const wc = (mc == 0 ? 1 : 0); | |
35 | const bc = (mc <= 1 ? 1 : 0); | |
36 | this.reserve = { | |
37 | w: { | |
38 | [V.PAWN]: wc * 8, | |
39 | [V.ROOK]: wc * 2, | |
40 | [V.KNIGHT]: wc * 2, | |
41 | [V.BISHOP]: wc * 2, | |
42 | [V.QUEEN]: wc, | |
43 | [V.KING]: wc | |
44 | }, | |
45 | b: { | |
46 | [V.PAWN]: bc * 8, | |
47 | [V.ROOK]: bc * 2, | |
48 | [V.KNIGHT]: bc * 2, | |
49 | [V.BISHOP]: bc * 2, | |
50 | [V.QUEEN]: bc, | |
51 | [V.KING]: bc | |
52 | } | |
53 | } | |
54 | this.subTurn = subTurn || 1; | |
55 | } | |
56 | ||
57 | re_setEnlightened(onOff) { | |
58 | if (!onOff) delete this["enlightened"]; | |
59 | else { | |
60 | // Turn on: | |
61 | this.enlightened = { | |
62 | 'w': ArrayFun.init(8, 8, false), | |
63 | 'b': ArrayFun.init(8, 8, false) | |
64 | }; | |
65 | for (let i=0; i<4; i++) { | |
66 | for (let j=0; j<8; j++) this.enlightened['b'][i][j] = true; | |
67 | } | |
68 | for (let i=5; i<8; i++) { | |
69 | for (let j=0; j<8; j++) this.enlightened['w'][i][j] = true; | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
74 | setOtherVariables(fen) { | |
75 | super.setOtherVariables(fen); | |
76 | if (this.movesCount <= 1) { | |
77 | this.re_setReserve(); | |
78 | this.re_setEnlightened(true); | |
79 | } | |
80 | } | |
81 | ||
82 | getColor(i, j) { | |
83 | if (i >= V.size.x) return i == V.size.x ? "w" : "b"; | |
84 | return this.board[i][j].charAt(0); | |
85 | } | |
86 | ||
87 | getPiece(i, j) { | |
88 | if (i >= V.size.x) return V.RESERVE_PIECES[j]; | |
89 | return this.board[i][j].charAt(1); | |
90 | } | |
91 | ||
92 | getReservePpath(index, color) { | |
93 | return color + V.RESERVE_PIECES[index]; | |
94 | } | |
95 | ||
96 | static get RESERVE_PIECES() { | |
97 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING]; | |
98 | } | |
99 | ||
100 | getPotentialMovesFrom([x, y]) { | |
101 | if (this.movesCount >= 2) return super.getPotentialMovesFrom([x, y]); | |
102 | // Only reserve moves are allowed for now: | |
103 | if (V.OnBoard(x, y)) return []; | |
104 | const color = this.turn; | |
105 | const p = V.RESERVE_PIECES[y]; | |
106 | if (this.reserve[color][p] == 0) return []; | |
107 | const shift = (p == V.PAWN ? 1 : 0); | |
108 | let iBound = (color == 'w' ? [4, 7 - shift] : [shift, 3]); | |
109 | let moves = []; | |
110 | ||
111 | // Pawns cannot stack on files, one bishop per color | |
112 | let forbiddenFiles = []; | |
113 | if (p == V.PAWN) { | |
114 | const colorShift = (color == 'w' ? 4 : 1); | |
115 | forbiddenFiles = | |
116 | ArrayFun.range(8).filter(jj => { | |
117 | return ArrayFun.range(3).some(ii => { | |
118 | return ( | |
119 | this.board[colorShift + ii][jj] != V.EMPTY && | |
120 | this.getPiece(colorShift + ii, jj) == V.PAWN | |
121 | ); | |
122 | }) | |
123 | }); | |
124 | } | |
125 | let forbiddenColor = -1; | |
126 | if (p == V.BISHOP) { | |
127 | const colorShift = (color == 'w' ? 4 : 0); | |
128 | outerLoop: for (let ii = colorShift; ii < colorShift + 4; ii++) { | |
129 | for (let jj = 0; jj < 8; jj++) { | |
130 | if ( | |
131 | this.board[ii][jj] != V.EMPTY && | |
132 | this.getPiece(ii, jj) == V.BISHOP | |
133 | ) { | |
134 | forbiddenColor = (ii + jj) % 2; | |
135 | break outerLoop; | |
136 | } | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | for (let i = iBound[0]; i <= iBound[1]; i++) { | |
142 | for (let j = 0; j < 8; j++) { | |
143 | if ( | |
144 | this.board[i][j] == V.EMPTY && | |
145 | (p != V.PAWN || !forbiddenFiles.includes(j)) && | |
146 | (p != V.BISHOP || (i + j) % 2 != forbiddenColor) | |
147 | ) { | |
148 | // Ok, move is valid: | |
149 | let mv = new Move({ | |
150 | appear: [ | |
151 | new PiPo({ | |
152 | x: i, | |
153 | y: j, | |
154 | c: color, | |
155 | p: p | |
156 | }) | |
157 | ], | |
158 | vanish: [], | |
159 | start: { x: x, y: y }, | |
160 | end: { x: i, y: j } | |
161 | }); | |
162 | moves.push(mv); | |
163 | } | |
164 | } | |
165 | } | |
166 | moves.forEach(m => { m.end.noHighlight = true; }); | |
167 | return moves; | |
168 | } | |
169 | ||
170 | underCheck(color) { | |
171 | if (this.movesCount <= 1) return false; | |
172 | return super.underCheck(color); | |
173 | } | |
174 | ||
175 | getAllValidMoves() { | |
176 | if (this.movesCount >= 2) return super.getAllValidMoves(); | |
177 | const color = this.turn; | |
178 | let moves = []; | |
179 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
180 | moves = moves.concat( | |
181 | this.getPotentialMovesFrom([V.size.x + (color == "w" ? 0 : 1), i]) | |
182 | ); | |
183 | } | |
184 | return this.filterValid(moves); | |
185 | } | |
186 | ||
187 | play(move) { | |
188 | const color = move.appear[0].c; | |
189 | if (this.movesCount <= 1) { | |
190 | V.PlayOnBoard(this.board, move); | |
191 | const piece = move.appear[0].p; | |
192 | this.reserve[color][piece]--; | |
193 | if (piece == V.KING) this.kingPos[color] = [move.end.x, move.end.y]; | |
194 | if (this.subTurn == 16) { | |
195 | // All placement moves are done | |
196 | this.movesCount++; | |
197 | this.turn = V.GetOppCol(color); | |
198 | if (this.movesCount == 1) this.subTurn = 1; | |
199 | else { | |
200 | // Initial placement is over | |
201 | delete this["reserve"]; | |
202 | delete this["subTurn"]; | |
203 | } | |
204 | } | |
205 | else this.subTurn++; | |
206 | } | |
207 | else { | |
208 | if (this.movesCount == 2) this.re_setEnlightened(false); | |
209 | super.play(move); | |
210 | } | |
211 | } | |
212 | ||
213 | undo(move) { | |
214 | const color = move.appear[0].c; | |
215 | if (this.movesCount <= 2) { | |
216 | V.UndoOnBoard(this.board, move); | |
217 | const piece = move.appear[0].p; | |
218 | if (piece == V.KING) this.kingPos[color] = [-1, -1]; | |
219 | if (!this.subTurn || this.subTurn == 1) { | |
220 | // All placement moves are undone (if any) | |
221 | if (!this.subTurn) this.re_setReserve(16); | |
222 | else this.subTurn = 16; | |
223 | this.movesCount--; | |
224 | if (this.movesCount == 1) this.re_setEnlightened(true); | |
225 | this.turn = color; | |
226 | } | |
227 | else this.subTurn--; | |
228 | this.reserve[color][piece]++; | |
229 | } | |
230 | else super.undo(move); | |
231 | } | |
232 | ||
233 | getCheckSquares() { | |
234 | if (this.movesCount <= 1) return []; | |
235 | return super.getCheckSquares(); | |
236 | } | |
237 | ||
238 | getCurrentScore() { | |
239 | if (this.movesCount <= 1) return "*"; | |
240 | return super.getCurrentScore(); | |
241 | } | |
242 | ||
243 | getComputerMove() { | |
244 | if (this.movesCount >= 2) return super.getComputerMove(); | |
245 | // Play a random "initialization move" | |
246 | let res = []; | |
247 | for (let i=0; i<16; i++) { | |
248 | const moves = this.getAllValidMoves(); | |
249 | const moveIdx = randInt(moves.length); | |
250 | this.play(moves[moveIdx]); | |
251 | res.push(moves[moveIdx]); | |
252 | } | |
253 | for (let i=15; i>=0; i--) this.undo(res[i]); | |
254 | return res; | |
255 | } | |
256 | ||
257 | getNotation(move) { | |
258 | // Do not note placement moves (complete move would be too long) | |
259 | if (move.vanish.length == 0) return ""; | |
260 | return super.getNotation(move); | |
261 | } | |
262 | ||
263 | }; |