Add Horde variant + fix typos
[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 // 27 first chars are 3 rows + 3 slashes
43 fen.substr(0, 27)
44 // En passant available, but no castle:
45 .concat("1PP2PP1/PPPPPPPP/PPPPPPPP/PPPPPPPP/PPPPPPPP w 0 -")
46 );
47 }
48
49 getCurrentScore() {
50 if (this.turn == 'w') {
51 // Do I have any unit remaining? If not, I lost.
52 // If yes and no available move, draw.
53 let somethingRemains = false;
54 outerLoop: for (let i=0; i<8; i++) {
55 for (let j=0; j<8; j++) {
56 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == 'w') {
57 somethingRemains = true;
58 break outerLoop;
59 }
60 }
61 }
62 if (!somethingRemains) return "0-1";
63 if (this.atLeastOneMove()) return "*";
64 return "1/2";
65 }
66 // From black side, just run usual checks:
67 return super.getCurrentScore();
68 }
69 };