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;
51 const showLight = this.settings.highlight && V.ShowMoves == "all";
52 const orientation = !V.CanFlip ? "w" : this.orientation;
53 // Ensure that squares colors do not change when board is flipped
54 const lightSquareMod = (sizeX + sizeY) % 2;
55 const showPiece = (x, y) => {
57 this.vr.board[x][y] != V.EMPTY &&
58 (!this.vr.enlightened || this.analyze || this.score != "*" ||
59 (!!this.userColor && this.vr.enlightened[this.userColor][x][y]))
62 const inHighlight = (x, y) => {
63 return showLight && !!lm && (
64 (lm.end.x == x && lm.end.y == y) ||
65 (lm.start.x == x && lm.start.y == y));
67 const inShadow = (x, y) => {
71 this.vr.enlightened &&
72 (!this.userColor || !this.vr.enlightened[this.userColor][x][y])
75 // Create board element (+ reserves if needed by variant)
84 [...Array(sizeX).keys()].map(i => {
85 const ci = orientation == "w" ? i : sizeX - i - 1;
92 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
94 [...Array(sizeY).keys()].map(j => {
95 const cj = orientation == "w" ? j : sizeY - j - 1;
97 if (showPiece(ci, cj)) {
103 !!this.selectedPiece &&
104 this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj
109 this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) +
115 if (this.settings.hints && hintSquares[ci][cj]) {
122 src: "/images/mark.svg"
127 const lightSquare = (ci + cj) % 2 == lightSquareMod;
133 ["board" + sizeY]: true,
134 "light-square": lightSquare,
135 "dark-square": !lightSquare,
136 [this.settings.bcolor]: true,
137 "in-shadow": inShadow(ci, cj),
138 "highlight-light": inHighlight(ci, cj) && lightSquare,
139 "highlight-dark": inHighlight(ci, cj) && !lightSquare,
140 "incheck-light": showLight && lightSquare && incheckSq[ci][cj],
141 "incheck-dark": showLight && !lightSquare && incheckSq[ci][cj]
144 id: getSquareId({ x: ci, y: cj })
153 let elementArray = [gameDiv];
154 const playingColor = this.userColor || "w"; //default for an observer
155 if (this.vr.reserve) {
156 const shiftIdx = playingColor == "w" ? 0 : 1;
157 let myReservePiecesArray = [];
158 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
159 myReservePiecesArray.push(
163 class: { board: true, ["board" + sizeY]: true },
164 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }
168 class: { piece: true, reserve: true },
172 this.vr.getReservePpath(i, playingColor) +
176 h("sup", { class: { "reserve-count": true } }, [
177 this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
183 let oppReservePiecesArray = [];
184 const oppCol = V.GetOppCol(playingColor);
185 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
186 oppReservePiecesArray.push(
190 class: { board: true, ["board" + sizeY]: true },
191 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }
195 class: { piece: true, reserve: true },
199 this.vr.getReservePpath(i, oppCol) +
203 h("sup", { class: { "reserve-count": true } }, [
204 this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]
224 "reserve-row-1": true
229 h("div", { class: { row: true } }, oppReservePiecesArray)
232 elementArray.push(reserves);
234 const boardElt = document.querySelector(".game");
235 if (this.choices.length > 0 && !!boardElt) {
236 //no choices to show at first drawing
237 const squareWidth = boardElt.offsetWidth / sizeY;
238 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
242 attrs: { id: "choices" },
243 class: { row: true },
245 top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
248 (squareWidth * (sizeY - this.choices.length)) / 2 +
250 width: this.choices.length * squareWidth + "px",
251 height: squareWidth + "px"
254 this.choices.map(m => {
255 //a "choice" is a move
261 ["board" + sizeY]: true
264 width: 100 / this.choices.length + "%",
265 "padding-bottom": 100 / this.choices.length + "%"
273 this.vr.getPpath(m.appear[0].c + m.appear[0].p) +
276 class: { "choice-piece": true },
288 elementArray.unshift(choices);
291 // NOTE: click = mousedown + mouseup
292 if ("ontouchstart" in window) {
295 touchstart: this.mousedown,
296 touchmove: this.mousemove,
297 touchend: this.mouseup
303 mousedown: this.mousedown,
304 mousemove: this.mousemove,
305 mouseup: this.mouseup
309 return h("div", onEvents, elementArray);
312 mousedown: function(e) {
313 // Abort if a piece is already being processed, or target is not a piece.
314 // NOTE: just looking at classList[0] because piece is the first assigned class
315 if (!!this.selectedPiece || e.target.classList[0] != "piece") return;
316 e.preventDefault(); //disable native drag & drop
317 let parent = e.target.parentNode; //the surrounding square
318 // Next few lines to center the piece on mouse cursor
319 let rect = parent.getBoundingClientRect();
321 x: rect.x + rect.width / 2,
322 y: rect.y + rect.width / 2,
325 this.selectedPiece = e.target.cloneNode();
326 let spStyle = this.selectedPiece.style;
327 spStyle.position = "absolute";
329 spStyle.display = "inline-block";
330 spStyle.zIndex = 3000;
331 const startSquare = getSquareFromId(parent.id);
332 this.possibleMoves = [];
333 const color = this.analyze ? this.vr.turn : this.userColor;
334 if (this.vr.canIplay(color, startSquare))
335 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
336 // Next line add moving piece just after current image
337 // (required for Crazyhouse reserve)
338 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
340 mousemove: function(e) {
341 if (!this.selectedPiece) return;
342 // There is an active element: move it around
343 const [offsetX, offsetY] = e.clientX
344 ? [e.clientX, e.clientY] //desktop browser
345 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
346 this.selectedPiece.style.left = offsetX - this.start.x + "px";
347 this.selectedPiece.style.top = offsetY - this.start.y + "px";
349 mouseup: function(e) {
350 if (!this.selectedPiece) return;
351 // There is an active element: obtain the move from start and end squares
352 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
353 const [offsetX, offsetY] = e.clientX
354 ? [e.clientX, e.clientY]
355 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
356 let landing = document.elementFromPoint(offsetX, offsetY);
357 this.selectedPiece.style.zIndex = 3000;
358 // Next condition: classList.contains(piece) fails because of marks
359 while (landing.tagName == "IMG") landing = landing.parentNode;
360 if (this.start.id == landing.id)
361 // One or multi clicks on same piece
363 // OK: process move attempt, landing is a square node
364 let endSquare = getSquareFromId(landing.id);
365 let moves = this.findMatchingMoves(endSquare);
366 this.possibleMoves = [];
367 if (moves.length > 1) this.choices = moves;
368 else if (moves.length == 1) this.play(moves[0]);
369 // Else: impossible move
370 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
371 delete this.selectedPiece;
372 this.selectedPiece = null;
374 findMatchingMoves: function(endSquare) {
375 // Run through moves list and return the matching set (if promotions...)
377 this.possibleMoves.forEach(function(m) {
378 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) moves.push(m);
382 play: function(move) {
383 this.$emit("play-move", move);
389 <style lang="sass" scoped>
399 // NOTE: no variants with reserve of size != 8
412 background-color: rgba(0,0,0,0)
415 background-color: #e6ee9c
417 background-color: skyblue
429 background-color: rgba(0, 204, 102, 0.7) !important
431 background-color: rgba(0, 204, 102, 0.9) !important
434 background-color: rgba(204, 51, 0, 0.7) !important
436 background-color: rgba(204, 51, 0, 0.9) !important
438 .light-square.lichess
439 background-color: #f0d9b5;
441 background-color: #b58863;
443 .light-square.chesscom
444 background-color: #e5e5ca;
445 .dark-square.chesscom
446 background-color: #6f8f57;
448 .light-square.chesstempo
449 background-color: #fdfdfd;
450 .dark-square.chesstempo
451 background-color: #88a0a8;