1 import { ChessRules
} from "@/base_rules";
3 export const VariantRules
= class ShatranjRules
extends ChessRules
{
4 static get HasFlags() {
8 static get HasCastle() {
12 static get HasEnpassant() {
16 static get ElephantSteps() {
25 static GenRandInitFen(randomness
) {
26 // Remove castle flags and en-passant indication
27 return ChessRules
.GenRandInitFen(randomness
).slice(0, -7);
30 getPotentialPawnMoves([x
, y
]) {
31 const color
= this.turn
;
33 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
34 const shiftX
= color
== "w" ? -1 : 1;
35 const startRank
= color
== "w" ? sizeX
- 2 : 1;
36 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
37 // Promotion in minister (queen) only:
38 const finalPiece
= x
+ shiftX
== lastRank
? V
.QUEEN : V
.PAWN
;
40 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
43 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
50 for (let shiftY
of [-1, 1]) {
54 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
55 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
58 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
69 getPotentialBishopMoves(sq
) {
70 let moves
= this.getSlideNJumpMoves(sq
, V
.ElephantSteps
, "oneStep");
71 // Complete with "repositioning moves": like a queen, without capture
72 let repositioningMoves
= this.getSlideNJumpMoves(
76 ).filter(m
=> m
.vanish
.length
== 1);
77 return moves
.concat(repositioningMoves
);
80 getPotentialQueenMoves(sq
) {
81 // Diagonal capturing moves
82 let captures
= this.getSlideNJumpMoves(
86 ).filter(m
=> m
.vanish
.length
== 2);
87 return captures
.concat(
88 // Orthogonal non-capturing moves
89 this.getSlideNJumpMoves(
93 ).filter(m
=> m
.vanish
.length
== 1)
97 isAttackedByBishop(sq
, color
) {
98 return this.isAttackedBySlideNJump(
107 isAttackedByQueen(sq
, color
) {
108 return this.isAttackedBySlideNJump(
118 const color
= this.turn
;
119 const getScoreLost
= () => {
121 return color
== "w" ? "0-1" : "1-0";
123 if (!this.atLeastOneMove())
124 // No valid move: I lose (this includes checkmate)
125 return getScoreLost();
126 // Win if the opponent has no pieces left (except king),
127 // and cannot bare king on the next move.
129 // No need to remember all pieces' squares:
130 // variable only used if just one remaining piece.
131 "w": {count: 0, square: null},
132 "b": {count: 0, square: null}
134 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
135 for (let j
=0; j
<V
.size
.y
; j
++) {
136 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
,j
) != V
.KING
) {
137 const sqCol
= this.getColor(i
,j
);
138 piecesLeft
[sqCol
].count
++;
139 piecesLeft
[sqCol
].square
= [i
,j
];
143 if (Object
.values(piecesLeft
).every(v
=> v
.count
> 0))
145 // No pieces left for some side: if both kings are bare, it's a draw
146 if (Object
.values(piecesLeft
).every(v
=> v
.count
== 0))
148 if (piecesLeft
[color
].count
> 0)
149 // He could have drawn, but didn't take my last piece...
150 return color
== "w" ? "1-0" : "0-1";
151 const oppCol
= V
.GetOppCol(color
);
152 if (piecesLeft
[oppCol
].count
>= 2)
153 // 2 enemy units or more: I lose
154 return getScoreLost();
155 // I don't have any piece, my opponent have one: can I take it?
156 if (this.isAttacked(piecesLeft
[oppCol
].square
, color
))
157 // Yes! But I still need to take it
160 return getScoreLost();
163 static get VALUES() {