Chakart almost ready...
[xogo.git] / variants / Hex / class.js
CommitLineData
535c464b
BA
1import ChessRules from "/base_rules.js";
2import PiPo from "/utils/PiPo.js";
3import Move from "/utils/Move.js";
4
d621e620
BA
5export default class HexRules extends ChessRules {
6
7 static get Options() {
8 return {
9 input: [
10 {
11 label: "Board size",
535c464b 12 variable: "bsize",
d621e620 13 type: "number",
535c464b
BA
14 defaut: 11
15 },
d621e620
BA
16 {
17 label: "Swap",
535c464b
BA
18 variable: "swap",
19 type: "checkbox",
20 defaut: true
d621e620
BA
21 }
22 ]
23 };
24 }
25
535c464b
BA
26 get hasFlags() {
27 return false;
28 }
29 get hasEnpassant() {
30 return false;
31 }
d621e620
BA
32 get hasReserve() {
33 return false;
34 }
35
36 get noAnimate() {
37 return true;
38 }
39
40 doClick(coords) {
41 if (
535c464b
BA
42 this.playerColor != this.turn ||
43 (
44 this.board[coords.x][coords.y] != "" &&
45 (!this.options["swap"] || this.movesCount >= 2)
46 )
d621e620
BA
47 ) {
48 return null;
49 }
50 let res = new Move({
51 start: {x: coords.x, y: coords.y},
52 appear: [
53 new PiPo({
54 x: coords.x,
55 y: coords.y,
56 c: this.turn,
57 p: 'p'
58 })
59 ],
60 vanish: []
61 });
62 if (this.board[coords.x][coords.y] != "") {
63 res.vanish.push(
64 new PiPo({
65 x: coords.x,
66 y: coords.y,
67 c: C.GetOppCol(this.turn),
68 p: 'p'
69 })
70 );
71 }
72 return res;
73 }
74
75 genRandInitFen() {
76 // NOTE: size.x == size.y (square boards)
535c464b
BA
77 const emptyCount = C.FenEmptySquares(this.size.x);
78 return (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0";
d621e620
BA
79 }
80
728cb1e3 81 getSvgChessboard() {
535c464b
BA
82 // NOTE: with small margin seems nicer
83 let width = 173.2 * this.size.y + 173.2 * (this.size.y-1) / 2 + 30,
84 height = 50 + Math.floor(150 * this.size.x) + 30,
85 min_x = -86.6 - 15,
86 min_y = -100 - 15;
fcede3ef
BA
87 if (this.size.ratio < 1) {
88 // Rotate by 30 degrees to display vertically
89 [width, height] = [height, width];
90 [min_x, min_y] = [min_y, min_x];
91 }
728cb1e3
BA
92 let board = `
93 <svg
535c464b 94 viewBox="${min_x} ${min_y} ${width} ${height}"
fcede3ef 95 class="chessboard_SVG">
728cb1e3
BA
96 <defs>
97 <g id="hexa">
98 <polygon
535c464b 99 style="stroke:#000000;stroke-width:1"
728cb1e3
BA
100 points="0,-100.0 86.6,-50.0 86.6,50.0 0,100.0 -86.6,50.0 -86.6,-50.0"
101 />
102 </g>
103 </defs>`;
fcede3ef
BA
104 board += "<g";
105 if (this.size.ratio < 1)
106 board += ` transform="rotate(30)"`
107 board += ">";
728cb1e3
BA
108 for (let i=0; i < this.size.x; i++) {
109 for (let j=0; j < this.size.y; j++) {
535c464b
BA
110 board += `
111 <use
112 href="#hexa"
113 class="neutral-square"
114 id="${this.coordsToId({x: i, y: j})}"
115 x="${173.2*j + 86.6*i}"
116 y="${150*i}"
117 />`;
728cb1e3
BA
118 }
119 }
fcede3ef
BA
120 board += `</g><g style="fill:none;stroke-width:10"`;
121 if (this.size.ratio < 1)
122 board += ` transform="rotate(30)"`
535c464b 123 // Goals: up/down/left/right
fcede3ef 124 board += `><polyline style="stroke:red" points="`
535c464b
BA
125 for (let i=0; i<=2*this.size.y; i++)
126 board += ((i-1)*86.6) + ',' + (i % 2 == 0 ? -50 : -100) + ' ';
127 board += `"/><polyline style="stroke:red" points="`;
128 for (let i=1; i<=2*this.size.y; i++) {
129 const jShift = 200 * Math.floor((this.size.y+1)/2) +
130 100 * (Math.floor(this.size.y/2) - 1) +
131 (i % 2 == 0 ? -50 : 0) +
132 (this.size.y % 2 == 0 ? 50 : 0);
133 board += ((i+this.size.y-2)*86.6) + ',' + jShift + ' ';
134 }
135 board += `"/><polyline style="stroke:blue" points="`;
136 let sumY = -100;
137 for (let i=0; i<=2*this.size.x; i++) {
138 board += ((Math.floor(i/2)-1) * 86.6) + ',' +
139 (sumY += (i % 2 == 0 ? 50 : 100)) + ' ';
140 }
141 board += `"/><polyline style="stroke:blue" points="`;
142 sumY = -100;
143 for (let i=0; i<2*this.size.x; i++) {
144 board += (173.2*this.size.x + (Math.floor(i/2)-1) * 86.6) + ',' +
145 (sumY += (i % 2 == 0 ? 50 : 100)) + ' ';
146 }
147 board += `"/></g></svg>`;
728cb1e3
BA
148 return board;
149 }
150
d621e620 151 setupPieces() {
535c464b
BA
152 for (let i=0; i<this.size.x; i++) {
153 for (let j=0; j<this.size.y; j++) {
154 if (this.board[i][j] != "") {
155 const sqColor = (this.getColor(i, j) == 'w' ? "white" : "black");
156 const elt = document.getElementById(this.coordsToId({x: i, y: j}));
157 elt.classList.remove("neutral-square");
158 elt.classList.add("bg-" + sqColor);
159 }
160 }
161 }
d621e620
BA
162 }
163
164 initMouseEvents() {
165 const mousedown = (e) => {
166 if (e.touches && e.touches.length > 1)
167 e.preventDefault();
168 const cd = this.idToCoords(e.target.id);
169 if (cd) {
170 const move = this.doClick(cd);
171 if (move)
172 this.playPlusVisual(move);
173 }
174 };
175
437dfd42 176 if ('onmousedown' in window) {
d621e620 177 document.addEventListener("mousedown", mousedown);
437dfd42
BA
178 document.addEventListener("wheel",
179 (e) => this.rescale(e.deltaY < 0 ? "up" : "down"));
180 }
d621e620
BA
181 if ('ontouchstart' in window)
182 document.addEventListener("touchstart", mousedown, {passive: false});
183 }
184
185 get size() {
fcede3ef
BA
186 const baseRatio = 1.6191907514450865; //2801.2 / 1730, "widescreen"
187 const rotate = window.innerWidth < window.innerHeight; //"vertical screen"
d621e620 188 return {
535c464b
BA
189 x: this.options["bsize"],
190 y: this.options["bsize"],
a1b45f4c 191 ratio: (rotate ? 1 / baseRatio : baseRatio)
d621e620
BA
192 };
193 }
194
195 play(move) {
535c464b
BA
196 this.playOnBoard(move);
197 this.movesCount++;
198 this.turn = C.GetOppCol(this.turn);
d621e620
BA
199 }
200
d621e620
BA
201 getCurrentScore(move) {
202 const oppCol = C.GetOppCol(this.turn);
535c464b
BA
203 // Search for connecting path of opp color:
204 let explored = {}, component;
205 let min, max;
206 const getIndex = (x, y) => x + "." + y;
207 // Explore one connected component:
208 const neighborsSearch = ([x, y], index) => {
209 // Let's say "white" connects on x and "black" on y
210 const z = (oppCol == 'w' ? x : y);
211 if (z < min)
212 min = z;
213 if (z > max)
214 max = z;
215 explored[index] = true;
216 component[index] = true;
217 for (let [dx, dy] of super.pieces()['k'].moves[0].steps) {
218 const [nx, ny] = [x + dx, y + dy];
219 const nidx = getIndex(nx, ny);
220 if (
221 this.onBoard(nx, ny) &&
222 this.getColor(nx, ny) == oppCol &&
223 !component[nidx]
224 ) {
225 neighborsSearch([nx, ny], nidx);
226 }
227 }
228 };
229 // Explore all components:
230 for (let i=0; i<this.size.x; i++) {
231 for (let j=0; j<this.size.y; j++) {
232 const index = getIndex(i, j);
233 if (this.getColor(i, j) == oppCol && !explored[index]) {
234 component = {};
235 [min, max] = [this.size.x, 0];
236 neighborsSearch([i, j], index);
237 if (max - min == this.size.x - 1)
238 return (oppCol == "w" ? "1-0" : "0-1");
239 }
240 }
241 }
d621e620
BA
242 return "*";
243 }
244
245 playVisual(move) {
246 move.vanish.forEach(v => {
535c464b
BA
247 let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y}));
248 elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black"));
d621e620
BA
249 });
250 move.appear.forEach(a => {
535c464b
BA
251 let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y}));
252 elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black"));
d621e620 253 });
728cb1e3
BA
254 }
255
d621e620 256};