1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { shuffle
} from "@/utils/alea";
5 export class CircularRules
extends ChessRules
{
7 static get HasCastle() {
11 static get HasEnpassant() {
15 static get CanFlip() {
21 w: [...Array(8).fill(true)], //pawns can move 2 squares?
22 b: [...Array(8).fill(true)]
24 for (let c
of ["w", "b"]) {
25 for (let i
= 0; i
< 8; i
++)
26 this.pawnFlags
[c
][i
] = fenflags
.charAt((c
== "w" ? 0 : 8) + i
) == "1";
31 return this.pawnFlags
;
34 disaggregateFlags(flags
) {
35 this.pawnFlags
= flags
;
38 static GenRandInitFen(randomness
) {
39 if (randomness
== 0) {
40 return "8/8/pppppppp/rnbqkbnr/8/8/PPPPPPPP/RNBQKBNR " +
41 "w 0 1111111111111111";
44 let pieces
= { w: new Array(8), b: new Array(8) };
45 // Shuffle pieces on first and last rank
46 for (let c
of ["w", "b"]) {
47 if (c
== 'b' && randomness
== 1) {
48 pieces
['b'] = pieces
['w'];
52 // Get random squares for every piece, totally freely
53 let positions
= shuffle(ArrayFun
.range(8));
54 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
55 const rem2
= positions
[0] % 2;
56 if (rem2
== positions
[1] % 2) {
57 // Fix bishops (on different colors)
58 for (let i
=2; i
<8; i
++) {
59 if (positions
[i
] % 2 != rem2
)
60 [positions
[1], positions
[i
]] = [positions
[i
], positions
[1]];
63 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
67 pieces
["b"].join("") +
69 pieces
["w"].join("").toUpperCase() +
70 // 16 flags: can pawns advance 2 squares?
71 " w 0 1111111111111111"
75 // Output basically x % 8 (circular board)
77 let res
= x
% V
.size
.x
;
83 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
85 // Don't add move twice when running on an infinite file:
86 let infiniteSteps
= {};
87 outerLoop: for (let step
of steps
) {
88 if (!!infiniteSteps
[(-step
[0]) + "." + (-step
[1])]) continue;
89 let i
= V
.ComputeX(x
+ step
[0]);
91 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
92 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
93 if (oneStep
!== undefined) continue outerLoop
;
94 i
= V
.ComputeX(i
+ step
[0]);
97 if (V
.OnBoard(i
, j
)) {
99 // Looped back onto initial square
100 infiniteSteps
[step
[0] + "." + step
[1]] = true;
101 else if (this.canTake([x
, y
], [i
, j
]))
102 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
108 getPotentialPawnMoves([x
, y
]) {
109 const color
= this.turn
;
111 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
112 // All pawns go in the same direction!
114 const startRank
= color
== "w" ? sizeX
- 2 : 2;
116 // One square forward
117 const nextRow
= V
.ComputeX(x
+ shiftX
);
118 if (this.board
[nextRow
][y
] == V
.EMPTY
) {
119 moves
.push(this.getBasicMove([x
, y
], [nextRow
, y
]));
122 this.pawnFlags
[color
][y
] &&
123 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
126 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
130 for (let shiftY
of [-1, 1]) {
133 y
+ shiftY
< sizeY
&&
134 this.board
[nextRow
][y
+ shiftY
] != V
.EMPTY
&&
135 this.canTake([x
, y
], [nextRow
, y
+ shiftY
])
137 moves
.push(this.getBasicMove([x
, y
], [nextRow
, y
+ shiftY
]));
145 const filteredMoves
= super.filterValid(moves
);
146 // If at least one full move made, everything is allowed:
147 if (this.movesCount
>= 2) return filteredMoves
;
148 // Else, forbid check:
149 const oppCol
= V
.GetOppCol(this.turn
);
150 return filteredMoves
.filter(m
=> {
152 const res
= !this.underCheck(oppCol
);
158 isAttackedByPawn([x
, y
], color
) {
159 // pawn shift is always 1 (all pawns go the same way)
160 const attackerRow
= V
.ComputeX(x
+ 1);
161 for (let i
of [-1, 1]) {
165 this.getPiece(attackerRow
, y
+ i
) == V
.PAWN
&&
166 this.getColor(attackerRow
, y
+ i
) == color
174 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
175 for (let step
of steps
) {
176 let rx
= V
.ComputeX(x
+ step
[0]),
178 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
179 rx
= V
.ComputeX(rx
+ step
[0]);
184 this.getPiece(rx
, ry
) == piece
&&
185 this.getColor(rx
, ry
) == color
194 // Return pawns flags
196 for (let c
of ["w", "b"]) {
197 for (let i
= 0; i
< 8; i
++) flags
+= this.pawnFlags
[c
][i
] ? "1" : "0";
203 super.postPlay(move);
204 const c
= move.vanish
[0].c
;
205 const secondRank
= { "w": 6, "b": 2 };
206 if (move.vanish
[0].p
== V
.PAWN
&& secondRank
[c
] == move.start
.x
)
207 // This move turns off a 2-squares pawn flag
208 this.pawnFlags
[c
][move.start
.y
] = false;
211 static get VALUES() {
222 static get SEARCH_DEPTH() {