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: {}, //pixels coordinates + id of starting square (click or drag)
27 settings: store.state.settings
32 // Return empty div of class 'game' to avoid error when setting size
39 const [sizeX, sizeY] = [V.size.x, V.size.y];
40 // Precompute hints squares to facilitate rendering
41 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
42 this.possibleMoves.forEach(m => {
43 hintSquares[m.end.x][m.end.y] = true;
45 // Also precompute in-check squares
46 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
47 this.incheck.forEach(sq => {
48 incheckSq[sq[0]][sq[1]] = true;
51 const lm = this.lastMove;
53 this.settings.highlight &&
54 ["all","highlight"].includes(V.ShowMoves)
56 const orientation = !V.CanFlip ? "w" : this.orientation;
57 // Ensure that squares colors do not change when board is flipped
58 const lightSquareMod = (sizeX + sizeY) % 2;
59 const showPiece = (x, y) => {
61 this.vr.board[x][y] != V.EMPTY &&
62 (!this.vr.enlightened || this.analyze || this.score != "*" ||
63 (!!this.userColor && this.vr.enlightened[this.userColor][x][y]))
66 const inHighlight = (x, y) => {
67 return showLight && !!lm && (
68 (lm.end.x == x && lm.end.y == y) ||
69 (lm.start.x == x && lm.start.y == y));
71 const inShadow = (x, y) => {
75 this.vr.enlightened &&
76 (!this.userColor || !this.vr.enlightened[this.userColor][x][y])
79 // Create board element (+ reserves if needed by variant)
88 [...Array(sizeX).keys()].map(i => {
89 const ci = orientation == "w" ? i : sizeX - i - 1;
96 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
98 [...Array(sizeY).keys()].map(j => {
99 const cj = orientation == "w" ? j : sizeY - j - 1;
101 if (showPiece(ci, cj)) {
107 !!this.selectedPiece &&
108 this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj
113 this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) +
119 if (this.settings.hints && hintSquares[ci][cj]) {
126 src: "/images/mark.svg"
131 const lightSquare = (ci + cj) % 2 == lightSquareMod;
137 ["board" + sizeY]: true,
138 "light-square": lightSquare,
139 "dark-square": !lightSquare,
140 [this.settings.bcolor]: true,
141 "in-shadow": inShadow(ci, cj),
142 "highlight-light": inHighlight(ci, cj) && lightSquare,
143 "highlight-dark": inHighlight(ci, cj) && !lightSquare,
144 "incheck-light": showLight && lightSquare && incheckSq[ci][cj],
145 "incheck-dark": showLight && !lightSquare && incheckSq[ci][cj]
148 id: getSquareId({ x: ci, y: cj })
157 let elementArray = [gameDiv];
158 const playingColor = this.userColor || "w"; //default for an observer
159 if (this.vr.reserve) {
160 const shiftIdx = playingColor == "w" ? 0 : 1;
161 let myReservePiecesArray = [];
162 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
163 myReservePiecesArray.push(
167 class: { board: true, ["board" + sizeY]: true },
168 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }
172 class: { piece: true, reserve: true },
176 this.vr.getReservePpath(i, playingColor) +
180 h("sup", { class: { "reserve-count": true } }, [
181 this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
187 let oppReservePiecesArray = [];
188 const oppCol = V.GetOppCol(playingColor);
189 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
190 oppReservePiecesArray.push(
194 class: { board: true, ["board" + sizeY]: true },
195 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }
199 class: { piece: true, reserve: true },
203 this.vr.getReservePpath(i, oppCol) +
207 h("sup", { class: { "reserve-count": true } }, [
208 this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]
228 "reserve-row-1": true
233 h("div", { class: { row: true } }, oppReservePiecesArray)
236 elementArray.push(reserves);
238 const boardElt = document.querySelector(".game");
239 if (this.choices.length > 0 && !!boardElt) {
240 //no choices to show at first drawing
241 const squareWidth = boardElt.offsetWidth / sizeY;
242 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
246 attrs: { id: "choices" },
247 class: { row: true },
249 top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
252 (squareWidth * (sizeY - this.choices.length)) / 2 +
254 width: this.choices.length * squareWidth + "px",
255 height: squareWidth + "px"
258 this.choices.map(m => {
259 //a "choice" is a move
265 ["board" + sizeY]: true
268 width: 100 / this.choices.length + "%",
269 "padding-bottom": 100 / this.choices.length + "%"
277 this.vr.getPpath(m.appear[0].c + m.appear[0].p) +
280 class: { "choice-piece": true },
292 elementArray.unshift(choices);
295 // NOTE: click = mousedown + mouseup
296 if (this.mobileBrowser) {
299 touchstart: this.mousedown,
300 touchmove: this.mousemove,
301 touchend: this.mouseup
307 mousedown: this.mousedown,
308 mousemove: this.mousemove,
309 mouseup: this.mouseup
313 return h("div", onEvents, elementArray);
316 mousedown: function(e) {
317 // Abort if a piece is already being processed, or target is not a piece.
318 // NOTE: just looking at classList[0] because piece is the first assigned class
319 if (!!this.selectedPiece || e.target.classList[0] != "piece") return;
320 e.preventDefault(); //disable native drag & drop
321 let parent = e.target.parentNode; //the surrounding square
322 // Next few lines to center the piece on mouse cursor
323 let rect = parent.getBoundingClientRect();
325 x: rect.x + rect.width / 2,
326 y: rect.y + rect.width / 2,
329 this.selectedPiece = e.target.cloneNode();
330 let spStyle = this.selectedPiece.style;
331 spStyle.position = "absolute";
333 spStyle.display = "inline-block";
334 spStyle.zIndex = 3000;
335 const startSquare = getSquareFromId(parent.id);
336 this.possibleMoves = [];
337 const color = this.analyze ? this.vr.turn : this.userColor;
338 if (this.vr.canIplay(color, startSquare))
339 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
340 // Next line add moving piece just after current image
341 // (required for Crazyhouse reserve)
342 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
344 mousemove: function(e) {
345 if (!this.selectedPiece) return;
346 // There is an active element: move it around
347 const [offsetX, offsetY] =
349 ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY]
350 : [e.clientX, e.clientY];
351 this.selectedPiece.style.left = offsetX - this.start.x + "px";
352 this.selectedPiece.style.top = offsetY - this.start.y + "px";
354 mouseup: function(e) {
355 if (!this.selectedPiece) return;
356 // There is an active element: obtain the move from start and end squares
357 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
358 const [offsetX, offsetY] =
360 ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY]
361 : [e.clientX, e.clientY];
362 let landing = document.elementFromPoint(offsetX, offsetY);
363 this.selectedPiece.style.zIndex = 3000;
364 // Next condition: classList.contains(piece) fails because of marks
365 while (landing.tagName == "IMG") landing = landing.parentNode;
366 if (this.start.id == landing.id)
367 // One or multi clicks on same piece
369 // OK: process move attempt, landing is a square node
370 let endSquare = getSquareFromId(landing.id);
371 let moves = this.findMatchingMoves(endSquare);
372 this.possibleMoves = [];
373 if (moves.length > 1) this.choices = moves;
374 else if (moves.length == 1) this.play(moves[0]);
375 // Else: impossible move
376 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
377 delete this.selectedPiece;
378 this.selectedPiece = null;
380 findMatchingMoves: function(endSquare) {
381 // Run through moves list and return the matching set (if promotions...)
383 this.possibleMoves.forEach(function(m) {
384 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) moves.push(m);
388 play: function(move) {
389 this.$emit("play-move", move);
395 <style lang="sass" scoped>
405 // NOTE: no variants with reserve of size != 8
418 background-color: rgba(0,0,0,0)
421 background-color: #e6ee9c
423 background-color: skyblue
435 background-color: rgba(0, 204, 102, 0.7) !important
437 background-color: rgba(0, 204, 102, 0.9) !important
440 background-color: rgba(204, 51, 0, 0.7) !important
442 background-color: rgba(204, 51, 0, 0.9) !important
444 .light-square.lichess
445 background-color: #f0d9b5;
447 background-color: #b58863;
449 .light-square.chesscom
450 background-color: #e5e5ca;
451 .dark-square.chesscom
452 background-color: #6f8f57;
454 .light-square.chesstempo
455 background-color: #fdfdfd;
456 .dark-square.chesstempo
457 background-color: #88a0a8;