2 import { getSquareId, getSquareFromId } from "@/utils/squareId";
3 import { ArrayFun } from "@/utils/array";
4 import { store } from "@/store";
7 // Last move cannot be guessed from here, and is required to highlight squares
8 // vr: object to check moves, print board...
9 // userColor is left undefined for an external observer
22 possibleMoves: [], //filled after each valid click/dragstart
23 choices: [], //promotion pieces, or checkered captures... (as moves)
24 selectedPiece: null, //moving piece (or clicked piece)
25 start: {}, //pixels coordinates + id of starting square (click or drag)
26 settings: store.state.settings
31 // Return empty div of class 'game' to avoid error when setting size
38 const [sizeX, sizeY] = [V.size.x, V.size.y];
39 // Precompute hints squares to facilitate rendering
40 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
41 this.possibleMoves.forEach(m => {
42 hintSquares[m.end.x][m.end.y] = true;
44 // Also precompute in-check squares
45 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
46 this.incheck.forEach(sq => {
47 incheckSq[sq[0]][sq[1]] = true;
50 const lm = this.lastMove;
52 this.settings.highlight &&
53 ["all","highlight"].includes(V.ShowMoves)
55 const orientation = !V.CanFlip ? "w" : this.orientation;
56 // Ensure that squares colors do not change when board is flipped
57 const lightSquareMod = (sizeX + sizeY) % 2;
58 const showPiece = (x, y) => {
60 this.vr.board[x][y] != V.EMPTY &&
61 (!this.vr.enlightened || this.analyze || this.score != "*" ||
62 (!!this.userColor && this.vr.enlightened[this.userColor][x][y]))
65 const inHighlight = (x, y) => {
66 return showLight && !!lm && (
67 (lm.end.x == x && lm.end.y == y) ||
68 (lm.start.x == x && lm.start.y == y));
70 const inShadow = (x, y) => {
74 this.vr.enlightened &&
75 (!this.userColor || !this.vr.enlightened[this.userColor][x][y])
78 // Create board element (+ reserves if needed by variant)
87 [...Array(sizeX).keys()].map(i => {
88 const ci = orientation == "w" ? i : sizeX - i - 1;
95 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
97 [...Array(sizeY).keys()].map(j => {
98 const cj = orientation == "w" ? j : sizeY - j - 1;
100 if (showPiece(ci, cj)) {
106 !!this.selectedPiece &&
107 this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj
112 this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) +
118 if (this.settings.hints && hintSquares[ci][cj]) {
125 src: "/images/mark.svg"
130 const lightSquare = (ci + cj) % 2 == lightSquareMod;
136 ["board" + sizeY]: true,
137 "light-square": lightSquare,
138 "dark-square": !lightSquare,
139 [this.settings.bcolor]: true,
140 "in-shadow": inShadow(ci, cj),
141 "highlight-light": inHighlight(ci, cj) && lightSquare,
142 "highlight-dark": inHighlight(ci, cj) && !lightSquare,
143 "incheck-light": showLight && lightSquare && incheckSq[ci][cj],
144 "incheck-dark": showLight && !lightSquare && incheckSq[ci][cj]
147 id: getSquareId({ x: ci, y: cj })
156 let elementArray = [gameDiv];
157 const playingColor = this.userColor || "w"; //default for an observer
158 if (this.vr.reserve) {
159 const shiftIdx = playingColor == "w" ? 0 : 1;
160 let myReservePiecesArray = [];
161 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
162 myReservePiecesArray.push(
166 class: { board: true, ["board" + sizeY]: true },
167 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }
171 class: { piece: true, reserve: true },
175 this.vr.getReservePpath(i, playingColor) +
179 h("sup", { class: { "reserve-count": true } }, [
180 this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
186 let oppReservePiecesArray = [];
187 const oppCol = V.GetOppCol(playingColor);
188 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
189 oppReservePiecesArray.push(
193 class: { board: true, ["board" + sizeY]: true },
194 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }
198 class: { piece: true, reserve: true },
202 this.vr.getReservePpath(i, oppCol) +
206 h("sup", { class: { "reserve-count": true } }, [
207 this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]
227 "reserve-row-1": true
232 h("div", { class: { row: true } }, oppReservePiecesArray)
235 elementArray.push(reserves);
237 const boardElt = document.querySelector(".game");
238 if (this.choices.length > 0 && !!boardElt) {
239 //no choices to show at first drawing
240 const squareWidth = boardElt.offsetWidth / sizeY;
241 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
245 attrs: { id: "choices" },
246 class: { row: true },
248 top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
251 (squareWidth * (sizeY - this.choices.length)) / 2 +
253 width: this.choices.length * squareWidth + "px",
254 height: squareWidth + "px"
257 this.choices.map(m => {
258 //a "choice" is a move
264 ["board" + sizeY]: true
267 width: 100 / this.choices.length + "%",
268 "padding-bottom": 100 / this.choices.length + "%"
276 this.vr.getPpath(m.appear[0].c + m.appear[0].p) +
279 class: { "choice-piece": true },
291 elementArray.unshift(choices);
294 // NOTE: click = mousedown + mouseup
295 if ("ontouchstart" in window) {
298 touchstart: this.mousedown,
299 touchmove: this.mousemove,
300 touchend: this.mouseup
306 mousedown: this.mousedown,
307 mousemove: this.mousemove,
308 mouseup: this.mouseup
312 return h("div", onEvents, elementArray);
315 mousedown: function(e) {
316 // Abort if a piece is already being processed, or target is not a piece.
317 // NOTE: just looking at classList[0] because piece is the first assigned class
318 if (!!this.selectedPiece || e.target.classList[0] != "piece") return;
319 e.preventDefault(); //disable native drag & drop
320 let parent = e.target.parentNode; //the surrounding square
321 // Next few lines to center the piece on mouse cursor
322 let rect = parent.getBoundingClientRect();
324 x: rect.x + rect.width / 2,
325 y: rect.y + rect.width / 2,
328 this.selectedPiece = e.target.cloneNode();
329 let spStyle = this.selectedPiece.style;
330 spStyle.position = "absolute";
332 spStyle.display = "inline-block";
333 spStyle.zIndex = 3000;
334 const startSquare = getSquareFromId(parent.id);
335 this.possibleMoves = [];
336 const color = this.analyze ? this.vr.turn : this.userColor;
337 if (this.vr.canIplay(color, startSquare))
338 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
339 // Next line add moving piece just after current image
340 // (required for Crazyhouse reserve)
341 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
343 mousemove: function(e) {
344 if (!this.selectedPiece) return;
345 // There is an active element: move it around
346 const [offsetX, offsetY] = e.clientX
347 ? [e.clientX, e.clientY] //desktop browser
348 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
349 this.selectedPiece.style.left = offsetX - this.start.x + "px";
350 this.selectedPiece.style.top = offsetY - this.start.y + "px";
352 mouseup: function(e) {
353 if (!this.selectedPiece) return;
354 // There is an active element: obtain the move from start and end squares
355 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
356 const [offsetX, offsetY] = e.clientX
357 ? [e.clientX, e.clientY]
358 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
359 let landing = document.elementFromPoint(offsetX, offsetY);
360 this.selectedPiece.style.zIndex = 3000;
361 // Next condition: classList.contains(piece) fails because of marks
362 while (landing.tagName == "IMG") landing = landing.parentNode;
363 if (this.start.id == landing.id)
364 // One or multi clicks on same piece
366 // OK: process move attempt, landing is a square node
367 let endSquare = getSquareFromId(landing.id);
368 let moves = this.findMatchingMoves(endSquare);
369 this.possibleMoves = [];
370 if (moves.length > 1) this.choices = moves;
371 else if (moves.length == 1) this.play(moves[0]);
372 // Else: impossible move
373 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
374 delete this.selectedPiece;
375 this.selectedPiece = null;
377 findMatchingMoves: function(endSquare) {
378 // Run through moves list and return the matching set (if promotions...)
380 this.possibleMoves.forEach(function(m) {
381 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) moves.push(m);
385 play: function(move) {
386 this.$emit("play-move", move);
392 <style lang="sass" scoped>
402 // NOTE: no variants with reserve of size != 8
415 background-color: rgba(0,0,0,0)
418 background-color: #e6ee9c
420 background-color: skyblue
432 background-color: rgba(0, 204, 102, 0.7) !important
434 background-color: rgba(0, 204, 102, 0.9) !important
437 background-color: rgba(204, 51, 0, 0.7) !important
439 background-color: rgba(204, 51, 0, 0.9) !important
441 .light-square.lichess
442 background-color: #f0d9b5;
444 background-color: #b58863;
446 .light-square.chesscom
447 background-color: #e5e5ca;
448 .dark-square.chesscom
449 background-color: #6f8f57;
451 .light-square.chesstempo
452 background-color: #fdfdfd;
453 .dark-square.chesstempo
454 background-color: #88a0a8;