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 moves: [{steps: steps
, range: 1}]
66 genRandInitBaseFen() {
69 fen
= "9/".repeat(8) + "9 w 0";
70 else if (this.options
["randomness"] == 0) {
71 fen
= "2Bb5/1BbBb4/1bBbBbB2/1BbBbBbBb/BbBb1bBbB/" +
72 "bBbBbBbB1/2BbBbBb1/4bBbB1/5bB2 w 0";
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) +
86 return { fen: fen
, o: {} };
89 getSquareColorClass(x
, y
) {
98 if (!super.onBoard(x
, y
))
102 return [2, 3].includes(y
);
104 return [1, 2, 3, 4].includes(y
);
106 return [1, 2, 3, 4, 5, 6].includes(y
);
114 return [2, 3, 4, 5, 6, 7].includes(y
);
116 return [4, 5, 6, 7].includes(y
);
118 return [5, 6].includes(y
);
120 return false; //never reached
124 return this.playerColor
== this.turn
;
128 if (!this.freefill
|| this.board
[coords
.x
][coords
.y
] != "")
131 start: {x: coords
.x
, y: coords
.y
},
133 appear: [new PiPo({x: coords
.x
, y: coords
.y
, c: this.turn
, p: 'b'})]
137 getBasicMove([x1
, y1
], [x2
, y2
]) {
138 const cp1
= this.board
[x1
][y1
],
139 cp2
= this.board
[x2
][y2
];
141 String
.fromCharCode(cp1
.charCodeAt(1) + cp2
.charCodeAt(1) - 97);
145 new PiPo({ x: x1
, y: y1
, c: cp1
.charAt(0), p: cp1
.charAt(1) }),
146 new PiPo({ x: x2
, y: y2
, c: cp2
.charAt(0), p: cp2
.charAt(1) })
149 new PiPo({ x: x2
, y: y2
, c: cp1
.charAt(0), p: newPiece
})
155 getPotentialMovesFrom([x
, y
]) {
156 const height
= this.board
[x
][y
].charCodeAt(1) - 97;
160 for (let s
of this.pieces()['b'].moves
[0].steps
) {
161 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
163 this.onBoard(i
, j
) &&
164 this.board
[i
][j
] != "" &&
165 (height
+ this.board
[i
][j
].charCodeAt(1) - 97 <= 5)
167 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
178 let towersCount
= {w: 0, b: 0};
179 for (let i
= 0; i
< this.size
.x
; i
++) {
180 for (let j
= 0; j
< this.size
.y
; j
++) {
181 if (this.board
[i
][j
] != "") {
182 if (this.getPotentialMovesFrom([i
, j
]).length
> 0)
184 towersCount
[ this.board
[i
][j
][0] ]++;
188 if (towersCount
['w'] > towersCount
['b'])
190 if (towersCount
['b'] > towersCount
['w'])