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 mobileBrowser: ("ontouchstart" in window),
23 possibleMoves: [], //filled after each valid click/dragstart
24 choices: [], //promotion pieces, or checkered captures... (as moves)
25 selectedPiece: null, //moving piece (or clicked piece)
26 start: null, //pixels coordinates + id of starting square (click or drag)
28 settings: store.state.settings
33 // Return empty div of class 'game' to avoid error when setting size
40 const [sizeX, sizeY] = [V.size.x, V.size.y];
41 // Precompute hints squares to facilitate rendering
42 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
43 this.possibleMoves.forEach(m => {
44 hintSquares[m.end.x][m.end.y] = true;
46 // Also precompute in-check squares
47 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
48 this.incheck.forEach(sq => {
49 incheckSq[sq[0]][sq[1]] = true;
52 const lm = this.lastMove;
54 this.settings.highlight &&
55 ["all","highlight"].includes(V.ShowMoves)
57 const orientation = !V.CanFlip ? "w" : this.orientation;
58 // Ensure that squares colors do not change when board is flipped
59 const lightSquareMod = (sizeX + sizeY) % 2;
60 const showPiece = (x, y) => {
62 this.vr.board[x][y] != V.EMPTY &&
63 (!this.vr.enlightened || this.analyze || this.score != "*" ||
64 (!!this.userColor && this.vr.enlightened[this.userColor][x][y]))
67 const inHighlight = (x, y) => {
68 return showLight && !!lm && (
69 (lm.end.x == x && lm.end.y == y) ||
70 (lm.start.x == x && lm.start.y == y));
72 const inShadow = (x, y) => {
76 this.vr.enlightened &&
77 (!this.userColor || !this.vr.enlightened[this.userColor][x][y])
80 // Create board element (+ reserves if needed by variant)
81 let elementArray = [];
90 [...Array(sizeX).keys()].map(i => {
91 const ci = orientation == "w" ? i : sizeX - i - 1;
98 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
100 [...Array(sizeY).keys()].map(j => {
101 const cj = orientation == "w" ? j : sizeY - j - 1;
103 if (showPiece(ci, cj)) {
109 !!this.selectedPiece &&
110 this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj
115 this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) +
121 if (this.settings.hints && hintSquares[ci][cj]) {
128 src: "/images/mark.svg"
133 const lightSquare = (ci + cj) % 2 == lightSquareMod;
139 ["board" + sizeY]: true,
140 "light-square": lightSquare,
141 "dark-square": !lightSquare,
142 [this.settings.bcolor]: true,
143 "in-shadow": inShadow(ci, cj),
144 "highlight-light": inHighlight(ci, cj) && lightSquare,
145 "highlight-dark": inHighlight(ci, cj) && !lightSquare,
146 "incheck-light": showLight && lightSquare && incheckSq[ci][cj],
147 "incheck-dark": showLight && !lightSquare && incheckSq[ci][cj]
150 id: getSquareId({ x: ci, y: cj })
159 if (!!this.vr.reserve) {
160 const playingColor = this.userColor || "w"; //default for an observer
161 const shiftIdx = playingColor == "w" ? 0 : 1;
162 let myReservePiecesArray = [];
163 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
164 const qty = this.vr.reserve[playingColor][V.RESERVE_PIECES[i]];
165 myReservePiecesArray.push(
169 class: { board: true, ["board" + sizeY]: true },
170 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) },
171 style: { opacity: qty > 0 ? 1 : 0.35 }
175 class: { piece: true, reserve: true },
179 this.vr.getReservePpath(i, playingColor) +
183 h("sup", { class: { "reserve-count": true } }, [ qty ])
188 let oppReservePiecesArray = [];
189 const oppCol = V.GetOppCol(playingColor);
190 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
191 const qty = this.vr.reserve[oppCol][V.RESERVE_PIECES[i]];
192 oppReservePiecesArray.push(
196 class: { board: true, ["board" + sizeY]: true },
197 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) },
198 style: { opacity: qty > 0 ? 1 : 0.35 }
202 class: { piece: true, reserve: true },
206 this.vr.getReservePpath(i, oppCol) +
210 h("sup", { class: { "reserve-count": true } }, [ qty ])
215 const myReserveTop = (
216 (playingColor == 'w' && orientation == 'b') ||
217 (playingColor == 'b' && orientation == 'w')
219 // Center reserves, assuming same number of pieces for each side:
220 const nbReservePieces = myReservePiecesArray.length;
221 const marginLeft = ((100 - nbReservePieces * (100 / sizeY)) / 2) + "%";
231 "margin-left": marginLeft
243 myReserveTop ? myReservePiecesArray : oppReservePiecesArray
256 "margin-left": marginLeft
268 myReserveTop ? oppReservePiecesArray : myReservePiecesArray
272 elementArray.push(reserveTop);
274 elementArray.push(gameDiv);
275 if (!!this.vr.reserve) elementArray.push(reserveBottom);
276 const boardElt = document.querySelector(".game");
277 if (this.choices.length > 0 && !!boardElt) {
278 //no choices to show at first drawing
279 const squareWidth = boardElt.offsetWidth / sizeY;
280 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
284 attrs: { id: "choices" },
285 class: { row: true },
287 top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
290 (squareWidth * (sizeY - this.choices.length)) / 2 +
292 width: this.choices.length * squareWidth + "px",
293 height: squareWidth + "px"
296 this.choices.map(m => {
297 // A "choice" is a move
298 const applyMove = (e) => {
305 ? { touchend: applyMove }
306 : { mouseup: applyMove };
312 ["board" + sizeY]: true
315 width: 100 / this.choices.length + "%",
316 "padding-bottom": 100 / this.choices.length + "%"
324 this.vr.getPpath(m.appear[0].c + m.appear[0].p) +
327 class: { "choice-piece": true },
334 elementArray.unshift(choices);
337 // NOTE: click = mousedown + mouseup
338 if (this.mobileBrowser) {
341 touchstart: this.mousedown,
342 touchmove: this.mousemove,
343 touchend: this.mouseup
349 mousedown: this.mousedown,
350 mousemove: this.mousemove,
351 mouseup: this.mouseup
355 return h("div", onEvents, elementArray);
358 mousedown: function(e) {
361 // Start square must contain a piece.
362 // NOTE: classList[0] is enough: 'piece' is the first assigned class
363 if (e.target.classList[0] != "piece") return;
364 let parent = e.target.parentNode; //surrounding square
365 // Show possible moves if current player allowed to play
366 const startSquare = getSquareFromId(parent.id);
367 this.possibleMoves = [];
368 const color = this.analyze ? this.vr.turn : this.userColor;
369 if (this.vr.canIplay(color, startSquare))
370 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
371 // For potential drag'n drop, remember start coordinates
372 // (to center the piece on mouse cursor)
373 let rect = parent.getBoundingClientRect();
375 x: rect.x + rect.width / 2,
376 y: rect.y + rect.width / 2,
379 // Add the moving piece to the board, just after current image
380 this.selectedPiece = e.target.cloneNode();
382 this.selectedPiece.style,
384 position: "absolute",
386 display: "inline-block",
390 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
392 this.processMoveAttempt(e);
395 mousemove: function(e) {
396 if (!this.selectedPiece) return;
398 // There is an active element: move it around
399 const [offsetX, offsetY] =
401 ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY]
402 : [e.clientX, e.clientY];
404 this.selectedPiece.style,
406 left: offsetX - this.start.x + "px",
407 top: offsetY - this.start.y + "px"
411 mouseup: function(e) {
412 if (!this.selectedPiece) return;
414 // Drag'n drop. Selected piece is no longer needed:
415 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
416 delete this.selectedPiece;
417 this.selectedPiece = null;
418 this.processMoveAttempt(e);
420 processMoveAttempt: function(e) {
421 // Obtain the move from start and end squares
422 const [offsetX, offsetY] =
424 ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY]
425 : [e.clientX, e.clientY];
426 let landing = document.elementFromPoint(offsetX, offsetY);
427 // Next condition: classList.contains(piece) fails because of marks
428 while (landing.tagName == "IMG") landing = landing.parentNode;
429 if (this.start.id == landing.id) {
430 if (this.click == landing.id) {
431 // Second click on same square: cancel current move
432 this.possibleMoves = [];
435 } else this.click = landing.id;
439 // OK: process move attempt, landing is a square node
440 let endSquare = getSquareFromId(landing.id);
441 let moves = this.findMatchingMoves(endSquare);
442 this.possibleMoves = [];
443 if (moves.length > 1) this.choices = moves;
444 else if (moves.length == 1) this.play(moves[0]);
445 // else: forbidden move attempt
447 findMatchingMoves: function(endSquare) {
448 // Run through moves list and return the matching set (if promotions...)
450 this.possibleMoves.filter(m => {
451 return (endSquare[0] == m.end.x && endSquare[1] == m.end.y);
455 play: function(move) {
456 this.$emit("play-move", move);
462 <style lang="sass" scoped>
472 // NOTE: no variants with reserve of size != 8
487 background-color: rgba(0,0,0,0)
490 background-color: #e6ee9c
492 background-color: skyblue
504 background-color: rgba(204, 51, 0, 0.7) !important
506 background-color: rgba(204, 51, 0, 0.9) !important
508 .light-square.lichess
509 background-color: #f0d9b5;
511 background-color: #b58863;
513 .light-square.chesscom
514 background-color: #e5e5ca;
515 .dark-square.chesscom
516 background-color: #6f8f57;
518 .light-square.chesstempo
519 background-color: #dfdfdf;
520 .dark-square.chesstempo
521 background-color: #7287b6;
523 // TODO: no predefined highlight colors, but layers. How?
525 .light-square.lichess.highlight-light
526 background-color: #cdd26a !important
527 .dark-square.lichess.highlight-dark
528 background-color: #aaa23a !important
530 .light-square.chesscom.highlight-light
531 background-color: #f7f783 !important
532 .dark-square.chesscom.highlight-dark
533 background-color: #bacb44 !important
535 .light-square.chesstempo.highlight-light
536 background-color: #9f9fff !important
537 .dark-square.chesstempo.highlight-dark
538 background-color: #557fff !important