1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export const VariantRules
= class LosersRules
extends ChessRules
{
6 static get HasFlags() {
10 getPotentialPawnMoves([x
, y
]) {
11 let moves
= super.getPotentialPawnMoves([x
, y
]);
13 // Complete with promotion(s) into king, if possible
14 const color
= this.turn
;
15 const shift
= color
== "w" ? -1 : 1;
16 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
17 if (x
+ shift
== lastRank
) {
19 if (this.board
[x
+ shift
][y
] == V
.EMPTY
)
21 this.getBasicMove([x
, y
], [x
+ shift
, y
], { c: color
, p: V
.KING
})
26 this.canTake([x
, y
], [x
+ shift
, y
- 1]) &&
27 this.board
[x
+ shift
][y
- 1] != V
.EMPTY
30 this.getBasicMove([x
, y
], [x
+ shift
, y
- 1], { c: color
, p: V
.KING
})
35 this.canTake([x
, y
], [x
+ shift
, y
+ 1]) &&
36 this.board
[x
+ shift
][y
+ 1] != V
.EMPTY
39 this.getBasicMove([x
, y
], [x
+ shift
, y
+ 1], { c: color
, p: V
.KING
})
47 getPotentialKingMoves(sq
) {
49 return this.getSlideNJumpMoves(
51 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
56 // Stop at the first capture found (if any)
58 const color
= this.turn
;
59 const oppCol
= V
.GetOppCol(color
);
60 for (let i
= 0; i
< V
.size
.x
; i
++) {
61 for (let j
= 0; j
< V
.size
.y
; j
++) {
62 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) != oppCol
) {
63 const moves
= this.getPotentialMovesFrom([i
, j
]);
64 if (moves
.length
> 0) {
65 for (let k
= 0; k
< moves
.length
; k
++) {
67 moves
[k
].vanish
.length
== 2 &&
68 this.filterValid([moves
[k
]]).length
> 0
79 // Trim all non-capturing moves
80 static KeepCaptures(moves
) {
81 return moves
.filter(m
=> {
82 return m
.vanish
.length
== 2;
86 getPossibleMovesFrom(sq
) {
87 let moves
= this.filterValid(this.getPotentialMovesFrom(sq
));
88 // This is called from interface: we need to know if a capture is possible
89 if (this.atLeastOneCapture()) moves
= V
.KeepCaptures(moves
);
94 let moves
= super.getAllValidMoves();
97 return m
.vanish
.length
== 2;
100 moves
= V
.KeepCaptures(moves
);
105 return false; //No notion of check
112 // No variables update because no royal king + no castling
114 unupdateVariables() {}
117 if (this.atLeastOneMove())
121 // No valid move: the side who cannot move wins
122 return this.turn
== "w" ? "1-0" : "0-1";
125 static get VALUES() {
137 static get SEARCH_DEPTH() {
142 return -super.evalPosition(); //better with less material
145 static GenRandInitFen(randomness
) {
147 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 -";
149 let pieces
= { w: new Array(8), b: new Array(8) };
150 // Shuffle pieces on first and last rank
151 for (let c
of ["w", "b"]) {
152 if (c
== 'b' && randomness
== 1) {
153 pieces
['b'] = pieces
['w'];
157 let positions
= ArrayFun
.range(8);
159 // Get random squares for bishops
160 let randIndex
= 2 * randInt(4);
161 let bishop1Pos
= positions
[randIndex
];
162 // The second bishop must be on a square of different color
163 let randIndex_tmp
= 2 * randInt(4) + 1;
164 let bishop2Pos
= positions
[randIndex_tmp
];
165 // Remove chosen squares
166 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
167 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
169 // Get random squares for knights
170 randIndex
= randInt(6);
171 let knight1Pos
= positions
[randIndex
];
172 positions
.splice(randIndex
, 1);
173 randIndex
= randInt(5);
174 let knight2Pos
= positions
[randIndex
];
175 positions
.splice(randIndex
, 1);
177 // Get random square for queen
178 randIndex
= randInt(4);
179 let queenPos
= positions
[randIndex
];
180 positions
.splice(randIndex
, 1);
182 // Random square for king (no castle)
183 randIndex
= randInt(3);
184 let kingPos
= positions
[randIndex
];
185 positions
.splice(randIndex
, 1);
187 // Rooks positions are now fixed
188 let rook1Pos
= positions
[0];
189 let rook2Pos
= positions
[1];
191 // Finally put the shuffled pieces in the board array
192 pieces
[c
][rook1Pos
] = "r";
193 pieces
[c
][knight1Pos
] = "n";
194 pieces
[c
][bishop1Pos
] = "b";
195 pieces
[c
][queenPos
] = "q";
196 pieces
[c
][kingPos
] = "k";
197 pieces
[c
][bishop2Pos
] = "b";
198 pieces
[c
][knight2Pos
] = "n";
199 pieces
[c
][rook2Pos
] = "r";
202 pieces
["b"].join("") +
203 "/pppppppp/8/8/8/8/PPPPPPPP/" +
204 pieces
["w"].join("").toUpperCase() +
205 // En-passant allowed, but no flags