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