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