A few fixes (for updateCastleFlags()) + add Madhouse and Pocketknight variants
[vchess.git] / client / src / variants / Threechecks.js
CommitLineData
c322a844
BA
1import { ChessRules } from "@/base_rules";
2
32f6285e 3export class ThreechecksRules extends ChessRules {
e71161fb
BA
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"]) {
e50a8025 14 this.checkFlags[c] = parseInt(flags.charAt(c == "w" ? 0 : 1), 10);
e71161fb
BA
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) {
2c5d7b20 28 // TODO: !!this.checkFlags condition for printDiagram, but it's not good.
b627d118 29 // This is just a temporary fix.
6b7b2cf7 30 if (b[1] == 'k' && !!this.checkFlags && this.checkFlags[b[0]] > 0)
92240cf0 31 return "Threechecks/" + b[0] + 'k_' + this.checkFlags[b[0]];
e71161fb
BA
32 return b;
33 }
34
3a2a7b5f
BA
35 postPlay(move) {
36 super.postPlay(move);
e71161fb
BA
37 // Does this move give check?
38 const oppCol = this.turn;
39 if (this.underCheck(oppCol))
40 this.checkFlags[oppCol]++;
41 }
42
43 getCurrentScore() {
44 const color = this.turn;
45 if (this.checkFlags[color] >= 3)
46 return color == "w" ? "0-1" : "1-0";
47 return super.getCurrentScore();
48 }
49
7ba4a5bc 50 static GenRandInitFen(randomness) {
3a2a7b5f
BA
51 // Add check flags (at 0)
52 return ChessRules.GenRandInitFen(randomness).slice(0, -2) + "00";
e71161fb
BA
53 }
54
55 getFlagsFen() {
56 let fen = super.getFlagsFen();
57 // Add check flags
3a2a7b5f 58 for (let c of ["w", "b"]) fen += this.checkFlags[c];
e71161fb
BA
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"];
c322a844
BA
66 }
67};