1 import ChessRules
from "/base_rules.js";
2 import PiPo
from "/utils/PiPo.js";
3 import Move
from "/utils/Move.js";
5 export default class HexRules
extends ChessRules
{
44 this.playerColor
!= this.turn
||
46 this.board
[coords
.x
][coords
.y
] != "" &&
47 (!this.options
["swap"] || this.movesCount
>= 2)
53 start: {x: coords
.x
, y: coords
.y
},
64 if (this.board
[coords
.x
][coords
.y
] != "") {
69 c: C
.GetOppCol(this.turn
),
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";
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,
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
];
96 viewBox="${min_x} ${min_y} ${width} ${height}"
97 class="chessboard_SVG">
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"
107 if (this.size
.ratio
< 1)
108 board
+= ` transform="rotate(30)"`
110 for (let i
=0; i
< this.size
.x
; i
++) {
111 for (let j
=0; j
< this.size
.y
; j
++) {
115 class="neutral-square"
116 id="${this.coordsToId({x: i, y: j})}"
117 x="${173.2*j + 86.6*i}"
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
+ ' ';
137 board
+= `"/><polyline style="stroke:blue" points="`;
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)) + ' ';
143 board
+= `"/><polyline style="stroke:blue" points="`;
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)) + ' ';
149 board
+= `"/></g></svg>`;
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
);
167 const baseRatio
= 1.6191907514450865; //2801.2 / 1730, "widescreen"
169 document
.getElementById(this.containerid
).getBoundingClientRect();
170 const rotate
= rc
.width
< rc
.height
; //"vertical screen"
172 x: this.options
["bsize"],
173 y: this.options
["bsize"],
174 ratio: (rotate
? 1 / baseRatio : baseRatio
)
179 this.playOnBoard(move);
181 this.turn
= C
.GetOppCol(this.turn
);
184 getCurrentScore(move) {
185 const oppCol
= C
.GetOppCol(this.turn
);
186 // Search for connecting path of opp color:
187 let explored
= {}, component
;
189 const getIndex
= (x
, y
) => x
+ "." + y
;
190 // Explore one connected component:
191 const neighborsSearch
= ([x
, y
], index
) => {
192 // Let's say "white" connects on x and "black" on y
193 const z
= (oppCol
== 'w' ? x : y
);
198 explored
[index
] = true;
199 component
[index
] = true;
200 for (let [dx
, dy
] of super.pieces()['k'].moves
[0].steps
) {
201 const [nx
, ny
] = [x
+ dx
, y
+ dy
];
202 const nidx
= getIndex(nx
, ny
);
204 this.onBoard(nx
, ny
) &&
205 this.getColor(nx
, ny
) == oppCol
&&
208 neighborsSearch([nx
, ny
], nidx
);
212 // Explore all components:
213 for (let i
=0; i
<this.size
.x
; i
++) {
214 for (let j
=0; j
<this.size
.y
; j
++) {
215 const index
= getIndex(i
, j
);
216 if (this.getColor(i
, j
) == oppCol
&& !explored
[index
]) {
218 [min
, max
] = [this.size
.x
, 0];
219 neighborsSearch([i
, j
], index
);
220 if (max
- min
== this.size
.x
- 1)
221 return (oppCol
== "w" ? "1-0" : "0-1");
229 move.vanish
.forEach(v
=> {
230 let elt
= document
.getElementById(this.coordsToId({x: v
.x
, y: v
.y
}));
231 elt
.classList
.remove("bg-" + (v
.c
== 'w' ? "white" : "black"));
233 move.appear
.forEach(a
=> {
234 let elt
= document
.getElementById(this.coordsToId({x: a
.x
, y: a
.y
}));
235 elt
.classList
.add("bg-" + (a
.c
== 'w' ? "white" : "black"));