Commit | Line | Data |
---|---|---|
535c464b BA |
1 | import ChessRules from "/base_rules.js"; |
2 | import PiPo from "/utils/PiPo.js"; | |
3 | import Move from "/utils/Move.js"; | |
4 | ||
d621e620 BA |
5 | export 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 | } | |
d621e620 BA |
35 | get noAnimate() { |
36 | return true; | |
37 | } | |
6b9320bb BA |
38 | get clickOnly() { |
39 | return true; | |
40 | } | |
d621e620 BA |
41 | |
42 | doClick(coords) { | |
43 | if ( | |
535c464b BA |
44 | this.playerColor != this.turn || |
45 | ( | |
46 | this.board[coords.x][coords.y] != "" && | |
47 | (!this.options["swap"] || this.movesCount >= 2) | |
48 | ) | |
d621e620 BA |
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) | |
535c464b BA |
79 | const emptyCount = C.FenEmptySquares(this.size.x); |
80 | return (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0"; | |
d621e620 BA |
81 | } |
82 | ||
728cb1e3 | 83 | getSvgChessboard() { |
535c464b BA |
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; | |
fcede3ef BA |
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 | } | |
728cb1e3 BA |
94 | let board = ` |
95 | <svg | |
535c464b | 96 | viewBox="${min_x} ${min_y} ${width} ${height}" |
fcede3ef | 97 | class="chessboard_SVG"> |
728cb1e3 BA |
98 | <defs> |
99 | <g id="hexa"> | |
100 | <polygon | |
535c464b | 101 | style="stroke:#000000;stroke-width:1" |
728cb1e3 BA |
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>`; | |
fcede3ef BA |
106 | board += "<g"; |
107 | if (this.size.ratio < 1) | |
108 | board += ` transform="rotate(30)"` | |
109 | board += ">"; | |
728cb1e3 BA |
110 | for (let i=0; i < this.size.x; i++) { |
111 | for (let j=0; j < this.size.y; j++) { | |
535c464b BA |
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 | />`; | |
728cb1e3 BA |
120 | } |
121 | } | |
fcede3ef BA |
122 | board += `</g><g style="fill:none;stroke-width:10"`; |
123 | if (this.size.ratio < 1) | |
124 | board += ` transform="rotate(30)"` | |
535c464b | 125 | // Goals: up/down/left/right |
fcede3ef | 126 | board += `><polyline style="stroke:red" points="` |
535c464b BA |
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>`; | |
728cb1e3 BA |
150 | return board; |
151 | } | |
152 | ||
d621e620 | 153 | setupPieces() { |
535c464b BA |
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 | } | |
d621e620 BA |
164 | } |
165 | ||
d621e620 | 166 | get size() { |
fcede3ef BA |
167 | const baseRatio = 1.6191907514450865; //2801.2 / 1730, "widescreen" |
168 | const rotate = window.innerWidth < window.innerHeight; //"vertical screen" | |
d621e620 | 169 | return { |
535c464b BA |
170 | x: this.options["bsize"], |
171 | y: this.options["bsize"], | |
a1b45f4c | 172 | ratio: (rotate ? 1 / baseRatio : baseRatio) |
d621e620 BA |
173 | }; |
174 | } | |
175 | ||
176 | play(move) { | |
535c464b BA |
177 | this.playOnBoard(move); |
178 | this.movesCount++; | |
179 | this.turn = C.GetOppCol(this.turn); | |
d621e620 BA |
180 | } |
181 | ||
d621e620 BA |
182 | getCurrentScore(move) { |
183 | const oppCol = C.GetOppCol(this.turn); | |
535c464b BA |
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 | } | |
d621e620 BA |
223 | return "*"; |
224 | } | |
225 | ||
226 | playVisual(move) { | |
227 | move.vanish.forEach(v => { | |
535c464b BA |
228 | let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y})); |
229 | elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black")); | |
d621e620 BA |
230 | }); |
231 | move.appear.forEach(a => { | |
535c464b BA |
232 | let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y})); |
233 | elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black")); | |
d621e620 | 234 | }); |
728cb1e3 BA |
235 | } |
236 | ||
d621e620 | 237 | }; |