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