Fix size of chessboard following container
[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 get noAnimate() {
36 return true;
37 }
38 get clickOnly() {
39 return true;
40 }
41
42 doClick(coords) {
43 if (
44 this.playerColor != this.turn ||
45 (
46 this.board[coords.x][coords.y] != "" &&
47 (!this.options["swap"] || this.movesCount >= 2)
48 )
49 ) {
50 return null;
51 }
52 let res = new Move({
53 start: {x: coords.x, y: coords.y},
54 appear: [
55 new PiPo({
56 x: coords.x,
57 y: coords.y,
58 c: this.turn,
59 p: 'p'
60 })
61 ],
62 vanish: []
63 });
64 if (this.board[coords.x][coords.y] != "") {
65 res.vanish.push(
66 new PiPo({
67 x: coords.x,
68 y: coords.y,
69 c: C.GetOppCol(this.turn),
70 p: 'p'
71 })
72 );
73 }
74 return res;
75 }
76
77 genRandInitFen() {
78 // NOTE: size.x == size.y (square boards)
79 const emptyCount = C.FenEmptySquares(this.size.x);
80 return (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0";
81 }
82
83 getSvgChessboard() {
84 // NOTE: with small margin seems nicer
85 let width = 173.2 * this.size.y + 173.2 * (this.size.y-1) / 2 + 30,
86 height = 50 + Math.floor(150 * this.size.x) + 30,
87 min_x = -86.6 - 15,
88 min_y = -100 - 15;
89 if (this.size.ratio < 1) {
90 // Rotate by 30 degrees to display vertically
91 [width, height] = [height, width];
92 [min_x, min_y] = [min_y, min_x];
93 }
94 let board = `
95 <svg
96 viewBox="${min_x} ${min_y} ${width} ${height}"
97 class="chessboard_SVG">
98 <defs>
99 <g id="hexa">
100 <polygon
101 style="stroke:#000000;stroke-width:1"
102 points="0,-100.0 86.6,-50.0 86.6,50.0 0,100.0 -86.6,50.0 -86.6,-50.0"
103 />
104 </g>
105 </defs>`;
106 board += "<g";
107 if (this.size.ratio < 1)
108 board += ` transform="rotate(30)"`
109 board += ">";
110 for (let i=0; i < this.size.x; i++) {
111 for (let j=0; j < this.size.y; j++) {
112 board += `
113 <use
114 href="#hexa"
115 class="neutral-square"
116 id="${this.coordsToId({x: i, y: j})}"
117 x="${173.2*j + 86.6*i}"
118 y="${150*i}"
119 />`;
120 }
121 }
122 board += `</g><g style="fill:none;stroke-width:10"`;
123 if (this.size.ratio < 1)
124 board += ` transform="rotate(30)"`
125 // Goals: up/down/left/right
126 board += `><polyline style="stroke:red" points="`
127 for (let i=0; i<=2*this.size.y; i++)
128 board += ((i-1)*86.6) + ',' + (i % 2 == 0 ? -50 : -100) + ' ';
129 board += `"/><polyline style="stroke:red" points="`;
130 for (let i=1; i<=2*this.size.y; i++) {
131 const jShift = 200 * Math.floor((this.size.y+1)/2) +
132 100 * (Math.floor(this.size.y/2) - 1) +
133 (i % 2 == 0 ? -50 : 0) +
134 (this.size.y % 2 == 0 ? 50 : 0);
135 board += ((i+this.size.y-2)*86.6) + ',' + jShift + ' ';
136 }
137 board += `"/><polyline style="stroke:blue" points="`;
138 let sumY = -100;
139 for (let i=0; i<=2*this.size.x; i++) {
140 board += ((Math.floor(i/2)-1) * 86.6) + ',' +
141 (sumY += (i % 2 == 0 ? 50 : 100)) + ' ';
142 }
143 board += `"/><polyline style="stroke:blue" points="`;
144 sumY = -100;
145 for (let i=0; i<2*this.size.x; i++) {
146 board += (173.2*this.size.x + (Math.floor(i/2)-1) * 86.6) + ',' +
147 (sumY += (i % 2 == 0 ? 50 : 100)) + ' ';
148 }
149 board += `"/></g></svg>`;
150 return board;
151 }
152
153 setupPieces() {
154 for (let i=0; i<this.size.x; i++) {
155 for (let j=0; j<this.size.y; j++) {
156 if (this.board[i][j] != "") {
157 const sqColor = (this.getColor(i, j) == 'w' ? "white" : "black");
158 const elt = document.getElementById(this.coordsToId({x: i, y: j}));
159 elt.classList.remove("neutral-square");
160 elt.classList.add("bg-" + sqColor);
161 }
162 }
163 }
164 }
165
166 get size() {
167 const baseRatio = 1.6191907514450865; //2801.2 / 1730, "widescreen"
168 const rotate = window.innerWidth < window.innerHeight; //"vertical screen"
169 return {
170 x: this.options["bsize"],
171 y: this.options["bsize"],
172 ratio: (rotate ? 1 / baseRatio : baseRatio)
173 };
174 }
175
176 play(move) {
177 this.playOnBoard(move);
178 this.movesCount++;
179 this.turn = C.GetOppCol(this.turn);
180 }
181
182 getCurrentScore(move) {
183 const oppCol = C.GetOppCol(this.turn);
184 // Search for connecting path of opp color:
185 let explored = {}, component;
186 let min, max;
187 const getIndex = (x, y) => x + "." + y;
188 // Explore one connected component:
189 const neighborsSearch = ([x, y], index) => {
190 // Let's say "white" connects on x and "black" on y
191 const z = (oppCol == 'w' ? x : y);
192 if (z < min)
193 min = z;
194 if (z > max)
195 max = z;
196 explored[index] = true;
197 component[index] = true;
198 for (let [dx, dy] of super.pieces()['k'].moves[0].steps) {
199 const [nx, ny] = [x + dx, y + dy];
200 const nidx = getIndex(nx, ny);
201 if (
202 this.onBoard(nx, ny) &&
203 this.getColor(nx, ny) == oppCol &&
204 !component[nidx]
205 ) {
206 neighborsSearch([nx, ny], nidx);
207 }
208 }
209 };
210 // Explore all components:
211 for (let i=0; i<this.size.x; i++) {
212 for (let j=0; j<this.size.y; j++) {
213 const index = getIndex(i, j);
214 if (this.getColor(i, j) == oppCol && !explored[index]) {
215 component = {};
216 [min, max] = [this.size.x, 0];
217 neighborsSearch([i, j], index);
218 if (max - min == this.size.x - 1)
219 return (oppCol == "w" ? "1-0" : "0-1");
220 }
221 }
222 }
223 return "*";
224 }
225
226 playVisual(move) {
227 move.vanish.forEach(v => {
228 let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y}));
229 elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black"));
230 });
231 move.appear.forEach(a => {
232 let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y}));
233 elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black"));
234 });
235 }
236
237 };