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"
168 const rotate
= window
.innerWidth
< window
.innerHeight
; //"vertical screen"
170 x: this.options
["bsize"],
171 y: this.options
["bsize"],
172 ratio: (rotate
? 1 / baseRatio : baseRatio
)
177 this.playOnBoard(move);
179 this.turn
= C
.GetOppCol(this.turn
);
182 getCurrentScore(move) {
183 const oppCol
= C
.GetOppCol(this.turn
);
184 // Search for connecting path of opp color:
185 let explored
= {}, component
;
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
);
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
);
202 this.onBoard(nx
, ny
) &&
203 this.getColor(nx
, ny
) == oppCol
&&
206 neighborsSearch([nx
, ny
], nidx
);
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
]) {
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");
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"));
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"));