Commit | Line | Data |
---|---|---|
e3e2cc44 BA |
1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
2 | import { ArrayFun } from "@/utils/array"; | |
32f6285e | 3 | import { shuffle } from "@/utils/alea"; |
e3e2cc44 | 4 | |
32f6285e | 5 | export class CircularRules extends ChessRules { |
3a2a7b5f BA |
6 | static get HasCastle() { |
7 | return false; | |
8 | } | |
9 | ||
71ef1664 | 10 | static get HasEnpassant() { |
e3e2cc44 BA |
11 | return false; |
12 | } | |
13 | ||
71ef1664 | 14 | static get CanFlip() { |
e3e2cc44 BA |
15 | return false; |
16 | } | |
17 | ||
71ef1664 BA |
18 | setFlags(fenflags) { |
19 | this.pawnFlags = { | |
20 | w: [...Array(8).fill(true)], //pawns can move 2 squares? | |
21 | b: [...Array(8).fill(true)] | |
22 | }; | |
23 | for (let c of ["w", "b"]) { | |
24 | for (let i = 0; i < 8; i++) | |
25 | this.pawnFlags[c][i] = fenflags.charAt((c == "w" ? 0 : 8) + i) == "1"; | |
26 | } | |
27 | } | |
28 | ||
29 | aggregateFlags() { | |
30 | return this.pawnFlags; | |
31 | } | |
32 | ||
33 | disaggregateFlags(flags) { | |
34 | this.pawnFlags = flags; | |
35 | } | |
e3e2cc44 | 36 | |
7ba4a5bc | 37 | static GenRandInitFen(randomness) { |
7ba4a5bc BA |
38 | if (randomness == 0) |
39 | return "8/8/pppppppp/rnbqkbnr/8/8/PPPPPPPP/RNBQKBNR w 0 1111111111111111"; | |
40 | ||
e3e2cc44 | 41 | let pieces = { w: new Array(8), b: new Array(8) }; |
32f6285e | 42 | // Shuffle pieces on first and last rank |
e3e2cc44 | 43 | for (let c of ["w", "b"]) { |
7ba4a5bc BA |
44 | if (c == 'b' && randomness == 1) { |
45 | pieces['b'] = pieces['w']; | |
46 | break; | |
47 | } | |
48 | ||
32f6285e BA |
49 | // Get random squares for every piece, totally freely |
50 | let positions = shuffle(ArrayFun.range(8)); | |
51 | const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; | |
52 | const rem2 = positions[0] % 2; | |
53 | if (rem2 == positions[1] % 2) { | |
54 | // Fix bishops (on different colors) | |
55 | for (let i=2; i<8; i++) { | |
56 | if (positions[i] % 2 != rem2) | |
57 | [positions[1], positions[i]] = [positions[i], positions[1]]; | |
58 | } | |
59 | } | |
60 | for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i]; | |
e3e2cc44 BA |
61 | } |
62 | return ( | |
71ef1664 | 63 | "8/8/pppppppp/" + |
e3e2cc44 | 64 | pieces["b"].join("") + |
71ef1664 | 65 | "/8/8/PPPPPPPP/" + |
e3e2cc44 | 66 | pieces["w"].join("").toUpperCase() + |
71ef1664 BA |
67 | // 16 flags: can pawns advance 2 squares? |
68 | " w 0 1111111111111111" | |
e3e2cc44 BA |
69 | ); |
70 | } | |
71 | ||
71ef1664 BA |
72 | // Output basically x % 8 (circular board) |
73 | static ComputeX(x) { | |
74 | let res = x % V.size.x; | |
75 | if (res < 0) | |
76 | res += V.size.x; | |
77 | return res; | |
78 | } | |
79 | ||
e3e2cc44 BA |
80 | getSlideNJumpMoves([x, y], steps, oneStep) { |
81 | let moves = []; | |
82 | outerLoop: for (let step of steps) { | |
71ef1664 | 83 | let i = V.ComputeX(x + step[0]); |
e3e2cc44 BA |
84 | let j = y + step[1]; |
85 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { | |
86 | moves.push(this.getBasicMove([x, y], [i, j])); | |
87 | if (oneStep !== undefined) continue outerLoop; | |
71ef1664 | 88 | i = V.ComputeX(i + step[0]); |
e3e2cc44 BA |
89 | j += step[1]; |
90 | } | |
91 | if (V.OnBoard(i, j) && this.canTake([x, y], [i, j])) | |
92 | moves.push(this.getBasicMove([x, y], [i, j])); | |
93 | } | |
94 | return moves; | |
95 | } | |
96 | ||
e3e2cc44 BA |
97 | getPotentialPawnMoves([x, y]) { |
98 | const color = this.turn; | |
99 | let moves = []; | |
100 | const [sizeX, sizeY] = [V.size.x, V.size.y]; | |
71ef1664 BA |
101 | // All pawns go in the same direction! |
102 | const shiftX = -1; | |
103 | const startRank = color == "w" ? sizeX - 2 : 2; | |
104 | ||
105 | // One square forward | |
106 | const nextRow = V.ComputeX(x + shiftX); | |
107 | if (this.board[nextRow][y] == V.EMPTY) { | |
108 | moves.push(this.getBasicMove([x, y], [nextRow, y])); | |
109 | if ( | |
110 | x == startRank && | |
111 | this.pawnFlags[color][y] && | |
112 | this.board[x + 2 * shiftX][y] == V.EMPTY | |
113 | ) { | |
114 | // Two squares jump | |
115 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); | |
e3e2cc44 | 116 | } |
71ef1664 BA |
117 | } |
118 | // Captures | |
119 | for (let shiftY of [-1, 1]) { | |
120 | if ( | |
121 | y + shiftY >= 0 && | |
122 | y + shiftY < sizeY && | |
123 | this.board[nextRow][y + shiftY] != V.EMPTY && | |
124 | this.canTake([x, y], [nextRow, y + shiftY]) | |
125 | ) { | |
126 | moves.push(this.getBasicMove([x, y], [nextRow, y + shiftY])); | |
e3e2cc44 BA |
127 | } |
128 | } | |
129 | ||
130 | return moves; | |
131 | } | |
132 | ||
71ef1664 BA |
133 | filterValid(moves) { |
134 | const filteredMoves = super.filterValid(moves); | |
135 | // If at least one full move made, everything is allowed: | |
9842aca2 | 136 | if (this.movesCount >= 2) return filteredMoves; |
71ef1664 BA |
137 | // Else, forbid check: |
138 | const oppCol = V.GetOppCol(this.turn); | |
139 | return filteredMoves.filter(m => { | |
140 | this.play(m); | |
141 | const res = !this.underCheck(oppCol); | |
142 | this.undo(m); | |
143 | return res; | |
144 | }); | |
145 | } | |
146 | ||
68e19a44 BA |
147 | isAttackedByPawn([x, y], color) { |
148 | // pawn shift is always 1 (all pawns go the same way) | |
149 | const attackerRow = V.ComputeX(x + 1); | |
150 | for (let i of [-1, 1]) { | |
151 | if ( | |
152 | y + i >= 0 && | |
153 | y + i < V.size.y && | |
154 | this.getPiece(attackerRow, y + i) == V.PAWN && | |
155 | this.getColor(attackerRow, y + i) == color | |
156 | ) { | |
157 | return true; | |
e3e2cc44 BA |
158 | } |
159 | } | |
160 | return false; | |
161 | } | |
162 | ||
68e19a44 | 163 | isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { |
e3e2cc44 | 164 | for (let step of steps) { |
71ef1664 | 165 | let rx = V.ComputeX(x + step[0]), |
e3e2cc44 BA |
166 | ry = y + step[1]; |
167 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { | |
71ef1664 | 168 | rx = V.ComputeX(rx + step[0]); |
e3e2cc44 BA |
169 | ry += step[1]; |
170 | } | |
171 | if ( | |
172 | V.OnBoard(rx, ry) && | |
68e19a44 BA |
173 | this.getPiece(rx, ry) == piece && |
174 | this.getColor(rx, ry) == color | |
e3e2cc44 BA |
175 | ) { |
176 | return true; | |
177 | } | |
178 | } | |
179 | return false; | |
180 | } | |
71ef1664 BA |
181 | |
182 | getFlagsFen() { | |
183 | // Return pawns flags | |
184 | let flags = ""; | |
185 | for (let c of ["w", "b"]) { | |
186 | for (let i = 0; i < 8; i++) flags += this.pawnFlags[c][i] ? "1" : "0"; | |
187 | } | |
188 | return flags; | |
189 | } | |
190 | ||
3a2a7b5f BA |
191 | postPlay(move) { |
192 | super.postPlay(move); | |
71ef1664 | 193 | const c = move.vanish[0].c; |
3a2a7b5f BA |
194 | const secondRank = { "w": 6, "b": 2 }; |
195 | if (move.vanish[0].p == V.PAWN && secondRank[c] == move.start.x) | |
71ef1664 BA |
196 | // This move turns off a 2-squares pawn flag |
197 | this.pawnFlags[c][move.start.y] = false; | |
198 | } | |
199 | ||
200 | static get VALUES() { | |
201 | return { | |
202 | p: 1, | |
203 | r: 5, | |
204 | n: 3, | |
205 | b: 4, | |
206 | q: 10, | |
207 | k: 1000 | |
208 | }; | |
209 | } | |
b83a675a BA |
210 | |
211 | static get SEARCH_DEPTH() { | |
212 | return 2; | |
213 | } | |
e3e2cc44 | 214 | }; |