Better Ball rules. Buggish but almost OK Synchrone variant
[vchess.git] / client / src / variants / Circular.js
CommitLineData
e3e2cc44
BA
1import { ChessRules, PiPo, Move } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
32f6285e 3import { shuffle } from "@/utils/alea";
e3e2cc44 4
32f6285e 5export 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 = [];
a8f0bbcb
BA
82 // Don't add move twice when running on an infinite file:
83 let infiniteSteps = {};
e3e2cc44 84 outerLoop: for (let step of steps) {
a8f0bbcb 85 if (!!infiniteSteps[(-step[0]) + "." + (-step[1])]) continue;
71ef1664 86 let i = V.ComputeX(x + step[0]);
e3e2cc44
BA
87 let j = y + step[1];
88 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
89 moves.push(this.getBasicMove([x, y], [i, j]));
90 if (oneStep !== undefined) continue outerLoop;
71ef1664 91 i = V.ComputeX(i + step[0]);
e3e2cc44
BA
92 j += step[1];
93 }
a8f0bbcb
BA
94 if (V.OnBoard(i, j)) {
95 if (i == x && j == y)
96 // Looped back onto initial square
97 infiniteSteps[step[0] + "." + step[1]] = true;
98 else if (this.canTake([x, y], [i, j]))
99 moves.push(this.getBasicMove([x, y], [i, j]));
100 }
e3e2cc44
BA
101 }
102 return moves;
103 }
104
e3e2cc44
BA
105 getPotentialPawnMoves([x, y]) {
106 const color = this.turn;
107 let moves = [];
108 const [sizeX, sizeY] = [V.size.x, V.size.y];
71ef1664
BA
109 // All pawns go in the same direction!
110 const shiftX = -1;
111 const startRank = color == "w" ? sizeX - 2 : 2;
112
113 // One square forward
114 const nextRow = V.ComputeX(x + shiftX);
115 if (this.board[nextRow][y] == V.EMPTY) {
116 moves.push(this.getBasicMove([x, y], [nextRow, y]));
117 if (
118 x == startRank &&
119 this.pawnFlags[color][y] &&
120 this.board[x + 2 * shiftX][y] == V.EMPTY
121 ) {
122 // Two squares jump
123 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
e3e2cc44 124 }
71ef1664
BA
125 }
126 // Captures
127 for (let shiftY of [-1, 1]) {
128 if (
129 y + shiftY >= 0 &&
130 y + shiftY < sizeY &&
131 this.board[nextRow][y + shiftY] != V.EMPTY &&
132 this.canTake([x, y], [nextRow, y + shiftY])
133 ) {
134 moves.push(this.getBasicMove([x, y], [nextRow, y + shiftY]));
e3e2cc44
BA
135 }
136 }
137
138 return moves;
139 }
140
71ef1664
BA
141 filterValid(moves) {
142 const filteredMoves = super.filterValid(moves);
143 // If at least one full move made, everything is allowed:
9842aca2 144 if (this.movesCount >= 2) return filteredMoves;
71ef1664
BA
145 // Else, forbid check:
146 const oppCol = V.GetOppCol(this.turn);
147 return filteredMoves.filter(m => {
148 this.play(m);
149 const res = !this.underCheck(oppCol);
150 this.undo(m);
151 return res;
152 });
153 }
154
68e19a44
BA
155 isAttackedByPawn([x, y], color) {
156 // pawn shift is always 1 (all pawns go the same way)
157 const attackerRow = V.ComputeX(x + 1);
158 for (let i of [-1, 1]) {
159 if (
160 y + i >= 0 &&
161 y + i < V.size.y &&
162 this.getPiece(attackerRow, y + i) == V.PAWN &&
163 this.getColor(attackerRow, y + i) == color
164 ) {
165 return true;
e3e2cc44
BA
166 }
167 }
168 return false;
169 }
170
68e19a44 171 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
e3e2cc44 172 for (let step of steps) {
71ef1664 173 let rx = V.ComputeX(x + step[0]),
e3e2cc44
BA
174 ry = y + step[1];
175 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
71ef1664 176 rx = V.ComputeX(rx + step[0]);
e3e2cc44
BA
177 ry += step[1];
178 }
179 if (
180 V.OnBoard(rx, ry) &&
68e19a44
BA
181 this.getPiece(rx, ry) == piece &&
182 this.getColor(rx, ry) == color
e3e2cc44
BA
183 ) {
184 return true;
185 }
186 }
187 return false;
188 }
71ef1664
BA
189
190 getFlagsFen() {
191 // Return pawns flags
192 let flags = "";
193 for (let c of ["w", "b"]) {
194 for (let i = 0; i < 8; i++) flags += this.pawnFlags[c][i] ? "1" : "0";
195 }
196 return flags;
197 }
198
3a2a7b5f
BA
199 postPlay(move) {
200 super.postPlay(move);
71ef1664 201 const c = move.vanish[0].c;
3a2a7b5f
BA
202 const secondRank = { "w": 6, "b": 2 };
203 if (move.vanish[0].p == V.PAWN && secondRank[c] == move.start.x)
71ef1664
BA
204 // This move turns off a 2-squares pawn flag
205 this.pawnFlags[c][move.start.y] = false;
206 }
207
208 static get VALUES() {
209 return {
210 p: 1,
211 r: 5,
212 n: 3,
213 b: 4,
214 q: 10,
215 k: 1000
216 };
217 }
b83a675a
BA
218
219 static get SEARCH_DEPTH() {
220 return 2;
221 }
e3e2cc44 222};