1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
, shuffle
} from "@/utils/alea";
5 export const VariantRules
= class CircularRules
extends ChessRules
{
6 static get HasCastle() {
10 static get HasEnpassant() {
14 static get CanFlip() {
20 w: [...Array(8).fill(true)], //pawns can move 2 squares?
21 b: [...Array(8).fill(true)]
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";
30 return this.pawnFlags
;
33 disaggregateFlags(flags
) {
34 this.pawnFlags
= flags
;
37 static GenRandInitFen(randomness
) {
39 return "8/8/pppppppp/rnbqkbnr/8/8/PPPPPPPP/RNBQKBNR w 0 1111111111111111";
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'];
49 let positions
= ArrayFun
.range(8);
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);
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);
69 // Get random square for queen
70 randIndex
= randInt(4);
71 const queenPos
= positions
[randIndex
];
72 positions
.splice(randIndex
, 1);
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];
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";
92 pieces
["b"].join("") +
94 pieces
["w"].join("").toUpperCase() +
95 // 16 flags: can pawns advance 2 squares?
96 " w 0 1111111111111111"
100 // Output basically x % 8 (circular board)
102 let res
= x
% V
.size
.x
;
108 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
110 outerLoop: for (let step
of steps
) {
111 let i
= V
.ComputeX(x
+ step
[0]);
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]);
119 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
]))
120 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
125 getPotentialPawnMoves([x
, y
]) {
126 const color
= this.turn
;
128 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
129 // All pawns go in the same direction!
131 const startRank
= color
== "w" ? sizeX
- 2 : 2;
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
]));
139 this.pawnFlags
[color
][y
] &&
140 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
143 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
147 for (let shiftY
of [-1, 1]) {
150 y
+ shiftY
< sizeY
&&
151 this.board
[nextRow
][y
+ shiftY
] != V
.EMPTY
&&
152 this.canTake([x
, y
], [nextRow
, y
+ shiftY
])
154 moves
.push(this.getBasicMove([x
, y
], [nextRow
, y
+ shiftY
]));
161 getPotentialKingMoves(sq
) {
162 return this.getSlideNJumpMoves(
164 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
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
=> {
177 const res
= !this.underCheck(oppCol
);
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]) {
190 this.getPiece(attackerRow
, y
+ i
) == V
.PAWN
&&
191 this.getColor(attackerRow
, y
+ i
) == color
199 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
200 for (let step
of steps
) {
201 let rx
= V
.ComputeX(x
+ step
[0]),
203 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
204 rx
= V
.ComputeX(rx
+ step
[0]);
209 this.getPiece(rx
, ry
) == piece
&&
210 this.getColor(rx
, ry
) == color
219 // Return pawns flags
221 for (let c
of ["w", "b"]) {
222 for (let i
= 0; i
< 8; i
++) flags
+= this.pawnFlags
[c
][i
] ? "1" : "0";
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;
236 static get VALUES() {
247 static get SEARCH_DEPTH() {