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