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 Notoodark() {
20 static get PawnSpecs() {
32 if (b
[1] == 'b') return "Shatranj/" + b
;
36 static get ElephantSteps() {
45 static GenRandInitFen(randomness
) {
46 // Remove castle flags and en-passant indication
47 return ChessRules
.GenRandInitFen(randomness
).slice(0, -7);
50 getPotentialBishopMoves(sq
) {
51 let moves
= this.getSlideNJumpMoves(sq
, V
.ElephantSteps
, "oneStep");
52 // Complete with "repositioning moves": like a queen, without capture
53 let repositioningMoves
= this.getSlideNJumpMoves(
57 ).filter(m
=> m
.vanish
.length
== 1);
58 return moves
.concat(repositioningMoves
);
61 getPotentialQueenMoves(sq
) {
62 // Diagonal capturing moves
63 let captures
= this.getSlideNJumpMoves(
67 ).filter(m
=> m
.vanish
.length
== 2);
68 return captures
.concat(
69 // Orthogonal non-capturing moves
70 this.getSlideNJumpMoves(
74 ).filter(m
=> m
.vanish
.length
== 1)
78 isAttackedByBishop(sq
, color
) {
79 return this.isAttackedBySlideNJump(
88 isAttackedByQueen(sq
, color
) {
89 return this.isAttackedBySlideNJump(
99 const color
= this.turn
;
100 const getScoreLost
= () => {
102 return color
== "w" ? "0-1" : "1-0";
104 if (!this.atLeastOneMove())
105 // No valid move: I lose (this includes checkmate)
106 return getScoreLost();
107 // Win if the opponent has no pieces left (except king),
108 // and cannot bare king on the next move.
110 // No need to remember all pieces' squares:
111 // variable only used if just one remaining piece.
112 "w": {count: 0, square: null},
113 "b": {count: 0, square: null}
115 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
116 for (let j
=0; j
<V
.size
.y
; j
++) {
117 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
,j
) != V
.KING
) {
118 const sqCol
= this.getColor(i
,j
);
119 piecesLeft
[sqCol
].count
++;
120 piecesLeft
[sqCol
].square
= [i
,j
];
124 if (Object
.values(piecesLeft
).every(v
=> v
.count
> 0))
126 // No pieces left for some side: if both kings are bare, it's a draw
127 if (Object
.values(piecesLeft
).every(v
=> v
.count
== 0))
129 if (piecesLeft
[color
].count
> 0)
130 // He could have drawn, but didn't take my last piece...
131 return color
== "w" ? "1-0" : "0-1";
132 const oppCol
= V
.GetOppCol(color
);
133 if (piecesLeft
[oppCol
].count
>= 2)
134 // 2 enemy units or more: I lose
135 return getScoreLost();
136 // I don't have any piece, my opponent have one: can I take it?
137 if (this.isAttacked(piecesLeft
[oppCol
].square
, color
))
138 // Yes! But I still need to take it
141 return getScoreLost();
144 static get VALUES() {