ab634b8891e94b849295ba684f48aa592a8e8285
[xogo.git] / variants / Hex / class.js
1 import ChessRules from "/base_rules.js";
2 import PiPo from "/utils/PiPo.js";
3 import Move from "/utils/Move.js";
4
5 export default class HexRules extends ChessRules {
6
7 static get Options() {
8 return {
9 input: [
10 {
11 label: "Board size",
12 variable: "bsize",
13 type: "number",
14 defaut: 11
15 },
16 {
17 label: "Swap",
18 variable: "swap",
19 type: "checkbox",
20 defaut: true
21 }
22 ]
23 };
24 }
25
26 get hasFlags() {
27 return false;
28 }
29 get hasEnpassant() {
30 return false;
31 }
32 get hasReserve() {
33 return false;
34 }
35
36 get noAnimate() {
37 return true;
38 }
39
40 doClick(coords) {
41 if (
42 this.playerColor != this.turn ||
43 (
44 this.board[coords.x][coords.y] != "" &&
45 (!this.options["swap"] || this.movesCount >= 2)
46 )
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)
77 const emptyCount = C.FenEmptySquares(this.size.x);
78 return (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0";
79 }
80
81 // TODO: enable vertical board (rotate?)
82 getSvgChessboard() {
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;
88 // if (this.size.ratio < 1) {
89 // [width, height] = [height, width];
90 // [min_x, min_y] = [min_y, min_x];
91 // }
92 let board = `
93 <svg
94 viewBox="${min_x} ${min_y} ${width} ${height}"
95 class="chessboard_SVG"`;
96 // if (this.size.ratio < 1)
97 // board += ` transform="rotate(90 ${min_x} ${min_y})"`
98 board += `>
99 <defs>
100 <g id="hexa">
101 <polygon
102 style="stroke:#000000;stroke-width:1"
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>`;
107 board += "<g>";
108 for (let i=0; i < this.size.x; i++) {
109 for (let j=0; j < this.size.y; j++) {
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 />`;
118 }
119 }
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>`;
146 return board;
147 }
148
149 setupPieces() {
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 }
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
180 // TODO: enable rotation
181 get size() {
182 const baseRatio = 1.619191; // 2801.2 / 1730, "widescreen"
183 const rotate = false; //window.innerWidth < window.innerHeight; //"vertical screen"
184 return {
185 x: this.options["bsize"],
186 y: this.options["bsize"],
187 ratio: (rotate ? 1 / baseRatio : baseRatio)
188 };
189 }
190
191 play(move) {
192 this.playOnBoard(move);
193 this.movesCount++;
194 this.turn = C.GetOppCol(this.turn);
195 }
196
197 getCurrentScore(move) {
198 const oppCol = C.GetOppCol(this.turn);
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 }
238 return "*";
239 }
240
241 playVisual(move) {
242 move.vanish.forEach(v => {
243 let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y}));
244 elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black"));
245 });
246 move.appear.forEach(a => {
247 let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y}));
248 elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black"));
249 });
250 }
251
252 };