1 import { ChessRules
} from "@/base_rules";
3 export const VariantRules
= class ShatranjRules
extends ChessRules
{
4 static get HasFlags() {
8 static get HasEnpassant() {
12 static get ElephantSteps() {
21 static GenRandInitFen(randomness
) {
22 return ChessRules
.GenRandInitFen(randomness
).replace("w 1111 -", "w");
25 getPotentialPawnMoves([x
, y
]) {
26 const color
= this.turn
;
28 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
29 const shiftX
= color
== "w" ? -1 : 1;
30 const startRank
= color
== "w" ? sizeX
- 2 : 1;
31 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
32 // Promotion in minister (queen) only:
33 const finalPiece
= x
+ shiftX
== lastRank
? V
.QUEEN : V
.PAWN
;
35 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
38 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
45 for (let shiftY
of [-1, 1]) {
49 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
50 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
53 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
64 getPotentialBishopMoves(sq
) {
65 let moves
= this.getSlideNJumpMoves(sq
, V
.ElephantSteps
, "oneStep");
66 // Complete with "repositioning moves": like a queen, without capture
67 let repositioningMoves
= this.getSlideNJumpMoves(
71 ).filter(m
=> m
.vanish
.length
== 1);
72 return moves
.concat(repositioningMoves
);
75 getPotentialQueenMoves(sq
) {
76 // Diagonal capturing moves
77 let captures
= this.getSlideNJumpMoves(
81 ).filter(m
=> m
.vanish
.length
== 2);
82 return captures
.concat(
83 // Orthogonal non-capturing moves
84 this.getSlideNJumpMoves(
88 ).filter(m
=> m
.vanish
.length
== 1)
92 getPotentialKingMoves(sq
) {
93 return this.getSlideNJumpMoves(
95 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
100 isAttackedByBishop(sq
, colors
) {
101 return this.isAttackedBySlideNJump(
110 isAttackedByQueen(sq
, colors
) {
111 return this.isAttackedBySlideNJump(
121 const color
= this.turn
;
122 const getScoreLost
= () => {
124 return color
== "w" ? "0-1" : "1-0";
126 if (!this.atLeastOneMove())
127 // No valid move: I lose (this includes checkmate)
128 return getScoreLost();
129 // Win if the opponent has no pieces left (except king),
130 // and cannot bare king on the next move.
132 // No need to remember all pieces' squares:
133 // variable only used if just one remaining piece.
134 "w": {count: 0, square: null},
135 "b": {count: 0, square: null}
137 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
138 for (let j
=0; j
<V
.size
.y
; j
++) {
139 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
,j
) != V
.KING
) {
140 const sqCol
= this.getColor(i
,j
);
141 piecesLeft
[sqCol
].count
++;
142 piecesLeft
[sqCol
].square
= [i
,j
];
146 if (Object
.values(piecesLeft
).every(v
=> v
.count
> 0))
148 // No pieces left for some side: if both kings are bare, it's a draw
149 if (Object
.values(piecesLeft
).every(v
=> v
.count
== 0))
151 if (piecesLeft
[color
].count
> 0)
152 // He could have drawn, but didn't take my last piece...
153 return color
== "w" ? "1-0" : "0-1";
154 const oppCol
= V
.GetOppCol(color
);
155 if (piecesLeft
[oppCol
].count
>= 2)
156 // 2 enemy units or more: I lose
157 return getScoreLost();
158 // I don't have any piece, my opponent have one: can I take it?
159 if (this.isAttacked(piecesLeft
[oppCol
].square
, [color
]))
160 // Yes! But I still need to take it
163 return getScoreLost();
166 static get VALUES() {