Fix rescale() instability. TODO: hex rotation vertical board
[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
a1b45f4c 81 // TODO: enable vertical board (rotate?)
728cb1e3 82 getSvgChessboard() {
535c464b
BA
83 // NOTE: with small margin seems nicer
84 let width = 173.2 * this.size.y + 173.2 * (this.size.y-1) / 2 + 30,
85 height = 50 + Math.floor(150 * this.size.x) + 30,
86 min_x = -86.6 - 15,
87 min_y = -100 - 15;
a1b45f4c 88// if (this.size.ratio < 1) {
535c464b
BA
89// [width, height] = [height, width];
90// [min_x, min_y] = [min_y, min_x];
91// }
728cb1e3
BA
92 let board = `
93 <svg
535c464b
BA
94 viewBox="${min_x} ${min_y} ${width} ${height}"
95 class="chessboard_SVG"`;
a1b45f4c 96// if (this.size.ratio < 1)
535c464b
BA
97// board += ` transform="rotate(90 ${min_x} ${min_y})"`
98 board += `>
728cb1e3
BA
99 <defs>
100 <g id="hexa">
101 <polygon
535c464b 102 style="stroke:#000000;stroke-width:1"
728cb1e3
BA
103 points="0,-100.0 86.6,-50.0 86.6,50.0 0,100.0 -86.6,50.0 -86.6,-50.0"
104 />
105 </g>
106 </defs>`;
535c464b 107 board += "<g>";
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 }
535c464b
BA
120 board += `</g><g style="fill:none;stroke-width:10">`;
121 // Goals: up/down/left/right
122 board += `<polyline style="stroke:red" points="`
123 for (let i=0; i<=2*this.size.y; i++)
124 board += ((i-1)*86.6) + ',' + (i % 2 == 0 ? -50 : -100) + ' ';
125 board += `"/><polyline style="stroke:red" points="`;
126 for (let i=1; i<=2*this.size.y; i++) {
127 const jShift = 200 * Math.floor((this.size.y+1)/2) +
128 100 * (Math.floor(this.size.y/2) - 1) +
129 (i % 2 == 0 ? -50 : 0) +
130 (this.size.y % 2 == 0 ? 50 : 0);
131 board += ((i+this.size.y-2)*86.6) + ',' + jShift + ' ';
132 }
133 board += `"/><polyline style="stroke:blue" points="`;
134 let sumY = -100;
135 for (let i=0; i<=2*this.size.x; i++) {
136 board += ((Math.floor(i/2)-1) * 86.6) + ',' +
137 (sumY += (i % 2 == 0 ? 50 : 100)) + ' ';
138 }
139 board += `"/><polyline style="stroke:blue" points="`;
140 sumY = -100;
141 for (let i=0; i<2*this.size.x; i++) {
142 board += (173.2*this.size.x + (Math.floor(i/2)-1) * 86.6) + ',' +
143 (sumY += (i % 2 == 0 ? 50 : 100)) + ' ';
144 }
145 board += `"/></g></svg>`;
728cb1e3
BA
146 return board;
147 }
148
d621e620 149 setupPieces() {
535c464b
BA
150 for (let i=0; i<this.size.x; i++) {
151 for (let j=0; j<this.size.y; j++) {
152 if (this.board[i][j] != "") {
153 const sqColor = (this.getColor(i, j) == 'w' ? "white" : "black");
154 const elt = document.getElementById(this.coordsToId({x: i, y: j}));
155 elt.classList.remove("neutral-square");
156 elt.classList.add("bg-" + sqColor);
157 }
158 }
159 }
d621e620
BA
160 }
161
162 initMouseEvents() {
163 const mousedown = (e) => {
164 if (e.touches && e.touches.length > 1)
165 e.preventDefault();
166 const cd = this.idToCoords(e.target.id);
167 if (cd) {
168 const move = this.doClick(cd);
169 if (move)
170 this.playPlusVisual(move);
171 }
172 };
173
174 if ('onmousedown' in window)
175 document.addEventListener("mousedown", mousedown);
176 if ('ontouchstart' in window)
177 document.addEventListener("touchstart", mousedown, {passive: false});
178 }
179
a1b45f4c 180 // TODO: enable rotation
d621e620 181 get size() {
535c464b 182 const baseRatio = 1.619191; // 2801.2 / 1730, "widescreen"
a1b45f4c 183 const rotate = false; //window.innerWidth < window.innerHeight; //"vertical screen"
d621e620 184 return {
535c464b
BA
185 x: this.options["bsize"],
186 y: this.options["bsize"],
a1b45f4c 187 ratio: (rotate ? 1 / baseRatio : baseRatio)
d621e620
BA
188 };
189 }
190
191 play(move) {
535c464b
BA
192 this.playOnBoard(move);
193 this.movesCount++;
194 this.turn = C.GetOppCol(this.turn);
d621e620
BA
195 }
196
d621e620
BA
197 getCurrentScore(move) {
198 const oppCol = C.GetOppCol(this.turn);
535c464b
BA
199 // Search for connecting path of opp color:
200 let explored = {}, component;
201 let min, max;
202 const getIndex = (x, y) => x + "." + y;
203 // Explore one connected component:
204 const neighborsSearch = ([x, y], index) => {
205 // Let's say "white" connects on x and "black" on y
206 const z = (oppCol == 'w' ? x : y);
207 if (z < min)
208 min = z;
209 if (z > max)
210 max = z;
211 explored[index] = true;
212 component[index] = true;
213 for (let [dx, dy] of super.pieces()['k'].moves[0].steps) {
214 const [nx, ny] = [x + dx, y + dy];
215 const nidx = getIndex(nx, ny);
216 if (
217 this.onBoard(nx, ny) &&
218 this.getColor(nx, ny) == oppCol &&
219 !component[nidx]
220 ) {
221 neighborsSearch([nx, ny], nidx);
222 }
223 }
224 };
225 // Explore all components:
226 for (let i=0; i<this.size.x; i++) {
227 for (let j=0; j<this.size.y; j++) {
228 const index = getIndex(i, j);
229 if (this.getColor(i, j) == oppCol && !explored[index]) {
230 component = {};
231 [min, max] = [this.size.x, 0];
232 neighborsSearch([i, j], index);
233 if (max - min == this.size.x - 1)
234 return (oppCol == "w" ? "1-0" : "0-1");
235 }
236 }
237 }
d621e620
BA
238 return "*";
239 }
240
241 playVisual(move) {
242 move.vanish.forEach(v => {
535c464b
BA
243 let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y}));
244 elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black"));
d621e620
BA
245 });
246 move.appear.forEach(a => {
535c464b
BA
247 let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y}));
248 elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black"));
d621e620 249 });
728cb1e3
BA
250 }
251
d621e620 252};