Fix Grasshopper variant
[vchess.git] / client / src / variants / Check3.js
1 import { ChessRules } from "@/base_rules";
2
3 export const VariantRules = class Check3Rules extends ChessRules {
4 static IsGoodFlags(flags) {
5 // 4 for castle + 2 for checks (0,1 or 2)
6 return !!flags.match(/^[01]{4,4}[012]{2,2}$/);
7 }
8
9 setFlags(fenflags) {
10 super.setFlags(fenflags); //castleFlags
11 this.checkFlags = { w: 0, b: 0 };
12 const flags = fenflags.substr(4); //skip first 4 digits, for castle
13 for (let c of ["w", "b"]) {
14 this.checkFlags[c] = parseInt(flags.charAt(c == "w" ? 0 : 1));
15 }
16 }
17
18 aggregateFlags() {
19 return [this.castleFlags, this.checkFlags];
20 }
21
22 disaggregateFlags(flags) {
23 this.castleFlags = flags[0];
24 this.checkFlags = flags[1];
25 }
26
27 getPpath(b) {
28 if (b[1] == 'k' && this.checkFlags[b[0]] > 0)
29 return "Check3/" + b[0] + 'k_' + this.checkFlags[b[0]];
30 return b;
31 }
32
33 updateVariables(move) {
34 super.updateVariables(move);
35 // Does this move give check?
36 const oppCol = this.turn;
37 if (this.underCheck(oppCol))
38 this.checkFlags[oppCol]++;
39 }
40
41 getCurrentScore() {
42 const color = this.turn;
43 if (this.checkFlags[color] >= 3)
44 return color == "w" ? "0-1" : "1-0";
45 return super.getCurrentScore();
46 }
47
48 static GenRandInitFen() {
49 const randFen = ChessRules.GenRandInitFen();
50 // Add check flags (at 0)
51 return randFen.replace(" w 0 1111", " w 0 111100");
52 }
53
54 getFlagsFen() {
55 let fen = super.getFlagsFen();
56 // Add check flags
57 for (let c of ["w", "b"])
58 fen += this.checkFlags[c];
59 return fen;
60 }
61
62 evalPosition() {
63 const baseEval = super.evalPosition();
64 // Take number of checks into account
65 return baseEval/5 - this.checkFlags["w"] + this.checkFlags["b"];
66 }
67 };