Add Swap, Switching, pawns variants
[vchess.git] / client / src / variants / Horde.js
1 import { ChessRules } from "@/base_rules";
2
3 export class HordeRules extends ChessRules {
4 static get HasFlags() {
5 return false;
6 }
7
8 static IsGoodPosition() {
9 // At least one white unit, and exactly one black king:
10 if (position.length == 0) return false;
11 const rows = position.split("/");
12 if (rows.length != V.size.x) return false;
13 let things = { "k": 0, "w": false };
14 for (let row of rows) {
15 let sumElts = 0;
16 for (let i = 0; i < row.length; i++) {
17 if (row[i] == 'k') things['k']++;
18 if (V.PIECES.includes(row[i].toLowerCase())) {
19 const rowCharCode = row[i].charCodeAt(0);
20 if (rowCharCode >= 65 && rowCharCode <= 90) {
21 // No white king:
22 if (row[i] == 'K') return false;
23 if (!things['w']) things['w'] = true;
24 }
25 sumElts++;
26 } else {
27 const num = parseInt(row[i]);
28 if (isNaN(num)) return false;
29 sumElts += num;
30 }
31 }
32 if (sumElts != V.size.y) return false;
33 }
34 if (things[''] != 1 || !things['w']) return false;
35 return true;
36 }
37
38 static GenRandInitFen(randomness) {
39 if (randomness == 2) randomness--;
40 const fen = ChessRules.GenRandInitFen(randomness);
41 return (
42 // 20 first chars are 3 rows + 3 slashes
43 fen.substr(0, 20)
44 // En passant available, but no castle:
45 .concat("1PP2PP1/PPPPPPPP/PPPPPPPP/PPPPPPPP/PPPPPPPP w 0 -")
46 );
47 }
48
49 filterValid(moves) {
50 if (this.turn == 'w') return moves;
51 return super.filterValid(moves);
52 }
53
54 getCheckSquares() {
55 if (this.turn == 'w') return [];
56 return (
57 this.underCheck('b')
58 ? [JSON.parse(JSON.stringify(this.kingPos['b']))]
59 : []
60 );
61 }
62
63 getCurrentScore() {
64 if (this.turn == 'w') {
65 // Do I have any unit remaining? If not, I lost.
66 // If yes and no available move, draw.
67 let somethingRemains = false;
68 outerLoop: for (let i=0; i<8; i++) {
69 for (let j=0; j<8; j++) {
70 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == 'w') {
71 somethingRemains = true;
72 break outerLoop;
73 }
74 }
75 }
76 if (!somethingRemains) return "0-1";
77 if (this.atLeastOneMove()) return "*";
78 return "1/2";
79 }
80 // From black side, just run usual checks:
81 return super.getCurrentScore();
82 }
83 };