Fix Dobutsu, extend Align4
[xogo.git] / variants / Align4 / class.js
CommitLineData
4cec374b
BA
1import ChessRules from "/base_rules.js";
2
3export default class Align4Rules extends ChessRules {
4
5 static get Options() {
6 return {
7 select: [{
8 label: "Randomness",
9 variable: "randomness",
10 defaut: 0,
11 options: [
12 {label: "Deterministic", value: 0},
13 {label: "Random", value: 1}
14 ]
15 }],
d6613539
BA
16 input: [
17 {
18 label: "Pawn first",
19 variable: "pawnfirst",
20 type: "checkbox",
21 defaut: false
22 }
23 ],
4cec374b
BA
24 styles: ["atomic", "capture", "cylinder"]
25 };
26 }
27
28 get hasReserve() {
29 return true;
30 }
31 get hasReserveFen() {
32 return false;
33 }
34
f31de5e4
BA
35 genRandInitBaseFen() {
36 let baseFen = super.genRandInitBaseFen();
b9877ed2
BA
37 return {
38 fen: baseFen.fen.replace("rnbqkbnr/pppppppp", "4k3/8"),
39 o: {flags: baseFen.o.flags.substr(0, 2) + "88"}
40 };
4cec374b
BA
41 }
42
fc12475f 43 initReserves() {
4cec374b
BA
44 this.reserve = { b: { p: 1 } };
45 }
46
47 // Just do not update any reserve (infinite supply)
48 updateReserve() {}
49
d6613539
BA
50 canDrop([c, p], [i, j]) {
51 return (
52 this.board[i][j] == "" &&
53 (
54 p != "p" || this.options["pawnfirst"] ||
55 (c == 'w' && i < this.size.x - 1) ||
56 (c == 'b' && i > 0)
57 )
58 );
59 }
60
eceb02f7
BA
61 getCurrentScore(move_s) {
62 const score = super.getCurrentScore(move_s);
4cec374b
BA
63 if (score != "*")
64 return score;
65 // Check pawns connection:
66 for (let i = 0; i < this.size.x; i++) {
67 for (let j = 0; j < this.size.y; j++) {
68 if (
69 this.board[i][j] != "" &&
70 this.getColor(i, j) == 'b' &&
71 this.getPiece(i, j) == 'p'
72 ) {
73 // Exploration "rightward + downward" is enough
74 for (let step of [[1, 0], [0, 1], [1, 1], [-1, 1]]) {
75 let [ii, jj] = [i + step[0], j + step[1]];
76 let kounter = 1;
77 while (
78 this.onBoard(ii, jj) &&
79 this.board[ii][jj] != "" &&
80 this.getColor(ii, jj) == 'b' &&
81 this.getPiece(ii, jj) == 'p'
82 ) {
83 kounter++;
84 ii += step[0];
85 jj += step[1];
86 }
87 if (kounter == 4)
88 return "0-1";
89 }
90 }
91 }
92 }
93 return "*";
94 }
95
96};