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