1 import { ChessRules
} from "@/base_rules";
3 export class ShatranjRules
extends ChessRules
{
4 static get HasFlags() {
8 static get HasCastle() {
12 static get HasEnpassant() {
16 static get PawnSpecs() {
27 static get ElephantSteps() {
36 static GenRandInitFen(randomness
) {
37 // Remove castle flags and en-passant indication
38 return ChessRules
.GenRandInitFen(randomness
).slice(0, -7);
41 getPotentialBishopMoves(sq
) {
42 let moves
= this.getSlideNJumpMoves(sq
, V
.ElephantSteps
, "oneStep");
43 // Complete with "repositioning moves": like a queen, without capture
44 let repositioningMoves
= this.getSlideNJumpMoves(
48 ).filter(m
=> m
.vanish
.length
== 1);
49 return moves
.concat(repositioningMoves
);
52 getPotentialQueenMoves(sq
) {
53 // Diagonal capturing moves
54 let captures
= this.getSlideNJumpMoves(
58 ).filter(m
=> m
.vanish
.length
== 2);
59 return captures
.concat(
60 // Orthogonal non-capturing moves
61 this.getSlideNJumpMoves(
65 ).filter(m
=> m
.vanish
.length
== 1)
69 isAttackedByBishop(sq
, color
) {
70 return this.isAttackedBySlideNJump(
79 isAttackedByQueen(sq
, color
) {
80 return this.isAttackedBySlideNJump(
90 const color
= this.turn
;
91 const getScoreLost
= () => {
93 return color
== "w" ? "0-1" : "1-0";
95 if (!this.atLeastOneMove())
96 // No valid move: I lose (this includes checkmate)
97 return getScoreLost();
98 // Win if the opponent has no pieces left (except king),
99 // and cannot bare king on the next move.
101 // No need to remember all pieces' squares:
102 // variable only used if just one remaining piece.
103 "w": {count: 0, square: null},
104 "b": {count: 0, square: null}
106 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
107 for (let j
=0; j
<V
.size
.y
; j
++) {
108 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
,j
) != V
.KING
) {
109 const sqCol
= this.getColor(i
,j
);
110 piecesLeft
[sqCol
].count
++;
111 piecesLeft
[sqCol
].square
= [i
,j
];
115 if (Object
.values(piecesLeft
).every(v
=> v
.count
> 0))
117 // No pieces left for some side: if both kings are bare, it's a draw
118 if (Object
.values(piecesLeft
).every(v
=> v
.count
== 0))
120 if (piecesLeft
[color
].count
> 0)
121 // He could have drawn, but didn't take my last piece...
122 return color
== "w" ? "1-0" : "0-1";
123 const oppCol
= V
.GetOppCol(color
);
124 if (piecesLeft
[oppCol
].count
>= 2)
125 // 2 enemy units or more: I lose
126 return getScoreLost();
127 // I don't have any piece, my opponent have one: can I take it?
128 if (this.isAttacked(piecesLeft
[oppCol
].square
, color
))
129 // Yes! But I still need to take it
132 return getScoreLost();
135 static get VALUES() {