Commit | Line | Data |
---|---|---|
1d184b4c BA |
1 | class CheckeredRules extends ChessRules |
2 | { | |
1d184b4c BA |
3 | static getPpath(b) |
4 | { | |
5 | return b[0]=='c' ? "Checkered/"+b : b; | |
6 | } | |
7 | static board2fen(b) | |
8 | { | |
9 | const checkered_codes = { | |
10 | 'p': 's', | |
11 | 'q': 't', | |
12 | 'r': 'u', | |
13 | 'b': 'c', | |
14 | 'n': 'o', | |
15 | }; | |
16 | if (b[0]=="c") | |
17 | return checkered_codes[b[1]]; | |
18 | return ChessRules.board2fen(b); | |
19 | } | |
20 | static fen2board(f) | |
21 | { | |
22 | const checkered_pieces = { | |
23 | 's': 'p', | |
24 | 't': 'q', | |
25 | 'u': 'r', | |
26 | 'c': 'b', | |
27 | 'o': 'n', | |
28 | }; | |
29 | if (Object.keys(checkered_pieces).includes(f)) | |
30 | return 'c'+checkered_pieces[f]; | |
31 | return ChessRules.fen2board(f); | |
32 | } | |
33 | ||
2526c041 | 34 | setFlags(fen) |
1d184b4c | 35 | { |
2526c041 BA |
36 | super.setFlags(fen); //castleFlags |
37 | this.pawnFlags = | |
38 | { | |
39 | "w": new Array(8), //pawns can move 2 squares? | |
40 | "b": new Array(8) | |
41 | }; | |
42 | const flags = fen.split(" ")[1].substr(4); //skip first 4 digits, for castle | |
1d184b4c BA |
43 | for (let c of ['w','b']) |
44 | { | |
45 | for (let i=0; i<8; i++) | |
2526c041 | 46 | this.pawnFlags[c][i] = (flags.charAt((c=='w'?0:8)+i) == '1'); |
1d184b4c | 47 | } |
1d184b4c BA |
48 | } |
49 | ||
2526c041 BA |
50 | // Aggregates flags into one object |
51 | get flags() { | |
52 | return [this.castleFlags, this.pawnFlags]; | |
1d184b4c BA |
53 | } |
54 | ||
2526c041 BA |
55 | // Reverse operation |
56 | parseFlags(flags) | |
1d184b4c | 57 | { |
2526c041 BA |
58 | this.castleFlags = flags[0]; |
59 | this.pawnFlags = flags[1]; | |
1d184b4c BA |
60 | } |
61 | ||
2526c041 | 62 | canTake([x1,y1], [x2,y2]) |
1d184b4c | 63 | { |
2526c041 BA |
64 | const color1 = this.getColor(x1,y1); |
65 | const color2 = this.getColor(x2,y2); | |
66 | // Checkered aren't captured | |
67 | return color1 != color2 && color2 != 'c' && (color1 != 'c' || color2 != this.turn); | |
1d184b4c BA |
68 | } |
69 | ||
2526c041 BA |
70 | // Post-processing: apply "checkerization" of standard moves |
71 | getPotentialMovesFrom([x,y]) | |
1d184b4c | 72 | { |
2526c041 | 73 | let standardMoves = super.getPotentialMovesFrom([x,y]); |
aea1443e | 74 | const lastRank = this.turn == "w" ? 0 : 7; |
0b7d99ec | 75 | if (this.getPiece(x,y) == V.KING) |
2526c041 | 76 | return standardMoves; //king has to be treated differently (for castles) |
1d184b4c | 77 | let moves = []; |
2526c041 | 78 | standardMoves.forEach(m => { |
0b7d99ec | 79 | if (m.vanish[0].p == V.PAWN) |
1d184b4c | 80 | { |
2316f8b8 BA |
81 | if (Math.abs(m.end.x-m.start.x)==2 && !this.pawnFlags[this.turn][m.start.y]) |
82 | return; //skip forbidden 2-squares jumps | |
0b7d99ec BA |
83 | if (this.board[m.end.x][m.end.y] == V.EMPTY && m.vanish.length==2 |
84 | && this.getColor(m.start.x,m.start.y) == 'c') | |
2316f8b8 BA |
85 | { |
86 | return; //checkered pawns cannot take en-passant | |
87 | } | |
1d184b4c | 88 | } |
2526c041 BA |
89 | if (m.vanish.length == 1) |
90 | moves.push(m); //no capture | |
91 | else | |
1d184b4c | 92 | { |
2526c041 BA |
93 | // A capture occured (m.vanish.length == 2) |
94 | m.appear[0].c = "c"; | |
aea1443e | 95 | moves.push(m); |
68f5ccc8 | 96 | if (m.appear[0].p != m.vanish[1].p //avoid promotions (already treated): |
0b7d99ec | 97 | && (m.vanish[0].p != V.PAWN || m.end.x != lastRank)) |
1d184b4c | 98 | { |
2526c041 BA |
99 | // Add transformation into captured piece |
100 | let m2 = JSON.parse(JSON.stringify(m)); | |
aea1443e | 101 | m2.appear[0].p = m.vanish[1].p; |
2526c041 | 102 | moves.push(m2); |
1d184b4c BA |
103 | } |
104 | } | |
2526c041 | 105 | }); |
1d184b4c BA |
106 | return moves; |
107 | } | |
108 | ||
46302e64 | 109 | canIplay(side, [x,y]) |
1d184b4c | 110 | { |
92342261 BA |
111 | return ((side=='w' && this.moves.length%2==0) |
112 | || (side=='b' && this.moves.length%2==1)) | |
46302e64 | 113 | && [side,'c'].includes(this.getColor(x,y)); |
1d184b4c BA |
114 | } |
115 | ||
116 | // Does m2 un-do m1 ? (to disallow undoing checkered moves) | |
117 | oppositeMoves(m1, m2) | |
118 | { | |
119 | return m1.appear.length == 1 && m2.appear.length == 1 | |
120 | && m1.vanish.length == 1 && m2.vanish.length == 1 | |
1d184b4c BA |
121 | && m1.start.x == m2.end.x && m1.end.x == m2.start.x |
122 | && m1.start.y == m2.end.y && m1.end.y == m2.start.y | |
123 | && m1.appear[0].c == m2.vanish[0].c && m1.appear[0].p == m2.vanish[0].p | |
124 | && m1.vanish[0].c == m2.appear[0].c && m1.vanish[0].p == m2.appear[0].p; | |
125 | } | |
126 | ||
127 | filterValid(moves) | |
128 | { | |
129 | if (moves.length == 0) | |
130 | return []; | |
46302e64 | 131 | const color = this.turn; |
1d184b4c BA |
132 | return moves.filter(m => { |
133 | const L = this.moves.length; | |
134 | if (L > 0 && this.oppositeMoves(this.moves[L-1], m)) | |
135 | return false; | |
46302e64 | 136 | return !this.underCheck(m); |
1d184b4c BA |
137 | }); |
138 | } | |
139 | ||
46302e64 BA |
140 | isAttackedByPawn([x,y], colors) |
141 | { | |
142 | for (let c of colors) | |
143 | { | |
144 | const color = (c=="c" ? this.turn : c); | |
145 | let pawnShift = (color=="w" ? 1 : -1); | |
146 | if (x+pawnShift>=0 && x+pawnShift<8) | |
147 | { | |
148 | for (let i of [-1,1]) | |
149 | { | |
0b7d99ec | 150 | if (y+i>=0 && y+i<8 && this.getPiece(x+pawnShift,y+i)==V.PAWN |
46302e64 BA |
151 | && this.getColor(x+pawnShift,y+i)==c) |
152 | { | |
153 | return true; | |
154 | } | |
155 | } | |
156 | } | |
157 | } | |
158 | return false; | |
159 | } | |
1d184b4c | 160 | |
46302e64 | 161 | underCheck(move) |
1d184b4c | 162 | { |
46302e64 | 163 | const color = this.turn; |
1d184b4c | 164 | this.play(move); |
46302e64 | 165 | let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']); |
1d184b4c BA |
166 | this.undo(move); |
167 | return res; | |
168 | } | |
169 | ||
46302e64 | 170 | getCheckSquares(move) |
bd6ff57c BA |
171 | { |
172 | this.play(move); | |
46302e64 | 173 | const color = this.turn; |
cd4cad04 | 174 | this.moves.push(move); //artifically change turn, for checkered pawns (TODO) |
92342261 BA |
175 | const kingAttacked = this.isAttacked( |
176 | this.kingPos[color], [this.getOppCol(color),'c']); | |
bd6ff57c | 177 | let res = kingAttacked |
46302e64 | 178 | ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] //need to duplicate! |
bd6ff57c | 179 | : [ ]; |
cd4cad04 | 180 | this.moves.pop(); |
bd6ff57c BA |
181 | this.undo(move); |
182 | return res; | |
183 | } | |
184 | ||
1d184b4c BA |
185 | updateVariables(move) |
186 | { | |
0b7d99ec BA |
187 | super.updateVariables(move); |
188 | // Does this move turn off a 2-squares pawn flag? | |
1d184b4c | 189 | const secondRank = [1,6]; |
0b7d99ec | 190 | if (secondRank.includes(move.start.x) && move.vanish[0].p == V.PAWN) |
2526c041 | 191 | this.pawnFlags[move.start.x==6 ? "w" : "b"][move.start.y] = false; |
1d184b4c BA |
192 | } |
193 | ||
46302e64 | 194 | checkGameEnd() |
1d184b4c | 195 | { |
46302e64 | 196 | const color = this.turn; |
e2b216fe BA |
197 | this.moves.length++; //artifically change turn, for checkered pawns (TODO) |
198 | const res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']) | |
199 | ? (color == "w" ? "0-1" : "1-0") | |
200 | : "1/2"; | |
201 | this.moves.length--; | |
202 | return res; | |
1d184b4c BA |
203 | } |
204 | ||
205 | evalPosition() | |
206 | { | |
1d184b4c BA |
207 | let evaluation = 0; |
208 | //Just count material for now, considering checkered neutral (...) | |
0b7d99ec | 209 | for (let i=0; i<V.size.x; i++) |
1d184b4c | 210 | { |
0b7d99ec | 211 | for (let j=0; j<V.size.y; j++) |
1d184b4c | 212 | { |
0b7d99ec | 213 | if (this.board[i][j] != V.EMPTY) |
1d184b4c BA |
214 | { |
215 | const sqColor = this.getColor(i,j); | |
216 | const sign = sqColor == "w" ? 1 : (sqColor=="b" ? -1 : 0); | |
0b7d99ec | 217 | evaluation += sign * V.VALUES[this.getPiece(i,j)]; |
1d184b4c BA |
218 | } |
219 | } | |
220 | } | |
221 | return evaluation; | |
222 | } | |
223 | ||
224 | static GenRandInitFen() | |
225 | { | |
f3802fcd | 226 | return ChessRules.GenRandInitFen() + "1111111111111111"; //add 16 pawns flags |
1d184b4c BA |
227 | } |
228 | ||
229 | getFlagsFen() | |
230 | { | |
2526c041 | 231 | let fen = super.getFlagsFen(); |
1d184b4c BA |
232 | // Add pawns flags |
233 | for (let c of ['w','b']) | |
234 | { | |
235 | for (let i=0; i<8; i++) | |
2526c041 | 236 | fen += this.pawnFlags[c][i] ? '1' : '0'; |
1d184b4c BA |
237 | } |
238 | return fen; | |
239 | } | |
240 | ||
241 | getNotation(move) | |
242 | { | |
243 | if (move.appear.length == 2) | |
244 | { | |
245 | // Castle | |
246 | if (move.end.y < move.start.y) | |
247 | return "0-0-0"; | |
248 | else | |
249 | return "0-0"; | |
250 | } | |
251 | ||
252 | // Translate final square | |
253 | let finalSquare = | |
0b7d99ec | 254 | String.fromCharCode(97 + move.end.y) + (V.size.x-move.end.x); |
1d184b4c BA |
255 | |
256 | let piece = this.getPiece(move.start.x, move.start.y); | |
0b7d99ec | 257 | if (piece == V.PAWN) |
1d184b4c BA |
258 | { |
259 | // Pawn move | |
260 | let notation = ""; | |
261 | if (move.vanish.length > 1) | |
262 | { | |
263 | // Capture | |
264 | let startColumn = String.fromCharCode(97 + move.start.y); | |
92342261 BA |
265 | notation = startColumn + "x" + finalSquare + |
266 | "=" + move.appear[0].p.toUpperCase(); | |
1d184b4c BA |
267 | } |
268 | else //no capture | |
098e8468 | 269 | { |
1d184b4c | 270 | notation = finalSquare; |
098e8468 BA |
271 | if (move.appear.length > 0 && piece != move.appear[0].p) //promotion |
272 | notation += "=" + move.appear[0].p.toUpperCase(); | |
273 | } | |
1d184b4c BA |
274 | return notation; |
275 | } | |
276 | ||
277 | else | |
278 | { | |
279 | // Piece movement | |
280 | return piece.toUpperCase() + (move.vanish.length > 1 ? "x" : "") + finalSquare | |
281 | + (move.vanish.length > 1 ? "=" + move.appear[0].p.toUpperCase() : ""); | |
282 | } | |
283 | } | |
284 | } |