1 import { ChessRules
} from "@/base_rules";
3 export class ShatranjRules
extends ChessRules
{
5 static get HasFlags() {
9 static get HasEnpassant() {
13 static get Monochrome() {
17 static get Notoodark() {
21 static get PawnSpecs() {
33 if (b
[1] == 'b') return "Shatranj/" + b
;
37 static get ElephantSteps() {
46 static GenRandInitFen(randomness
) {
47 // Remove castle flags and en-passant indication
48 return ChessRules
.GenRandInitFen(randomness
).slice(0, -7);
51 getPotentialBishopMoves(sq
) {
52 let moves
= this.getSlideNJumpMoves(sq
, V
.ElephantSteps
, "oneStep");
53 // Complete with "repositioning moves": like a queen, without capture
54 let repositioningMoves
= this.getSlideNJumpMoves(
58 ).filter(m
=> m
.vanish
.length
== 1);
59 return moves
.concat(repositioningMoves
);
62 getPotentialQueenMoves(sq
) {
63 // Diagonal capturing moves
64 let captures
= this.getSlideNJumpMoves(
68 ).filter(m
=> m
.vanish
.length
== 2);
69 return captures
.concat(
70 // Orthogonal non-capturing moves
71 this.getSlideNJumpMoves(
75 ).filter(m
=> m
.vanish
.length
== 1)
79 isAttackedByBishop(sq
, color
) {
80 return this.isAttackedBySlideNJump(
89 isAttackedByQueen(sq
, color
) {
90 return this.isAttackedBySlideNJump(
100 const color
= this.turn
;
101 const getScoreLost
= () => {
103 return color
== "w" ? "0-1" : "1-0";
105 if (!this.atLeastOneMove())
106 // No valid move: I lose (this includes checkmate)
107 return getScoreLost();
108 // Win if the opponent has no pieces left (except king),
109 // and cannot bare king on the next move.
111 // No need to remember all pieces' squares:
112 // variable only used if just one remaining piece.
113 "w": {count: 0, square: null},
114 "b": {count: 0, square: null}
116 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
117 for (let j
=0; j
<V
.size
.y
; j
++) {
118 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
,j
) != V
.KING
) {
119 const sqCol
= this.getColor(i
,j
);
120 piecesLeft
[sqCol
].count
++;
121 piecesLeft
[sqCol
].square
= [i
,j
];
125 if (Object
.values(piecesLeft
).every(v
=> v
.count
> 0))
127 // No pieces left for some side: if both kings are bare, it's a draw
128 if (Object
.values(piecesLeft
).every(v
=> v
.count
== 0))
130 if (piecesLeft
[color
].count
> 0)
131 // He could have drawn, but didn't take my last piece...
132 return color
== "w" ? "1-0" : "0-1";
133 const oppCol
= V
.GetOppCol(color
);
134 if (piecesLeft
[oppCol
].count
>= 2)
135 // 2 enemy units or more: I lose
136 return getScoreLost();
137 // I don't have any piece, my opponent have one: can I take it?
138 if (this.isAttacked(piecesLeft
[oppCol
].square
, color
))
139 // Yes! But I still need to take it
142 return getScoreLost();
145 static get VALUES() {