1 import ChessRules
from "/base_rules.js";
2 import {Random
} from "/utils/alea.js";
3 import PiPo
from "/utils/PiPo.js";
4 import Move
from "/utils/Move.js";
6 export default class AvalamRules
extends ChessRules
{
12 variable: "randomness",
15 {label: "Deterministic", value: 0},
16 {label: "Random", value: 1},
21 label: "Free placement",
39 [1, 0], [0, 1], [-1, 0], [0, -1],
40 [1, 1], [1, -1], [-1, 1], [-1, -1]
45 both: [{steps: steps
, range: 1}]
66 genRandInitBaseFen() {
69 fen
= "9/".repeat(8) + "9";
70 else if (this.options
["randomness"] == 0) {
71 fen
= "2Bb5/1BbBb4/1bBbBbB2/1BbBbBbBb/BbBb1bBbB/" +
72 "bBbBbBbB1/2BbBbBb1/4bBbB1/5bB2";
75 const pieces
= ('B'.repeat(24) + 'b'.repeat(24)).split("");
76 const a
= Random
.shuffle(pieces
, 48).join("");
78 "2" + a
.substr(0, 2) + "5/1" + a
.substr(2, 4) +
79 "4/1" + a
.substr(6, 6) + "2/1" + a
.substr(12, 8) +
80 "/" + a
.substr(20, 4) + "1" + a
.substr(24, 4) +
81 "/" + a
.substr(28, 8) + "1/2" + a
.substr(36, 6) +
82 "1/4" + a
.substr(42, 4) + "1/5" + a
.substr(46, 2) + "2"
85 return { fen: fen
, o: {} };
88 getSquareColorClass(x
, y
) {
97 if (!super.onBoard(x
, y
))
101 return [2, 3].includes(y
);
103 return [1, 2, 3, 4].includes(y
);
105 return [1, 2, 3, 4, 5, 6].includes(y
);
113 return [2, 3, 4, 5, 6, 7].includes(y
);
115 return [4, 5, 6, 7].includes(y
);
117 return [5, 6].includes(y
);
119 return false; //never reached
123 return this.playerColor
== this.turn
;
127 if (!this.freefill
|| this.board
[coords
.x
][coords
.y
] != "")
130 start: {x: coords
.x
, y: coords
.y
},
132 appear: [new PiPo({x: coords
.x
, y: coords
.y
, c: this.turn
, p: 'b'})]
136 getBasicMove([x1
, y1
], [x2
, y2
]) {
137 const cp1
= this.board
[x1
][y1
],
138 cp2
= this.board
[x2
][y2
];
140 String
.fromCharCode(cp1
.charCodeAt(1) + cp2
.charCodeAt(1) - 97);
144 new PiPo({ x: x1
, y: y1
, c: cp1
.charAt(0), p: cp1
.charAt(1) }),
145 new PiPo({ x: x2
, y: y2
, c: cp2
.charAt(0), p: cp2
.charAt(1) })
148 new PiPo({ x: x2
, y: y2
, c: cp1
.charAt(0), p: newPiece
})
154 getPotentialMovesFrom([x
, y
]) {
155 const height
= this.board
[x
][y
].charCodeAt(1) - 97;
159 for (let s
of this.pieces(this.turn
, x
, y
)['b'].both
[0].steps
) {
160 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
162 this.onBoard(i
, j
) &&
163 this.board
[i
][j
] != "" &&
164 (height
+ this.board
[i
][j
].charCodeAt(1) - 97 <= 5)
166 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
177 let towersCount
= {w: 0, b: 0};
178 for (let i
= 0; i
< this.size
.x
; i
++) {
179 for (let j
= 0; j
< this.size
.y
; j
++) {
180 if (this.board
[i
][j
] != "") {
181 if (this.getPotentialMovesFrom([i
, j
]).length
> 0)
183 towersCount
[ this.board
[i
][j
][0] ]++;
187 if (towersCount
['w'] > towersCount
['b'])
189 if (towersCount
['b'] > towersCount
['w'])