1 import { ChessRules
} from "@/base_rules";
3 export class WormholeRules
extends ChessRules
{
4 static get HasFlags() {
8 static get HasEnpassant() {
17 if (b
[0] == 'x') return 'x';
18 return ChessRules
.board2fen(b
);
22 if (f
== 'x') return V
.HOLE
;
23 return ChessRules
.fen2board(f
);
27 if (b
[0] == 'x') return "Wormhole/hole";
31 getSquareAfter(square
, movement
) {
33 if (Array
.isArray(movement
[0])) {
41 const tryMove
= (init
, shift
) => {
43 shift
[0] / Math
.abs(shift
[0]) || 0,
44 shift
[1] / Math
.abs(shift
[1]) || 0,
46 const nbSteps
= Math
.max(Math
.abs(shift
[0]), Math
.abs(shift
[1]));
47 let stepsAchieved
= 0;
48 let sq
= [init
[0] + step
[0], init
[1] + step
[1]];
49 while (V
.OnBoard(sq
[0],sq
[1])) {
50 if (this.board
[sq
[0]][sq
[1]] != V
.HOLE
)
52 if (stepsAchieved
< nbSteps
) {
58 if (stepsAchieved
< nbSteps
)
59 // The move is impossible
63 // First, apply shift1
64 let dest
= tryMove(square
, shift1
);
66 // A knight: apply second shift
67 dest
= tryMove(dest
, shift2
);
71 // NOTE (TODO?): some extra work done in some function because informations
72 // on one step should ease the computation for a step in the same direction.
85 // Decompose knight movements into one step orthogonal + one diagonal
119 getJumpMoves([x
, y
], steps
) {
121 for (let step
of steps
) {
122 const sq
= this.getSquareAfter([x
,y
], step
);
125 this.board
[sq
[0]][sq
[1]] == V
.EMPTY
||
126 this.canTake([x
, y
], sq
)
129 moves
.push(this.getBasicMove([x
, y
], sq
));
135 // What are the pawn moves from square x,y ?
136 getPotentialPawnMoves([x
, y
]) {
137 const color
= this.turn
;
139 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
140 const shiftX
= color
== "w" ? -1 : 1;
141 const startRank
= color
== "w" ? sizeX
- 2 : 1;
142 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
144 const sq1
= this.getSquareAfter([x
,y
], [shiftX
,0]);
145 if (sq1
&& this.board
[sq1
[0]][y
] == V
.EMPTY
) {
146 // One square forward (cannot be a promotion)
147 moves
.push(this.getBasicMove([x
, y
], [sq1
[0], y
]));
148 if (x
== startRank
) {
149 // If two squares after is available, then move is possible
150 const sq2
= this.getSquareAfter([x
,y
], [2*shiftX
,0]);
151 if (sq2
&& this.board
[sq2
[0]][y
] == V
.EMPTY
)
153 moves
.push(this.getBasicMove([x
, y
], [sq2
[0], y
]));
157 for (let shiftY
of [-1, 1]) {
158 const sq
= this.getSquareAfter([x
,y
], [shiftX
,shiftY
]);
161 this.board
[sq
[0]][sq
[1]] != V
.EMPTY
&&
162 this.canTake([x
, y
], [sq
[0], sq
[1]])
164 const finalPieces
= sq
[0] == lastRank
165 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
167 for (let piece
of finalPieces
) {
169 this.getBasicMove([x
, y
], [sq
[0], sq
[1]], {
181 getPotentialRookMoves(sq
) {
182 return this.getJumpMoves(sq
, V
.steps
[V
.ROOK
]);
185 getPotentialKnightMoves(sq
) {
186 return this.getJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
189 getPotentialBishopMoves(sq
) {
190 return this.getJumpMoves(sq
, V
.steps
[V
.BISHOP
]);
193 getPotentialQueenMoves(sq
) {
194 return this.getJumpMoves(
196 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
200 getPotentialKingMoves(sq
) {
201 return this.getJumpMoves(sq
, V
.steps
[V
.KING
]);
204 isAttackedByJump([x
, y
], color
, piece
, steps
) {
205 for (let step
of steps
) {
206 const sq
= this.getSquareAfter([x
,y
], step
);
209 this.getPiece(sq
[0], sq
[1]) == piece
&&
210 this.getColor(sq
[0], sq
[1]) == color
218 isAttackedByPawn([x
, y
], color
) {
219 const pawnShift
= (color
== "w" ? 1 : -1);
220 for (let i
of [-1, 1]) {
221 const sq
= this.getSquareAfter([x
,y
], [pawnShift
,i
]);
224 this.getPiece(sq
[0], sq
[1]) == V
.PAWN
&&
225 this.getColor(sq
[0], sq
[1]) == color
233 isAttackedByRook(sq
, color
) {
234 return this.isAttackedByJump(sq
, color
, V
.ROOK
, V
.steps
[V
.ROOK
]);
237 isAttackedByKnight(sq
, color
) {
238 // NOTE: knight attack is not symmetric in this variant:
239 // steps order need to be reversed.
240 return this.isAttackedByJump(
244 V
.steps
[V
.KNIGHT
].map(s
=> s
.reverse())
248 isAttackedByBishop(sq
, color
) {
249 return this.isAttackedByJump(sq
, color
, V
.BISHOP
, V
.steps
[V
.BISHOP
]);
252 isAttackedByQueen(sq
, color
) {
253 return this.isAttackedByJump(
257 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
261 isAttackedByKing(sq
, color
) {
262 return this.isAttackedByJump(sq
, color
, V
.KING
, V
.steps
[V
.KING
]);
265 // NOTE: altering move in getBasicMove doesn't work and wouldn't be logical.
266 // This is a side-effect on board generated by the move.
267 static PlayOnBoard(board
, move) {
268 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
269 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
273 if (this.atLeastOneMove()) return "*";
274 // No valid move: I lose
275 return this.turn
== "w" ? "0-1" : "1-0";
278 static get SEARCH_DEPTH() {
284 for (let i
= 0; i
< V
.size
.x
; i
++) {
285 for (let j
= 0; j
< V
.size
.y
; j
++) {
286 if (![V
.EMPTY
,V
.HOLE
].includes(this.board
[i
][j
])) {
287 const sign
= this.getColor(i
, j
) == "w" ? 1 : -1;
288 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
, j
)];
296 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
297 // Indicate start square + dest square, because holes distort the board
299 (piece
!= V
.PAWN
? piece
.toUpperCase() : "") +
300 V
.CoordsToSquare(move.start
) +
301 (move.vanish
.length
> move.appear
.length
? "x" : "") +
302 V
.CoordsToSquare(move.end
);
303 if (piece
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
)
305 notation
+= "=" + move.appear
[0].p
.toUpperCase();