8721227f7ef266480fdbfeddc489a2e87a328329
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
{
41 this.playerColor
!= this.turn
||
43 this.board
[coords
.x
][coords
.y
] != "" &&
44 (!this.options
["swap"] || this.movesCount
>= 2)
50 start: {x: coords
.x
, y: coords
.y
},
61 if (this.board
[coords
.x
][coords
.y
] != "") {
66 c: C
.GetOppCol(this.turn
),
75 // NOTE: size.x == size.y (square boards)
76 const emptyCount
= C
.FenEmptySquares(this.size
.x
);
77 return (emptyCount
+ "/").repeat(this.size
.x
).slice(0, -1) + " w 0";
81 // NOTE: with small margin seems nicer
82 let width
= 173.2 * this.size
.y
+ 173.2 * (this.size
.y
-1) / 2 + 30,
83 height
= 50 + Math
.floor(150 * this.size
.x
) + 30,
86 if (this.size
.ratio
< 1) {
87 // Rotate by 30 degrees to display vertically
88 [width
, height
] = [height
, width
];
89 [min_x
, min_y
] = [min_y
, min_x
];
93 viewBox="${min_x} ${min_y} ${width} ${height}"
94 class="chessboard_SVG">
98 style="stroke:#000000;stroke-width:1"
99 points="0,-100.0 86.6,-50.0 86.6,50.0 0,100.0 -86.6,50.0 -86.6,-50.0"
104 if (this.size
.ratio
< 1)
105 board
+= ` transform="rotate(30)"`
107 for (let i
=0; i
< this.size
.x
; i
++) {
108 for (let j
=0; j
< this.size
.y
; j
++) {
112 class="neutral-square"
113 id="${this.coordsToId({x: i, y: j})}"
114 x="${173.2*j + 86.6*i}"
119 board
+= `</g><g style="fill:none;stroke-width:10"`;
120 if (this.size
.ratio
< 1)
121 board
+= ` transform="rotate(30)"`
122 // Goals: up/down/left/right
123 board
+= `><polyline style="stroke:red" points="`
124 for (let i
=0; i
<=2*this.size
.y
; i
++)
125 board
+= ((i
-1)*86.6) + ',' + (i
% 2 == 0 ? -50 : -100) + ' ';
126 board
+= `"/><polyline style="stroke:red" points="`;
127 for (let i
=1; i
<=2*this.size
.y
; i
++) {
128 const jShift
= 200 * Math
.floor((this.size
.y
+1)/2) +
129 100 * (Math
.floor(this.size
.y
/2) - 1) +
130 (i
% 2 == 0 ? -50 : 0) +
131 (this.size
.y
% 2 == 0 ? 50 : 0);
132 board
+= ((i
+this.size
.y
-2)*86.6) + ',' + jShift
+ ' ';
134 board
+= `"/><polyline style="stroke:blue" points="`;
136 for (let i
=0; i
<=2*this.size
.x
; i
++) {
137 board
+= ((Math
.floor(i
/2)-1) * 86.6) + ',' +
138 (sumY
+= (i
% 2 == 0 ? 50 : 100)) + ' ';
140 board
+= `"/><polyline style="stroke:blue" points="`;
142 for (let i
=0; i
<2*this.size
.x
; i
++) {
143 board
+= (173.2*this.size
.x
+ (Math
.floor(i
/2)-1) * 86.6) + ',' +
144 (sumY
+= (i
% 2 == 0 ? 50 : 100)) + ' ';
146 board
+= `"/></g></svg>`;
151 for (let i
=0; i
<this.size
.x
; i
++) {
152 for (let j
=0; j
<this.size
.y
; j
++) {
153 if (this.board
[i
][j
] != "") {
154 const sqColor
= (this.getColor(i
, j
) == 'w' ? "white" : "black");
155 const elt
= document
.getElementById(this.coordsToId({x: i
, y: j
}));
156 elt
.classList
.remove("neutral-square");
157 elt
.classList
.add("bg-" + sqColor
);
164 const baseRatio
= 1.6191907514450865; //2801.2 / 1730, "widescreen"
165 const rotate
= window
.innerWidth
< window
.innerHeight
; //"vertical screen"
167 x: this.options
["bsize"],
168 y: this.options
["bsize"],
169 ratio: (rotate
? 1 / baseRatio : baseRatio
)
174 this.playOnBoard(move);
176 this.turn
= C
.GetOppCol(this.turn
);
179 getCurrentScore(move) {
180 const oppCol
= C
.GetOppCol(this.turn
);
181 // Search for connecting path of opp color:
182 let explored
= {}, component
;
184 const getIndex
= (x
, y
) => x
+ "." + y
;
185 // Explore one connected component:
186 const neighborsSearch
= ([x
, y
], index
) => {
187 // Let's say "white" connects on x and "black" on y
188 const z
= (oppCol
== 'w' ? x : y
);
193 explored
[index
] = true;
194 component
[index
] = true;
195 for (let [dx
, dy
] of super.pieces()['k'].moves
[0].steps
) {
196 const [nx
, ny
] = [x
+ dx
, y
+ dy
];
197 const nidx
= getIndex(nx
, ny
);
199 this.onBoard(nx
, ny
) &&
200 this.getColor(nx
, ny
) == oppCol
&&
203 neighborsSearch([nx
, ny
], nidx
);
207 // Explore all components:
208 for (let i
=0; i
<this.size
.x
; i
++) {
209 for (let j
=0; j
<this.size
.y
; j
++) {
210 const index
= getIndex(i
, j
);
211 if (this.getColor(i
, j
) == oppCol
&& !explored
[index
]) {
213 [min
, max
] = [this.size
.x
, 0];
214 neighborsSearch([i
, j
], index
);
215 if (max
- min
== this.size
.x
- 1)
216 return (oppCol
== "w" ? "1-0" : "0-1");
224 move.vanish
.forEach(v
=> {
225 let elt
= document
.getElementById(this.coordsToId({x: v
.x
, y: v
.y
}));
226 elt
.classList
.remove("bg-" + (v
.c
== 'w' ? "white" : "black"));
228 move.appear
.forEach(a
=> {
229 let elt
= document
.getElementById(this.coordsToId({x: a
.x
, y: a
.y
}));
230 elt
.classList
.add("bg-" + (a
.c
== 'w' ? "white" : "black"));