Fix typo
[vchess.git] / client / src / variants / Losers.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class LosersRules extends ChessRules {
6b7b2cf7
BA
6 // Trim all non-capturing moves
7 static KeepCaptures(moves) {
57d9b2c4 8 return moves.filter(m => m.vanish.length == 2 && m.appear.length == 1);
dac39588
BA
9 }
10
801e2870 11 // Stop at the first capture found (if any)
6808d7a1 12 atLeastOneCapture() {
dac39588 13 const color = this.turn;
6808d7a1
BA
14 for (let i = 0; i < V.size.x; i++) {
15 for (let j = 0; j < V.size.y; j++) {
6b7b2cf7
BA
16 if (
17 this.board[i][j] != V.EMPTY &&
5d416f0f
BA
18 this.getColor(i, j) == color &&
19 this.getPotentialMovesFrom([i, j]).some(m => {
20 return (
21 // Warning: discard castle moves
22 m.vanish.length == 2 && m.appear.length == 1 &&
23 this.filterValid([m]).length == 1
24 );
25 })
6b7b2cf7
BA
26 ) {
27 return true;
dac39588
BA
28 }
29 }
30 }
31 return false;
32 }
33
6808d7a1
BA
34 getPossibleMovesFrom(sq) {
35 let moves = this.filterValid(this.getPotentialMovesFrom(sq));
6b7b2cf7 36 const captureMoves = V.KeepCaptures(moves);
5d416f0f
BA
37
38console.log(this.atLeastOneCapture());
39
6b7b2cf7
BA
40 if (captureMoves.length > 0) return captureMoves;
41 if (this.atLeastOneCapture()) return [];
dac39588
BA
42 return moves;
43 }
44
6808d7a1 45 getAllValidMoves() {
6b7b2cf7 46 const moves = super.getAllValidMoves();
a34caace
BA
47 if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1))
48 return V.KeepCaptures(moves);
dac39588
BA
49 return moves;
50 }
51
6808d7a1 52 getCurrentScore() {
6b7b2cf7
BA
53 // If only my king remains, I win
54 const color = this.turn;
55 let onlyKing = true;
56 outerLoop: for (let i=0; i<V.size.x; i++) {
57 for (let j=0; j<V.size.y; j++) {
58 if (
59 this.board[i][j] != V.EMPTY &&
60 this.getColor(i,j) == color &&
61 this.getPiece(i,j) != V.KING
62 ) {
63 onlyKing = false;
64 break outerLoop;
65 }
66 }
67 }
68 if (onlyKing) return color == "w" ? "1-0" : "0-1";
69 if (this.atLeastOneMove()) return "*";
70 // No valid move: the side who cannot move (or is checkmated) wins
6808d7a1 71 return this.turn == "w" ? "1-0" : "0-1";
dac39588
BA
72 }
73
6808d7a1 74 evalPosition() {
6b7b2cf7
BA
75 // Less material is better (more subtle in fact but...)
76 return -super.evalPosition();
dac39588 77 }
6808d7a1 78};