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