Commit | Line | Data |
---|---|---|
c322a844 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | ||
32f6285e | 3 | export class ThreechecksRules extends ChessRules { |
7e8a7ea1 | 4 | |
e71161fb BA |
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"]) { | |
e50a8025 | 15 | this.checkFlags[c] = parseInt(flags.charAt(c == "w" ? 0 : 1), 10); |
e71161fb BA |
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) { | |
2c5d7b20 | 29 | // TODO: !!this.checkFlags condition for printDiagram, but it's not good. |
b627d118 | 30 | // This is just a temporary fix. |
6b7b2cf7 | 31 | if (b[1] == 'k' && !!this.checkFlags && this.checkFlags[b[0]] > 0) |
92240cf0 | 32 | return "Threechecks/" + b[0] + 'k_' + this.checkFlags[b[0]]; |
e71161fb BA |
33 | return b; |
34 | } | |
35 | ||
3a2a7b5f BA |
36 | postPlay(move) { |
37 | super.postPlay(move); | |
e71161fb BA |
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 | ||
7ba4a5bc | 51 | static GenRandInitFen(randomness) { |
3a2a7b5f BA |
52 | // Add check flags (at 0) |
53 | return ChessRules.GenRandInitFen(randomness).slice(0, -2) + "00"; | |
e71161fb BA |
54 | } |
55 | ||
56 | getFlagsFen() { | |
57 | let fen = super.getFlagsFen(); | |
58 | // Add check flags | |
3a2a7b5f | 59 | for (let c of ["w", "b"]) fen += this.checkFlags[c]; |
e71161fb BA |
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"]; | |
c322a844 | 67 | } |
7e8a7ea1 | 68 | |
c322a844 | 69 | }; |