1 class UltimaRules
extends ChessRules
5 if (b
[1] == "m") //'m' for Immobilizer (I is too similar to 1)
7 return b
; //usual piece
12 this.kingPos
= {'w':[-1,-1], 'b':[-1,-1]};
13 const fenParts
= fen
.split(" ");
14 const position
= fenParts
[0].split("/");
15 for (let i
=0; i
<position
.length
; i
++)
18 for (let j
=0; j
<position
[i
].length
; j
++)
20 switch (position
[i
].charAt(j
))
23 this.kingPos
['b'] = [i
,k
];
26 this.kingPos
['w'] = [i
,k
];
29 let num
= parseInt(position
[i
].charAt(j
));
36 this.epSquares
= []; //no en-passant here
41 // TODO: for compatibility?
42 this.castleFlags
= {"w":[false,false], "b":[false,false]};
45 static get IMMOBILIZER() { return 'm'; }
46 // Although other pieces keep their names here for coding simplicity,
48 // - a "rook" is a coordinator, capturing by coordinating with the king
49 // - a "knight" is a long-leaper, capturing as in draughts
50 // - a "bishop" is a chameleon, capturing as its prey
51 // - a "queen" is a withdrawer, capturing by moving away from pieces
53 // Is piece on square (x,y) immobilized?
56 const piece
= this.getPiece(x
,y
);
57 const color
= this.getColor(x
,y
);
58 const oppCol
= this.getOppCol(color
);
59 const V
= VariantRules
;
60 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
61 const [sizeX
,sizeY
] = V
.size
;
63 for (let step
of adjacentSteps
)
65 const [i
,j
] = [x
+step
[0],y
+step
[1]];
66 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
] != V
.EMPTY
67 && this.getColor(i
,j
) == oppCol
)
69 const oppPiece
= this.getPiece(i
,j
);
70 if (oppPiece
== V
.IMMOBILIZER
)
72 // Moving is impossible only if this immobilizer is not neutralized
73 for (let step2
of adjacentSteps
)
75 const [i2
,j2
] = [i
+step2
[0],j
+step2
[1]];
76 if (i2
== x
&& j2
== y
)
77 continue; //skip initial piece!
78 if (i2
>=0 && i2
<sizeX
&& j2
>=0 && j2
<sizeY
79 && this.board
[i2
][j2
] != V
.EMPTY
&& this.getColor(i2
,j2
) == color
)
81 if ([V
.BISHOP
,V
.IMMOBILIZER
].includes(this.getPiece(i2
,j2
)))
85 return true; //immobilizer isn't neutralized
87 // Chameleons can't be immobilized twice, because there is only one immobilizer
88 if (oppPiece
== V
.BISHOP
&& piece
== V
.IMMOBILIZER
)
95 getPotentialMovesFrom([x
,y
])
97 // Pre-check: is thing on this square immobilized?
98 if (this.isImmobilized([x
,y
]))
100 switch (this.getPiece(x
,y
))
102 case VariantRules
.IMMOBILIZER:
103 return this.getPotentialImmobilizerMoves([x
,y
]);
105 return super.getPotentialMovesFrom([x
,y
]);
109 getSlideNJumpMoves([x
,y
], steps
, oneStep
)
111 const color
= this.getColor(x
,y
);
112 const piece
= this.getPiece(x
,y
);
114 const [sizeX
,sizeY
] = VariantRules
.size
;
116 for (let step
of steps
)
120 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
121 && this.board
[i
][j
] == VariantRules
.EMPTY
)
123 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
124 if (oneStep
!== undefined)
129 // Only king can take on occupied square:
130 if (piece
==VariantRules
.KING
&& i
>=0 && i
<sizeX
&& j
>=0
131 && j
<sizeY
&& this.canTake([x
,y
], [i
,j
]))
133 moves
.push(this.getBasicMove([x
,y
], [i
,j
]));
139 // Modify capturing moves among listed pawn moves
140 addPawnCaptures(moves
, byChameleon
)
142 const steps
= VariantRules
.steps
[VariantRules
.ROOK
];
143 const [sizeX
,sizeY
] = VariantRules
.size
;
144 const color
= this.turn
;
145 const oppCol
= this.getOppCol(color
);
147 if (!!byChameleon
&& m
.start
.x
!=m
.end
.x
&& m
.start
.y
!=m
.end
.y
)
148 return; //chameleon not moving as pawn
149 // Try capturing in every direction
150 for (let step
of steps
)
152 const sq2
= [m
.end
.x
+2*step
[0],m
.end
.y
+2*step
[1]];
153 if (sq2
[0]>=0 && sq2
[0]<sizeX
&& sq2
[1]>=0 && sq2
[1]<sizeY
154 && this.board
[sq2
[0]][sq2
[1]] != VariantRules
.EMPTY
155 && this.getColor(sq2
[0],sq2
[1]) == color
)
158 const sq1
= [m
.end
.x
+step
[0],m
.end
.y
+step
[1]];
159 if (this.board
[sq1
[0]][sq1
[1]] != VariantRules
.EMPTY
160 && this.getColor(sq1
[0],sq1
[1]) == oppCol
)
162 const piece1
= this.getPiece(sq1
[0],sq1
[1]);
163 if (!byChameleon
|| piece1
== VariantRules
.PAWN
)
165 m
.vanish
.push(new PiPo({
179 getPotentialPawnMoves([x
,y
])
181 let moves
= super.getPotentialRookMoves([x
,y
]);
182 this.addPawnCaptures(moves
);
186 addRookCaptures(moves
, byChameleon
)
188 const color
= this.turn
;
189 const oppCol
= this.getOppCol(color
);
190 const kp
= this.kingPos
[color
];
192 // Check piece-king rectangle (if any) corners for enemy pieces
193 if (m
.end
.x
== kp
[0] || m
.end
.y
== kp
[1])
194 return; //"flat rectangle"
195 const corner1
= [m
.end
.x
, kp
[1]];
196 const corner2
= [kp
[0], m
.end
.y
];
197 for (let [i
,j
] of [corner1
,corner2
])
199 if (this.board
[i
][j
] != VariantRules
.EMPTY
&& this.getColor(i
,j
) == oppCol
)
201 const piece
= this.getPiece(i
,j
);
202 if (!byChameleon
|| piece
== VariantRules
.ROOK
)
204 m
.vanish
.push( new PiPo({
217 getPotentialRookMoves(sq
)
219 let moves
= super.getPotentialQueenMoves(sq
);
220 this.addRookCaptures(moves
);
225 getKnightCaptures(startSquare
, byChameleon
)
227 // Look in every direction for captures
228 const V
= VariantRules
;
229 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
230 const [sizeX
,sizeY
] = V
.size
;
231 const color
= this.turn
;
232 const oppCol
= this.getOppCol(color
);
234 const [x
,y
] = [startSquare
[0],startSquare
[1]];
235 const piece
= this.getPiece(x
,y
); //might be a chameleon!
237 for (let step
of steps
)
239 let [i
,j
] = [x
+step
[0], y
+step
[1]];
240 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
]==V
.EMPTY
)
245 if (i
<0 || i
>=sizeX
|| j
<0 || j
>=sizeY
|| this.getColor(i
,j
)==color
246 || (!!byChameleon
&& this.getPiece(i
,j
)!=V
.KNIGHT
))
250 // last(thing), cur(thing) : stop if "cur" is our color, or beyond board limits,
251 // or if "last" isn't empty and cur neither. Otherwise, if cur is empty then
252 // add move until cur square; if cur is occupied then stop if !!byChameleon and
253 // the square not occupied by a leaper.
255 let cur
= [i
+step
[0],j
+step
[1]];
256 let vanished
= [ new PiPo({x:x
,y:y
,c:color
,p:piece
}) ];
257 while (cur
[0]>=0 && cur
[0]<sizeX
&& cur
[1]>=0 && cur
[1]<sizeY
)
259 if (this.board
[last
[0]][last
[1]] != V
.EMPTY
)
261 const oppPiece
= this.getPiece(last
[0],last
[1]);
262 if (!!byChameleon
&& oppPiece
!= V
.KNIGHT
)
265 vanished
.push( new PiPo({x:last
[0],y:last
[1],c:oppCol
,p:oppPiece
}) );
267 if (this.board
[cur
[0]][cur
[1]] != V
.EMPTY
)
269 if (this.getColor(cur
[0],cur
[1]) == color
270 || this.board
[last
[0]][last
[1]] != V
.EMPTY
) //TODO: redundant test
277 moves
.push(new Move({
278 appear: [ new PiPo({x:cur
[0],y:cur
[1],c:color
,p:piece
}) ],
279 vanish: JSON
.parse(JSON
.stringify(vanished
)), //TODO: required?
281 end: {x:cur
[0],y:cur
[1]}
284 last
= [last
[0]+step
[0],last
[1]+step
[1]];
285 cur
= [cur
[0]+step
[0],cur
[1]+step
[1]];
292 getPotentialKnightMoves(sq
)
294 return super.getPotentialQueenMoves(sq
).concat(this.getKnightCaptures(sq
));
297 getPotentialBishopMoves([x
,y
])
299 let moves
= super.getPotentialQueenMoves([x
,y
])
300 .concat(this.getKnightCaptures([x
,y
],"asChameleon"));
301 // No "king capture" because king cannot remain under check
302 this.addPawnCaptures(moves
, "asChameleon");
303 this.addRookCaptures(moves
, "asChameleon");
304 this.addQueenCaptures(moves
, "asChameleon");
305 // Post-processing: merge similar moves, concatenating vanish arrays
306 let mergedMoves
= {};
307 const [sizeX
,sizeY
] = VariantRules
.size
;
309 const key
= m
.end
.x
+ sizeX
* m
.end
.y
;
310 if (!mergedMoves
[key
])
311 mergedMoves
[key
] = m
;
314 for (let i
=1; i
<m
.vanish
.length
; i
++)
315 mergedMoves
[key
].vanish
.push(m
.vanish
[i
]);
318 // Finally return an array
320 Object
.keys(mergedMoves
).forEach(k
=> { moves
.push(mergedMoves
[k
]); });
325 addQueenCaptures(moves
, byChameleon
)
327 if (moves
.length
== 0)
329 const [x
,y
] = [moves
[0].start
.x
,moves
[0].start
.y
];
330 const V
= VariantRules
;
331 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
332 let capturingDirections
= [];
333 const color
= this.turn
;
334 const oppCol
= this.getOppCol(color
);
335 const [sizeX
,sizeY
] = V
.size
;
336 adjacentSteps
.forEach(step
=> {
337 const [i
,j
] = [x
+step
[0],y
+step
[1]];
338 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
339 && this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
,j
) == oppCol
340 && (!byChameleon
|| this.getPiece(i
,j
) == V
.QUEEN
))
342 capturingDirections
.push(step
);
347 m
.end
.x
!=x
? (m
.end
.x
-x
)/Math
.abs(m
.end
.x
-x
) : 0,
348 m
.end
.y
!=y
? (m
.end
.y
-y
)/Math
.abs(m
.end
.y
-y
) : 0
350 // NOTE: includes() and even _.isEqual() functions fail...
351 // TODO: this test should be done only once per direction
352 if (capturingDirections
.some(dir
=>
353 { return (dir
[0]==-step
[0] && dir
[1]==-step
[1]); }))
355 const [i
,j
] = [x
-step
[0],y
-step
[1]];
356 m
.vanish
.push(new PiPo({
359 p:this.getPiece(i
,j
),
366 getPotentialQueenMoves(sq
)
368 let moves
= super.getPotentialQueenMoves(sq
);
369 this.addQueenCaptures(moves
);
373 getPotentialImmobilizerMoves(sq
)
375 // Immobilizer doesn't capture
376 return super.getPotentialQueenMoves(sq
);
379 getPotentialKingMoves(sq
)
381 const V
= VariantRules
;
382 return this.getSlideNJumpMoves(sq
,
383 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]), "oneStep");
386 // isAttacked() is OK because the immobilizer doesn't take
388 isAttackedByPawn([x
,y
], colors
)
390 // Square (x,y) must be surroundable by two enemy pieces,
391 // and one of them at least should be a pawn (moving).
392 const dirs
= [ [1,0],[0,1] ];
393 const steps
= VariantRules
.steps
[VariantRules
.ROOK
];
394 const [sizeX
,sizeY
] = VariantRules
.size
;
395 for (let dir
of dirs
)
397 const [i1
,j1
] = [x
-dir
[0],y
-dir
[1]]; //"before"
398 const [i2
,j2
] = [x
+dir
[0],y
+dir
[1]]; //"after"
399 if (i1
>=0 && i1
<sizeX
&& i2
>=0 && i2
<sizeX
400 && j1
>=0 && j1
<sizeY
&& j2
>=0 && j2
<sizeY
)
402 if ((this.board
[i1
][j1
]!=VariantRules
.EMPTY
403 && colors
.includes(this.getColor(i1
,j1
))
404 && this.board
[i2
][j2
]==VariantRules
.EMPTY
)
406 (this.board
[i2
][j2
]!=VariantRules
.EMPTY
407 && colors
.includes(this.getColor(i2
,j2
))
408 && this.board
[i1
][j1
]==VariantRules
.EMPTY
))
410 // Search a movable enemy pawn landing on the empty square
411 for (let step
of steps
)
413 let [ii
,jj
] = (this.board
[i1
][j1
]==VariantRules
.EMPTY
? [i1
,j1
] : [i2
,j2
]);
414 let [i3
,j3
] = [ii
+step
[0],jj
+step
[1]];
415 while (i3
>=0 && i3
<sizeX
&& j3
>=0 && j3
<sizeY
416 && this.board
[i3
][j3
]==VariantRules
.EMPTY
)
421 if (i3
>=0 && i3
<sizeX
&& j3
>=0 && j3
<sizeY
422 && colors
.includes(this.getColor(i3
,j3
))
423 && this.getPiece(i3
,j3
) == VariantRules
.PAWN
424 && !this.isImmobilized([i3
,j3
]))
435 isAttackedByRook([x
,y
], colors
)
437 // King must be on same column or row,
438 // and a rook should be able to reach a capturing square
439 const [sizeX
,sizeY
] = VariantRules
.size
;
440 // colors contains only one element, giving the oppCol and thus king position
441 const sameRow
= (x
== this.kingPos
[colors
[0]][0]);
442 const sameColumn
= (y
== this.kingPos
[colors
[0]][1]);
443 if (sameRow
|| sameColumn
)
445 // Look for the enemy rook (maximum 1)
446 for (let i
=0; i
<sizeX
; i
++)
448 for (let j
=0; j
<sizeY
; j
++)
450 if (this.board
[i
][j
] != VariantRules
.EMPTY
451 && colors
.includes(this.getColor(i
,j
))
452 && this.getPiece(i
,j
) == VariantRules
.ROOK
)
454 if (this.isImmobilized([i
,j
]))
455 return false; //because only one rook
456 // Can it reach a capturing square?
457 // Easy but quite suboptimal way (TODO): generate all moves (turn is OK)
458 const moves
= this.getPotentialMovesFrom([i
,j
]);
459 for (let move of moves
)
461 if (sameRow
&& move.end
.y
== y
|| sameColumn
&& move.end
.x
== x
)
471 isAttackedByKnight([x
,y
], colors
)
473 // Square (x,y) must be on same line as a knight,
474 // and there must be empty square(s) behind.
475 const V
= VariantRules
;
476 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
477 const [sizeX
,sizeY
] = V
.size
;
479 for (let step
of steps
)
481 const [i0
,j0
] = [x
+step
[0],y
+step
[1]];
482 if (i0
>=0 && i0
<sizeX
&& j0
>=0 && j0
<sizeY
&& this.board
[i0
][j0
] == V
.EMPTY
)
484 // Try in opposite direction:
485 let [i
,j
] = [x
-step
[0],y
-step
[1]];
486 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
)
488 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
] == V
.EMPTY
)
493 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
)
495 if (colors
.includes(this.getColor(i
,j
)))
497 if (this.getPiece(i
,j
) == V
.KNIGHT
&& !this.isImmobilized([i
,j
]))
501 // [else] Our color, could be captured *if there was an empty space*
502 if (this.board
[i
+step
[0]][j
+step
[1]] != V
.EMPTY
)
513 isAttackedByBishop([x
,y
], colors
)
515 // We cheat a little here: since this function is used exclusively for king,
516 // it's enough to check the immediate surrounding of the square.
517 const V
= VariantRules
;
518 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
519 const [sizeX
,sizeY
] = V
.size
;
520 for (let step
of adjacentSteps
)
522 const [i
,j
] = [x
+step
[0],y
+step
[1]];
523 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
]!=V
.EMPTY
524 && colors
.includes(this.getColor(i
,j
)) && this.getPiece(i
,j
) == V
.BISHOP
)
526 return true; //bishops are never immobilized
532 isAttackedByQueen([x
,y
], colors
)
534 // Square (x,y) must be adjacent to a queen, and the queen must have
535 // some free space in the opposite direction from (x,y)
536 const V
= VariantRules
;
537 const adjacentSteps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
538 const [sizeX
,sizeY
] = V
.size
;
539 for (let step
of adjacentSteps
)
541 const sq2
= [x
+2*step
[0],y
+2*step
[1]];
542 if (sq2
[0]>=0 && sq2
[0]<sizeX
&& sq2
[1]>=0 && sq2
[1]<sizeY
543 && this.board
[sq2
[0]][sq2
[1]] == V
.EMPTY
)
545 const sq1
= [x
+step
[0],y
+step
[1]];
546 if (this.board
[sq1
[0]][sq1
[1]] != V
.EMPTY
547 && colors
.includes(this.getColor(sq1
[0],sq1
[1]))
548 && this.getPiece(sq1
[0],sq1
[1]) == V
.QUEEN
549 && !this.isImmobilized(sq1
))
558 updateVariables(move)
560 // Just update king(s) position(s)
561 const piece
= this.getPiece(move.start
.x
,move.start
.y
);
562 const c
= this.getColor(move.start
.x
,move.start
.y
);
563 if (piece
== VariantRules
.KING
&& move.appear
.length
> 0)
565 this.kingPos
[c
][0] = move.appear
[0].x
;
566 this.kingPos
[c
][1] = move.appear
[0].y
;
570 static get VALUES() { //TODO: totally experimental!
582 static get SEARCH_DEPTH() { return 2; } //TODO?
584 static GenRandInitFen()
586 let pieces
= { "w": new Array(8), "b": new Array(8) };
587 // Shuffle pieces on first and last rank
588 for (let c
of ["w","b"])
590 let positions
= _
.range(8);
591 // Get random squares for every piece, totally freely
593 let randIndex
= _
.random(7);
594 const bishop1Pos
= positions
[randIndex
];
595 positions
.splice(randIndex
, 1);
597 randIndex
= _
.random(6);
598 const bishop2Pos
= positions
[randIndex
];
599 positions
.splice(randIndex
, 1);
601 randIndex
= _
.random(5);
602 const knight1Pos
= positions
[randIndex
];
603 positions
.splice(randIndex
, 1);
605 randIndex
= _
.random(4);
606 const knight2Pos
= positions
[randIndex
];
607 positions
.splice(randIndex
, 1);
609 randIndex
= _
.random(3);
610 const queenPos
= positions
[randIndex
];
611 positions
.splice(randIndex
, 1);
613 randIndex
= _
.random(2);
614 const kingPos
= positions
[randIndex
];
615 positions
.splice(randIndex
, 1);
617 randIndex
= _
.random(1);
618 const rookPos
= positions
[randIndex
];
619 positions
.splice(randIndex
, 1);
620 const immobilizerPos
= positions
[0];
622 pieces
[c
][bishop1Pos
] = 'b';
623 pieces
[c
][bishop2Pos
] = 'b';
624 pieces
[c
][knight1Pos
] = 'n';
625 pieces
[c
][knight2Pos
] = 'n';
626 pieces
[c
][queenPos
] = 'q';
627 pieces
[c
][kingPos
] = 'k';
628 pieces
[c
][rookPos
] = 'r';
629 pieces
[c
][immobilizerPos
] = 'm';
631 return pieces
["b"].join("") +
632 "/pppppppp/8/8/8/8/PPPPPPPP/" +
633 pieces
["w"].join("").toUpperCase() +
634 " 0000"; //TODO: flags?!
639 return "0000"; //TODO: or "-" ?
644 const initialSquare
=
645 String
.fromCharCode(97 + move.start
.y
) + (VariantRules
.size
[0]-move.start
.x
);
647 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
648 let notation
= undefined;
649 if (move.appear
[0].p
== VariantRules
.PAWN
)
651 // Pawn: generally ambiguous short notation, so we use full description
652 notation
= "P" + initialSquare
+ finalSquare
;
654 else if (move.appear
[0].p
== VariantRules
.KING
)
655 notation
= "K" + (move.vanish
.length
>1 ? "x" : "") + finalSquare
;
657 notation
= move.appear
[0].p
.toUpperCase() + finalSquare
;
658 if (move.vanish
.length
> 1 && move.appear
[0].p
!= VariantRules
.KING
)
659 notation
+= "X"; //capture mark (not describing what is captured...)