db60da74843a76b47bfea0a6890592ad79236efe
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
{
42 this.playerColor
!= this.turn
||
44 this.board
[coords
.x
][coords
.y
] != "" &&
45 (!this.options
["swap"] || this.movesCount
>= 2)
51 start: {x: coords
.x
, y: coords
.y
},
62 if (this.board
[coords
.x
][coords
.y
] != "") {
67 c: C
.GetOppCol(this.turn
),
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";
82 // NOTE: with small margin seems nicer
83 let width
= 173.2 * this.size
.y
+ 173.2 * (this.size
.y
-1) / 2 + 30,
84 height
= 50 + Math
.floor(150 * this.size
.x
) + 30,
87 if (this.size
.ratio
< 1) {
88 // Rotate by 30 degrees to display vertically
89 [width
, height
] = [height
, width
];
90 [min_x
, min_y
] = [min_y
, min_x
];
94 viewBox="${min_x} ${min_y} ${width} ${height}"
95 class="chessboard_SVG">
99 style="stroke:#000000;stroke-width:1"
100 points="0,-100.0 86.6,-50.0 86.6,50.0 0,100.0 -86.6,50.0 -86.6,-50.0"
105 if (this.size
.ratio
< 1)
106 board
+= ` transform="rotate(30)"`
108 for (let i
=0; i
< this.size
.x
; i
++) {
109 for (let j
=0; j
< this.size
.y
; j
++) {
113 class="neutral-square"
114 id="${this.coordsToId({x: i, y: j})}"
115 x="${173.2*j + 86.6*i}"
120 board
+= `</g><g style="fill:none;stroke-width:10"`;
121 if (this.size
.ratio
< 1)
122 board
+= ` transform="rotate(30)"`
123 // Goals: up/down/left/right
124 board
+= `><polyline style="stroke:red" points="`
125 for (let i
=0; i
<=2*this.size
.y
; i
++)
126 board
+= ((i
-1)*86.6) + ',' + (i
% 2 == 0 ? -50 : -100) + ' ';
127 board
+= `"/><polyline style="stroke:red" points="`;
128 for (let i
=1; i
<=2*this.size
.y
; i
++) {
129 const jShift
= 200 * Math
.floor((this.size
.y
+1)/2) +
130 100 * (Math
.floor(this.size
.y
/2) - 1) +
131 (i
% 2 == 0 ? -50 : 0) +
132 (this.size
.y
% 2 == 0 ? 50 : 0);
133 board
+= ((i
+this.size
.y
-2)*86.6) + ',' + jShift
+ ' ';
135 board
+= `"/><polyline style="stroke:blue" points="`;
137 for (let i
=0; i
<=2*this.size
.x
; i
++) {
138 board
+= ((Math
.floor(i
/2)-1) * 86.6) + ',' +
139 (sumY
+= (i
% 2 == 0 ? 50 : 100)) + ' ';
141 board
+= `"/><polyline style="stroke:blue" points="`;
143 for (let i
=0; i
<2*this.size
.x
; i
++) {
144 board
+= (173.2*this.size
.x
+ (Math
.floor(i
/2)-1) * 86.6) + ',' +
145 (sumY
+= (i
% 2 == 0 ? 50 : 100)) + ' ';
147 board
+= `"/></g></svg>`;
152 for (let i
=0; i
<this.size
.x
; i
++) {
153 for (let j
=0; j
<this.size
.y
; j
++) {
154 if (this.board
[i
][j
] != "") {
155 const sqColor
= (this.getColor(i
, j
) == 'w' ? "white" : "black");
156 const elt
= document
.getElementById(this.coordsToId({x: i
, y: j
}));
157 elt
.classList
.remove("neutral-square");
158 elt
.classList
.add("bg-" + sqColor
);
165 const mousedown
= (e
) => {
166 if (e
.touches
&& e
.touches
.length
> 1)
168 const cd
= this.idToCoords(e
.target
.id
);
170 const move = this.doClick(cd
);
172 this.playPlusVisual(move);
176 if ('onmousedown' in window
) {
177 document
.addEventListener("mousedown", mousedown
);
178 document
.addEventListener("wheel",
179 (e
) => this.rescale(e
.deltaY
< 0 ? "up" : "down"));
181 if ('ontouchstart' in window
)
182 document
.addEventListener("touchstart", mousedown
, {passive: false});
186 const baseRatio
= 1.6191907514450865; //2801.2 / 1730, "widescreen"
187 const rotate
= window
.innerWidth
< window
.innerHeight
; //"vertical screen"
189 x: this.options
["bsize"],
190 y: this.options
["bsize"],
191 ratio: (rotate
? 1 / baseRatio : baseRatio
)
196 this.playOnBoard(move);
198 this.turn
= C
.GetOppCol(this.turn
);
201 getCurrentScore(move) {
202 const oppCol
= C
.GetOppCol(this.turn
);
203 // Search for connecting path of opp color:
204 let explored
= {}, component
;
206 const getIndex
= (x
, y
) => x
+ "." + y
;
207 // Explore one connected component:
208 const neighborsSearch
= ([x
, y
], index
) => {
209 // Let's say "white" connects on x and "black" on y
210 const z
= (oppCol
== 'w' ? x : y
);
215 explored
[index
] = true;
216 component
[index
] = true;
217 for (let [dx
, dy
] of super.pieces()['k'].moves
[0].steps
) {
218 const [nx
, ny
] = [x
+ dx
, y
+ dy
];
219 const nidx
= getIndex(nx
, ny
);
221 this.onBoard(nx
, ny
) &&
222 this.getColor(nx
, ny
) == oppCol
&&
225 neighborsSearch([nx
, ny
], nidx
);
229 // Explore all components:
230 for (let i
=0; i
<this.size
.x
; i
++) {
231 for (let j
=0; j
<this.size
.y
; j
++) {
232 const index
= getIndex(i
, j
);
233 if (this.getColor(i
, j
) == oppCol
&& !explored
[index
]) {
235 [min
, max
] = [this.size
.x
, 0];
236 neighborsSearch([i
, j
], index
);
237 if (max
- min
== this.size
.x
- 1)
238 return (oppCol
== "w" ? "1-0" : "0-1");
246 move.vanish
.forEach(v
=> {
247 let elt
= document
.getElementById(this.coordsToId({x: v
.x
, y: v
.y
}));
248 elt
.classList
.remove("bg-" + (v
.c
== 'w' ? "white" : "black"));
250 move.appear
.forEach(a
=> {
251 let elt
= document
.getElementById(this.coordsToId({x: a
.x
, y: a
.y
}));
252 elt
.classList
.add("bg-" + (a
.c
== 'w' ? "white" : "black"));