1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
, sample
} from "@/utils/alea";
5 export class CoregalRules
extends ChessRules
{
6 static IsGoodPosition(position
) {
7 if (!super.IsGoodPosition(position
)) return false;
8 // Check that at least one queen of each color is there:
10 for (let row
of rows
) {
11 for (let i
= 0; i
< row
.length
; i
++)
12 if (['Q','q'].includes(row
[i
])) queens
[row
[i
]] = true;
14 if (Object
.keys(queens
).length
!= 2) return false;
18 static IsGoodFlags(flags
) {
19 return !!flags
.match(/^[a-z]{8,8}$/);
22 getCheckSquares(color
) {
24 const oppCol
= V
.GetOppCol(color
);
25 if (this.isAttacked(this.kingPos
[color
], oppCol
))
26 squares
.push(this.kingPos
[color
]);
27 for (let i
=0; i
<V
.size
.x
; i
++) {
28 for (let j
=0; j
<V
.size
.y
; j
++) {
30 this.getColor(i
, j
) == color
&&
31 this.getPiece(i
, j
) == V
.QUEEN
&&
32 this.isAttacked([i
, j
], oppCol
)
41 static GenRandInitFen(randomness
) {
43 // Castle flags here indicate pieces positions (if can castle)
44 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 adehadeh -";
46 let pieces
= { w: new Array(8), b: new Array(8) };
48 for (let c
of ["w", "b"]) {
49 if (c
== 'b' && randomness
== 1) {
50 pieces
['b'] = pieces
['w'];
55 // Get random squares for king and queen between b and g files
56 let randIndex
= randInt(6);
57 let kingPos
= randIndex
+ 1;
58 randIndex
= randInt(5);
59 if (randIndex
>= kingPos
) randIndex
++;
60 let queenPos
= randIndex
+ 1;
62 // Get random squares for rooks to the left and right of the queen
63 // and king: not all squares of the same colors (for bishops).
64 const minQR
= Math
.min(kingPos
, queenPos
);
65 const maxQR
= Math
.max(kingPos
, queenPos
);
66 let rook1Pos
= randInt(minQR
);
67 let rook2Pos
= 7 - randInt(7 - maxQR
);
69 // Now, if we are unlucky all these 4 pieces may be on the same color.
70 const rem2
= [kingPos
, queenPos
, rook1Pos
, rook2Pos
].map(pos
=> pos
% 2);
71 if (rem2
.every(r
=> r
== 0) || rem2
.every(r
=> r
== 1)) {
72 // Shift a random of these pieces to the left or right
75 if (rook1Pos
== 0) rook1Pos
++;
79 if (Math
.random() < 0.5) kingPos
++;
83 if (Math
.random() < 0.5) queenPos
++;
87 if (rook2Pos
== 7) rook2Pos
--;
92 let bishop1Options
= { 0: true, 2: true, 4: true, 6: true };
93 let bishop2Options
= { 1: true, 3: true, 5: true, 7: true };
94 [kingPos
, queenPos
, rook1Pos
, rook2Pos
].forEach(pos
=> {
95 if (!!bishop1Options
[pos
]) delete bishop1Options
[pos
];
96 else if (!!bishop2Options
[pos
]) delete bishop2Options
[pos
];
98 const bishop1Pos
= parseInt(sample(Object
.keys(bishop1Options
), 1)[0]);
99 const bishop2Pos
= parseInt(sample(Object
.keys(bishop2Options
), 1)[0]);
101 // Knights' positions are now determined
103 kingPos
, queenPos
, rook1Pos
, rook2Pos
, bishop1Pos
, bishop2Pos
105 const [knight1Pos
, knight2Pos
] =
106 ArrayFun
.range(8).filter(pos
=> !forbidden
.includes(pos
));
108 pieces
[c
][rook1Pos
] = "r";
109 pieces
[c
][knight1Pos
] = "n";
110 pieces
[c
][bishop1Pos
] = "b";
111 pieces
[c
][queenPos
] = "q";
112 pieces
[c
][kingPos
] = "k";
113 pieces
[c
][bishop2Pos
] = "b";
114 pieces
[c
][knight2Pos
] = "n";
115 pieces
[c
][rook2Pos
] = "r";
116 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(queenPos
) +
117 V
.CoordToColumn(kingPos
) + V
.CoordToColumn(rook2Pos
);
119 // Add turn + flags + enpassant
121 pieces
["b"].join("") +
122 "/pppppppp/8/8/8/8/PPPPPPPP/" +
123 pieces
["w"].join("").toUpperCase() +
124 " w 0 " + flags
+ " -"
129 // white a-castle, h-castle, black a-castle, h-castle
130 this.castleFlags
= { w: [...Array(4)], b: [...Array(4)] };
131 for (let i
= 0; i
< 8; i
++) {
132 this.castleFlags
[i
< 4 ? "w" : "b"][i
% 4] =
133 V
.ColumnToCoord(fenflags
.charAt(i
));
137 getPotentialQueenMoves(sq
) {
138 return super.getPotentialQueenMoves(sq
).concat(this.getCastleMoves(sq
));
141 getCastleMoves([x
, y
], castleInCheck
) {
143 // const c = this.getColor(x, y);
144 // if (x != (c == "w" ? V.size.x - 1 : 0) || y != this.INIT_COL_KING[c])
145 // return []; //x isn't first rank, or king has moved (shortcut)
148 // const oppCol = V.GetOppCol(c);
151 // // King, then rook:
152 // const finalSquares = [
154 // [V.size.y - 2, V.size.y - 3]
156 // castlingCheck: for (
157 // let castleSide = 0;
159 // castleSide++ //large, then small
161 // if (this.castleFlags[c][castleSide] >= V.size.y) continue;
162 // // If this code is reached, rooks and king are on initial position
164 // // NOTE: in some variants this is not a rook, but let's keep variable name
165 // const rookPos = this.castleFlags[c][castleSide];
166 // const castlingPiece = this.getPiece(x, rookPos);
167 // if (this.getColor(x, rookPos) != c)
168 // // Rook is here but changed color (see Benedict)
171 // // Nothing on the path of the king ? (and no checks)
172 // const finDist = finalSquares[castleSide][0] - y;
173 // let step = finDist / Math.max(1, Math.abs(finDist));
177 // (!castleInCheck && this.isAttacked([x, i], oppCol)) ||
178 // (this.board[x][i] != V.EMPTY &&
179 // // NOTE: next check is enough, because of chessboard constraints
180 // (this.getColor(x, i) != c ||
181 // ![V.KING, castlingPiece].includes(this.getPiece(x, i))))
183 // continue castlingCheck;
186 // } while (i != finalSquares[castleSide][0]);
188 // // Nothing on the path to the rook?
189 // step = castleSide == 0 ? -1 : 1;
190 // for (i = y + step; i != rookPos; i += step) {
191 // if (this.board[x][i] != V.EMPTY) continue castlingCheck;
194 // // Nothing on final squares, except maybe king and castling rook?
195 // for (i = 0; i < 2; i++) {
197 // this.board[x][finalSquares[castleSide][i]] != V.EMPTY &&
198 // this.getPiece(x, finalSquares[castleSide][i]) != V.KING &&
199 // finalSquares[castleSide][i] != rookPos
201 // continue castlingCheck;
205 // // If this code is reached, castle is valid
209 // new PiPo({ x: x, y: finalSquares[castleSide][0], p: V.KING, c: c }),
210 // new PiPo({ x: x, y: finalSquares[castleSide][1], p: castlingPiece, c: c })
213 // new PiPo({ x: x, y: y, p: V.KING, c: c }),
214 // new PiPo({ x: x, y: rookPos, p: castlingPiece, c: c })
217 // Math.abs(y - rookPos) <= 2
218 // ? { x: x, y: rookPos }
219 // : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) }
228 const oppCol
= V
.GetOppCol(color
);
229 if (this.isAttacked(this.kingPos
[color
], oppCol
)) return true;
230 for (let i
=0; i
<V
.size
.x
; i
++) {
231 for (let j
=0; j
<V
.size
.y
; j
++) {
233 this.getColor(i
, j
) == color
&&
234 this.getPiece(i
, j
) == V
.QUEEN
&&
235 this.isAttacked([i
, j
], oppCol
)
244 updateCastleFlags(move, piece
) {
245 // const c = V.GetOppCol(this.turn);
246 // const firstRank = (c == "w" ? V.size.x - 1 : 0);
247 // // Update castling flags if rooks are moved
248 // const oppCol = V.GetOppCol(c);
249 // const oppFirstRank = V.size.x - 1 - firstRank;
250 // if (piece == V.KING && move.appear.length > 0)
251 // this.castleFlags[c] = [V.size.y, V.size.y];
253 // move.start.x == firstRank && //our rook moves?
254 // this.castleFlags[c].includes(move.start.y)
256 // const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
257 // this.castleFlags[c][flagIdx] = V.size.y;
259 // move.end.x == oppFirstRank && //we took opponent rook?
260 // this.castleFlags[oppCol].includes(move.end.y)
262 // const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
263 // this.castleFlags[oppCol][flagIdx] = V.size.y;
267 // NOTE: do not set queen value to 1000 or so, because there may be several.