ab634b8891e94b849295ba684f48aa592a8e8285
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";
81 // TODO: enable vertical board (rotate?)
83 // NOTE: with small margin seems nicer
84 let width
= 173.2 * this.size
.y
+ 173.2 * (this.size
.y
-1) / 2 + 30,
85 height
= 50 + Math
.floor(150 * this.size
.x
) + 30,
88 // if (this.size.ratio < 1) {
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"`;
96 // if (this.size.ratio < 1)
97 // board += ` transform="rotate(90 ${min_x} ${min_y})"`
102 style="stroke:#000000;stroke-width:1"
103 points="0,-100.0 86.6,-50.0 86.6,50.0 0,100.0 -86.6,50.0 -86.6,-50.0"
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 // Goals: up/down/left/right
122 board
+= `<polyline style="stroke:red" points="`
123 for (let i
=0; i
<=2*this.size
.y
; i
++)
124 board
+= ((i
-1)*86.6) + ',' + (i
% 2 == 0 ? -50 : -100) + ' ';
125 board
+= `"/><polyline style="stroke:red" points="`;
126 for (let i
=1; i
<=2*this.size
.y
; i
++) {
127 const jShift
= 200 * Math
.floor((this.size
.y
+1)/2) +
128 100 * (Math
.floor(this.size
.y
/2) - 1) +
129 (i
% 2 == 0 ? -50 : 0) +
130 (this.size
.y
% 2 == 0 ? 50 : 0);
131 board
+= ((i
+this.size
.y
-2)*86.6) + ',' + jShift
+ ' ';
133 board
+= `"/><polyline style="stroke:blue" points="`;
135 for (let i
=0; i
<=2*this.size
.x
; i
++) {
136 board
+= ((Math
.floor(i
/2)-1) * 86.6) + ',' +
137 (sumY
+= (i
% 2 == 0 ? 50 : 100)) + ' ';
139 board
+= `"/><polyline style="stroke:blue" points="`;
141 for (let i
=0; i
<2*this.size
.x
; i
++) {
142 board
+= (173.2*this.size
.x
+ (Math
.floor(i
/2)-1) * 86.6) + ',' +
143 (sumY
+= (i
% 2 == 0 ? 50 : 100)) + ' ';
145 board
+= `"/></g></svg>`;
150 for (let i
=0; i
<this.size
.x
; i
++) {
151 for (let j
=0; j
<this.size
.y
; j
++) {
152 if (this.board
[i
][j
] != "") {
153 const sqColor
= (this.getColor(i
, j
) == 'w' ? "white" : "black");
154 const elt
= document
.getElementById(this.coordsToId({x: i
, y: j
}));
155 elt
.classList
.remove("neutral-square");
156 elt
.classList
.add("bg-" + sqColor
);
163 const mousedown
= (e
) => {
164 if (e
.touches
&& e
.touches
.length
> 1)
166 const cd
= this.idToCoords(e
.target
.id
);
168 const move = this.doClick(cd
);
170 this.playPlusVisual(move);
174 if ('onmousedown' in window
)
175 document
.addEventListener("mousedown", mousedown
);
176 if ('ontouchstart' in window
)
177 document
.addEventListener("touchstart", mousedown
, {passive: false});
180 // TODO: enable rotation
182 const baseRatio
= 1.619191; // 2801.2 / 1730, "widescreen"
183 const rotate
= false; //window.innerWidth < window.innerHeight; //"vertical screen"
185 x: this.options
["bsize"],
186 y: this.options
["bsize"],
187 ratio: (rotate
? 1 / baseRatio : baseRatio
)
192 this.playOnBoard(move);
194 this.turn
= C
.GetOppCol(this.turn
);
197 getCurrentScore(move) {
198 const oppCol
= C
.GetOppCol(this.turn
);
199 // Search for connecting path of opp color:
200 let explored
= {}, component
;
202 const getIndex
= (x
, y
) => x
+ "." + y
;
203 // Explore one connected component:
204 const neighborsSearch
= ([x
, y
], index
) => {
205 // Let's say "white" connects on x and "black" on y
206 const z
= (oppCol
== 'w' ? x : y
);
211 explored
[index
] = true;
212 component
[index
] = true;
213 for (let [dx
, dy
] of super.pieces()['k'].moves
[0].steps
) {
214 const [nx
, ny
] = [x
+ dx
, y
+ dy
];
215 const nidx
= getIndex(nx
, ny
);
217 this.onBoard(nx
, ny
) &&
218 this.getColor(nx
, ny
) == oppCol
&&
221 neighborsSearch([nx
, ny
], nidx
);
225 // Explore all components:
226 for (let i
=0; i
<this.size
.x
; i
++) {
227 for (let j
=0; j
<this.size
.y
; j
++) {
228 const index
= getIndex(i
, j
);
229 if (this.getColor(i
, j
) == oppCol
&& !explored
[index
]) {
231 [min
, max
] = [this.size
.x
, 0];
232 neighborsSearch([i
, j
], index
);
233 if (max
- min
== this.size
.x
- 1)
234 return (oppCol
== "w" ? "1-0" : "0-1");
242 move.vanish
.forEach(v
=> {
243 let elt
= document
.getElementById(this.coordsToId({x: v
.x
, y: v
.y
}));
244 elt
.classList
.remove("bg-" + (v
.c
== 'w' ? "white" : "black"));
246 move.appear
.forEach(a
=> {
247 let elt
= document
.getElementById(this.coordsToId({x: a
.x
, y: a
.y
}));
248 elt
.classList
.add("bg-" + (a
.c
== 'w' ? "white" : "black"));