Fix Alapo
[vchess.git] / client / src / variants / Alapo.js
1 import { ChessRules } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt } from "@/utils/alea";
4
5 export class AlapoRules extends ChessRules {
6
7 static get HasFlags() {
8 return false;
9 }
10
11 static get HasEnpassant() {
12 return false;
13 }
14
15 static get Lines() {
16 return [
17 [[1, 0], [1, 6]],
18 [[5, 0], [5, 6]]
19 ];
20 }
21
22 static get PIECES() {
23 return [V.ROOK, V.BISHOP, V.QUEEN, V.ROOK_S, V.BISHOP_S, V.QUEEN_S];
24 }
25
26 static get ROOK_S() {
27 return "t";
28 }
29 static get BISHOP_S() {
30 return "c";
31 }
32 static get QUEEN_S() {
33 return "s";
34 }
35
36 getPotentialMinirookMoves(sq) {
37 return super.getSlideNJumpMoves(sq, V.steps[V.ROOK], "oneStep");
38 }
39 getPotentialMinibishopMoves(sq) {
40 return super.getSlideNJumpMoves(sq, V.steps[V.BISHOP], "oneStep");
41 }
42 getPotentialMiniqueenMoves(sq) {
43 return (
44 super.getSlideNJumpMoves(
45 sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep")
46 );
47 }
48
49 getPotentialMovesFrom(sq) {
50 switch (this.getPiece(sq[0], sq[1])) {
51 case V.ROOK: return super.getPotentialRookMoves(sq);
52 case V.BISHOP: return super.getPotentialBishopMoves(sq);
53 case V.QUEEN: return super.getPotentialQueenMoves(sq);
54 case V.ROOK_S: return this.getPotentialMinirookMoves(sq);
55 case V.BISHOP_S: return this.getPotentialMinibishopMoves(sq);
56 case V.QUEEN_S: return this.getPotentialMiniqueenMoves(sq);
57 }
58 return [];
59 }
60
61 static get size() {
62 return { x: 6, y: 6 };
63 }
64
65 getPpath(b, color, score, orientation) {
66 // 'i' for "inversed":
67 const suffix = (b[0] == orientation ? "" : "i");
68 return "Alapo/" + b + suffix;
69 }
70
71 static GenRandInitFen(randomness) {
72 if (randomness == 0)
73 return "rbqqbr/tcssct/6/6/TCSSCT/RBQQBR w 0";
74
75 const piece2pawn = {
76 r: 't',
77 q: 's',
78 b: 'c'
79 };
80
81 let pieces = { w: new Array(6), b: new Array(6) };
82 // Shuffle pieces on first (and last rank if randomness == 2)
83 for (let c of ["w", "b"]) {
84 if (c == 'b' && randomness == 1) {
85 pieces['b'] = pieces['w'];
86 break;
87 }
88
89 let positions = ArrayFun.range(6);
90
91 // Get random squares for bishops
92 let randIndex = 2 * randInt(3);
93 const bishop1Pos = positions[randIndex];
94 let randIndex_tmp = 2 * randInt(3) + 1;
95 const bishop2Pos = positions[randIndex_tmp];
96 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
97 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
98
99 // Get random square for queens
100 randIndex = randInt(4);
101 const queen1Pos = positions[randIndex];
102 positions.splice(randIndex, 1);
103 randIndex = randInt(3);
104 const queen2Pos = positions[randIndex];
105 positions.splice(randIndex, 1);
106
107 // Rooks positions are now fixed,
108 const rook1Pos = positions[0];
109 const rook2Pos = positions[1];
110
111 pieces[c][rook1Pos] = "r";
112 pieces[c][bishop1Pos] = "b";
113 pieces[c][queen1Pos] = "q";
114 pieces[c][queen2Pos] = "q";
115 pieces[c][bishop2Pos] = "b";
116 pieces[c][rook2Pos] = "r";
117 }
118 return (
119 pieces["b"].join("") + "/" +
120 pieces["b"].map(p => piece2pawn[p]).join() +
121 "/8/8/8/8/" +
122 pieces["w"].map(p => piece2pawn[p].toUpperCase()).join() + "/" +
123 pieces["w"].join("").toUpperCase() +
124 " w 0"
125 );
126 }
127
128 static IsGoodPosition(position) {
129 if (position.length == 0) return false;
130 const rows = position.split("/");
131 if (rows.length != V.size.x) return false;
132 // Just check that at least one piece of each color is there:
133 let pieces = { "w": 0, "b": 0 };
134 for (let row of rows) {
135 let sumElts = 0;
136 for (let i = 0; i < row.length; i++) {
137 const lowerRi = row[i].toLowerCase();
138 if (V.PIECES.includes(lowerRi)) {
139 pieces[row[i] == lowerRi ? "b" : "w"]++;
140 sumElts++;
141 }
142 else {
143 const num = parseInt(row[i], 10);
144 if (isNaN(num)) return false;
145 sumElts += num;
146 }
147 }
148 if (sumElts != V.size.y) return false;
149 }
150 if (Object.values(pieces).some(v => v == 0)) return false;
151 return true;
152 }
153
154 // Find possible captures by opponent on [x, y]
155 findCaptures([x, y]) {
156 const color = this.getColor(x, y);
157 let moves = [];
158 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
159 const oppCol = V.GetOppCol(color);
160 for (let loop = 0; loop < steps.length; loop++) {
161 const step = steps[loop];
162 let i = x + step[0];
163 let j = y + step[1];
164 let stepsAfter = 1;
165 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
166 i += step[0];
167 j += step[1];
168 stepsAfter++;
169 }
170 if (
171 V.OnBoard(i, j) &&
172 this.board[i][j] != V.EMPTY &&
173 this.getColor(i, j) == oppCol
174 ) {
175 const oppPiece = this.getPiece(i, j);
176 if (
177 (
178 stepsAfter >= 2 &&
179 [V.ROOK_S, V.BISHOP_S, V.QUEEN_S].includes(oppPiece)
180 )
181 ||
182 (
183 [V.BISHOP, V.BISHOP_S].includes(oppPiece) &&
184 step.some(e => e == 0)
185 )
186 ||
187 (
188 [V.ROOK, V.ROOK_S].includes(oppPiece) &&
189 step.every(e => e != 0)
190 )
191 ) {
192 continue;
193 }
194 return true;
195 }
196 }
197 return false;
198 }
199
200 postPlay() {}
201 postUndo() {}
202
203 getCheckSquares() {
204 return [];
205 }
206 filterValid(moves) {
207 return moves;
208 }
209
210 getCurrentScore() {
211 // Try both colors (to detect potential suicides)
212 for (let c of ['w', 'b']) {
213 const oppCol = V.GetOppCol(c);
214 const goal = (c == 'w' ? 0 : 5);
215 if (
216 this.board[goal].some(
217 (b,j) => b[0] == c && !this.findCaptures([goal, j])
218 )
219 ) {
220 return c == 'w' ? "1-0" : "0-1";
221 }
222 }
223 return super.getCurrentScore();
224 }
225
226 static get VALUES() {
227 return {
228 r: 5,
229 b: 3,
230 q: 9,
231 t: 3,
232 c: 2,
233 s: 5
234 };
235 }
236
237 };