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