Add Knightrelay1. Some fixes. Move odd 'isAttackedBy_multiple_colors' to Checkered...
[vchess.git] / client / src / variants / Circular.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt, shuffle } from "@/utils/alea";
4
5 export const VariantRules = class CircularRules extends ChessRules {
6 static get HasCastle() {
7 return false;
8 }
9
10 static get HasEnpassant() {
11 return false;
12 }
13
14 static get CanFlip() {
15 return false;
16 }
17
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 }
36
37 static GenRandInitFen(randomness) {
38 if (randomness == 0)
39 return "8/8/pppppppp/rnbqkbnr/8/8/PPPPPPPP/RNBQKBNR w 0 1111111111111111";
40
41 let pieces = { w: new Array(8), b: new Array(8) };
42 // Shuffle pieces on first and fifth rank
43 for (let c of ["w", "b"]) {
44 if (c == 'b' && randomness == 1) {
45 pieces['b'] = pieces['w'];
46 break;
47 }
48
49 let positions = ArrayFun.range(8);
50
51 // Get random squares for bishops
52 let randIndex = 2 * randInt(4);
53 const bishop1Pos = positions[randIndex];
54 // The second bishop must be on a square of different color
55 let randIndex_tmp = 2 * randInt(4) + 1;
56 const bishop2Pos = positions[randIndex_tmp];
57 // Remove chosen squares
58 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
59 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
60
61 // Get random squares for knights
62 randIndex = randInt(6);
63 const knight1Pos = positions[randIndex];
64 positions.splice(randIndex, 1);
65 randIndex = randInt(5);
66 const knight2Pos = positions[randIndex];
67 positions.splice(randIndex, 1);
68
69 // Get random square for queen
70 randIndex = randInt(4);
71 const queenPos = positions[randIndex];
72 positions.splice(randIndex, 1);
73
74 // Rooks and king positions are now fixed,
75 // because of the ordering rook-king-rook
76 const rook1Pos = positions[0];
77 const kingPos = positions[1];
78 const rook2Pos = positions[2];
79
80 // Finally put the shuffled pieces in the board array
81 pieces[c][rook1Pos] = "r";
82 pieces[c][knight1Pos] = "n";
83 pieces[c][bishop1Pos] = "b";
84 pieces[c][queenPos] = "q";
85 pieces[c][kingPos] = "k";
86 pieces[c][bishop2Pos] = "b";
87 pieces[c][knight2Pos] = "n";
88 pieces[c][rook2Pos] = "r";
89 }
90 return (
91 "8/8/pppppppp/" +
92 pieces["b"].join("") +
93 "/8/8/PPPPPPPP/" +
94 pieces["w"].join("").toUpperCase() +
95 // 16 flags: can pawns advance 2 squares?
96 " w 0 1111111111111111"
97 );
98 }
99
100 // Output basically x % 8 (circular board)
101 static ComputeX(x) {
102 let res = x % V.size.x;
103 if (res < 0)
104 res += V.size.x;
105 return res;
106 }
107
108 getSlideNJumpMoves([x, y], steps, oneStep) {
109 let moves = [];
110 outerLoop: for (let step of steps) {
111 let i = V.ComputeX(x + step[0]);
112 let j = y + step[1];
113 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
114 moves.push(this.getBasicMove([x, y], [i, j]));
115 if (oneStep !== undefined) continue outerLoop;
116 i = V.ComputeX(i + step[0]);
117 j += step[1];
118 }
119 if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]))
120 moves.push(this.getBasicMove([x, y], [i, j]));
121 }
122 return moves;
123 }
124
125 getPotentialPawnMoves([x, y]) {
126 const color = this.turn;
127 let moves = [];
128 const [sizeX, sizeY] = [V.size.x, V.size.y];
129 // All pawns go in the same direction!
130 const shiftX = -1;
131 const startRank = color == "w" ? sizeX - 2 : 2;
132
133 // One square forward
134 const nextRow = V.ComputeX(x + shiftX);
135 if (this.board[nextRow][y] == V.EMPTY) {
136 moves.push(this.getBasicMove([x, y], [nextRow, y]));
137 if (
138 x == startRank &&
139 this.pawnFlags[color][y] &&
140 this.board[x + 2 * shiftX][y] == V.EMPTY
141 ) {
142 // Two squares jump
143 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
144 }
145 }
146 // Captures
147 for (let shiftY of [-1, 1]) {
148 if (
149 y + shiftY >= 0 &&
150 y + shiftY < sizeY &&
151 this.board[nextRow][y + shiftY] != V.EMPTY &&
152 this.canTake([x, y], [nextRow, y + shiftY])
153 ) {
154 moves.push(this.getBasicMove([x, y], [nextRow, y + shiftY]));
155 }
156 }
157
158 return moves;
159 }
160
161 getPotentialKingMoves(sq) {
162 return this.getSlideNJumpMoves(
163 sq,
164 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
165 "oneStep"
166 );
167 }
168
169 filterValid(moves) {
170 const filteredMoves = super.filterValid(moves);
171 // If at least one full move made, everything is allowed:
172 if (this.movesCount >= 2) return filteredMoves;
173 // Else, forbid check:
174 const oppCol = V.GetOppCol(this.turn);
175 return filteredMoves.filter(m => {
176 this.play(m);
177 const res = !this.underCheck(oppCol);
178 this.undo(m);
179 return res;
180 });
181 }
182
183 isAttackedByPawn([x, y], color) {
184 // pawn shift is always 1 (all pawns go the same way)
185 const attackerRow = V.ComputeX(x + 1);
186 for (let i of [-1, 1]) {
187 if (
188 y + i >= 0 &&
189 y + i < V.size.y &&
190 this.getPiece(attackerRow, y + i) == V.PAWN &&
191 this.getColor(attackerRow, y + i) == color
192 ) {
193 return true;
194 }
195 }
196 return false;
197 }
198
199 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
200 for (let step of steps) {
201 let rx = V.ComputeX(x + step[0]),
202 ry = y + step[1];
203 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
204 rx = V.ComputeX(rx + step[0]);
205 ry += step[1];
206 }
207 if (
208 V.OnBoard(rx, ry) &&
209 this.getPiece(rx, ry) == piece &&
210 this.getColor(rx, ry) == color
211 ) {
212 return true;
213 }
214 }
215 return false;
216 }
217
218 getFlagsFen() {
219 // Return pawns flags
220 let flags = "";
221 for (let c of ["w", "b"]) {
222 for (let i = 0; i < 8; i++) flags += this.pawnFlags[c][i] ? "1" : "0";
223 }
224 return flags;
225 }
226
227 postPlay(move) {
228 super.postPlay(move);
229 const c = move.vanish[0].c;
230 const secondRank = { "w": 6, "b": 2 };
231 if (move.vanish[0].p == V.PAWN && secondRank[c] == move.start.x)
232 // This move turns off a 2-squares pawn flag
233 this.pawnFlags[c][move.start.y] = false;
234 }
235
236 static get VALUES() {
237 return {
238 p: 1,
239 r: 5,
240 n: 3,
241 b: 4,
242 q: 10,
243 k: 1000
244 };
245 }
246
247 static get SEARCH_DEPTH() {
248 return 2;
249 }
250 };