1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
, shuffle
} from "@/utils/alea";
5 export class CylinderRules
extends ChessRules
{
7 // Output basically x % 8 (circular board)
9 let res
= y
% V
.size
.y
;
15 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
17 // Don't add move twice when running on an infinite rank:
18 let infiniteSteps
= {};
19 outerLoop: for (let step
of steps
) {
20 if (!!infiniteSteps
[(-step
[0]) + "." + (-step
[1])]) continue;
22 let j
= V
.ComputeY(y
+ step
[1]);
23 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
24 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
25 if (oneStep
) continue outerLoop
;
27 j
= V
.ComputeY(j
+ step
[1]);
29 if (V
.OnBoard(i
, j
)) {
31 // Looped back onto initial square
32 infiniteSteps
[step
[0] + "." + step
[1]] = true;
33 else if (this.canTake([x
, y
], [i
, j
]))
34 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
40 getPotentialPawnMoves([x
, y
]) {
41 const color
= this.turn
;
43 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
44 const shiftX
= color
== "w" ? -1 : 1;
45 const startRank
= color
== "w" ? sizeX
- 2 : 1;
46 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
49 x
+ shiftX
== lastRank
50 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
52 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
54 for (let piece
of finalPieces
) {
56 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
64 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
67 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
71 for (let shiftY
of [-1, 1]) {
72 const nextFile
= V
.ComputeY(y
+ shiftY
);
74 this.board
[x
+ shiftX
][nextFile
] != V
.EMPTY
&&
75 this.canTake([x
, y
], [x
+ shiftX
, nextFile
])
77 for (let piece
of finalPieces
) {
79 this.getBasicMove([x
, y
], [x
+ shiftX
, nextFile
], {
89 const Lep
= this.epSquares
.length
;
90 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
93 epSquare
.x
== x
+ shiftX
&&
94 Math
.abs( (epSquare
.y
- y
) % V
.size
.y
) == 1
96 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
97 enpassantMove
.vanish
.push({
101 c: this.getColor(x
, epSquare
.y
)
103 moves
.push(enpassantMove
);
109 isAttackedByPawn([x
, y
], color
) {
110 let pawnShift
= (color
== "w" ? 1 : -1);
111 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< V
.size
.x
) {
112 for (let i
of [-1, 1]) {
113 const nextFile
= V
.ComputeY(y
+ i
);
115 this.getPiece(x
+ pawnShift
, nextFile
) == V
.PAWN
&&
116 this.getColor(x
+ pawnShift
, nextFile
) == color
125 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
126 for (let step
of steps
) {
127 let rx
= x
+ step
[0],
128 ry
= V
.ComputeY(y
+ step
[1]);
129 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
131 ry
= V
.ComputeY(ry
+ step
[1]);
135 this.getPiece(rx
, ry
) == piece
&&
136 this.getColor(rx
, ry
) == color
144 static get SEARCH_DEPTH() {
148 static get VALUES() {