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 // Create board element (+ reserves if needed by variant)
51 const lm = this.lastMove;
52 const showLight = this.settings.highlight && V.ShowMoves == "all";
61 [...Array(sizeX).keys()].map(i => {
62 let ci = !V.CanFlip || this.orientation == "w" ? i : sizeX - i - 1;
69 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
71 [...Array(sizeY).keys()].map(j => {
72 let cj = !V.CanFlip || this.orientation == "w" ? j : sizeY - j - 1;
75 this.vr.board[ci][cj] != V.EMPTY &&
76 (!this.vr.enlightened || this.analyze || this.score != "*" ||
78 this.vr.enlightened[this.userColor][ci][cj]))
85 !!this.selectedPiece &&
86 this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj
91 this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) +
97 if (this.settings.hints && hintSquares[ci][cj]) {
104 src: "/images/mark.svg"
114 ["board" + sizeY]: true,
115 "light-square": (i + j) % 2 == 0,
116 "dark-square": (i + j) % 2 == 1,
117 [this.settings.bcolor]: true,
121 this.vr.enlightened &&
123 !this.vr.enlightened[this.userColor][ci][cj]),
125 showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
126 incheck: showLight && incheckSq[ci][cj]
129 id: getSquareId({ x: ci, y: cj })
138 let elementArray = [gameDiv];
139 const playingColor = this.userColor || "w"; //default for an observer
140 if (this.vr.reserve) {
141 const shiftIdx = playingColor == "w" ? 0 : 1;
142 let myReservePiecesArray = [];
143 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
144 myReservePiecesArray.push(
148 class: { board: true, ["board" + sizeY]: true },
149 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }
153 class: { piece: true, reserve: true },
157 this.vr.getReservePpath(i, playingColor) +
161 h("sup", { class: { "reserve-count": true } }, [
162 this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
168 let oppReservePiecesArray = [];
169 const oppCol = V.GetOppCol(playingColor);
170 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
171 oppReservePiecesArray.push(
175 class: { board: true, ["board" + sizeY]: true },
176 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }
180 class: { piece: true, reserve: true },
184 this.vr.getReservePpath(i, oppCol) +
188 h("sup", { class: { "reserve-count": true } }, [
189 this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]
209 "reserve-row-1": true
214 h("div", { class: { row: true } }, oppReservePiecesArray)
217 elementArray.push(reserves);
219 const boardElt = document.querySelector(".game");
220 if (this.choices.length > 0 && !!boardElt) {
221 //no choices to show at first drawing
222 const squareWidth = boardElt.offsetWidth / sizeY;
223 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
227 attrs: { id: "choices" },
228 class: { row: true },
230 top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
233 (squareWidth * (sizeY - this.choices.length)) / 2 +
235 width: this.choices.length * squareWidth + "px",
236 height: squareWidth + "px"
239 this.choices.map(m => {
240 //a "choice" is a move
246 ["board" + sizeY]: true
249 width: 100 / this.choices.length + "%",
250 "padding-bottom": 100 / this.choices.length + "%"
258 this.vr.getPpath(m.appear[0].c + m.appear[0].p) +
261 class: { "choice-piece": true },
273 elementArray.unshift(choices);
276 // NOTE: click = mousedown + mouseup
277 if ("ontouchstart" in window) {
280 touchstart: this.mousedown,
281 touchmove: this.mousemove,
282 touchend: this.mouseup
288 mousedown: this.mousedown,
289 mousemove: this.mousemove,
290 mouseup: this.mouseup
294 return h("div", onEvents, elementArray);
297 mousedown: function(e) {
298 // Abort if a piece is already being processed, or target is not a piece.
299 // NOTE: just looking at classList[0] because piece is the first assigned class
300 if (!!this.selectedPiece || e.target.classList[0] != "piece") return;
301 e.preventDefault(); //disable native drag & drop
302 let parent = e.target.parentNode; //the surrounding square
303 // Next few lines to center the piece on mouse cursor
304 let rect = parent.getBoundingClientRect();
306 x: rect.x + rect.width / 2,
307 y: rect.y + rect.width / 2,
310 this.selectedPiece = e.target.cloneNode();
311 let spStyle = this.selectedPiece.style;
312 spStyle.position = "absolute";
314 spStyle.display = "inline-block";
315 spStyle.zIndex = 3000;
316 const startSquare = getSquareFromId(parent.id);
317 this.possibleMoves = [];
318 const color = this.analyze ? this.vr.turn : this.userColor;
319 if (this.vr.canIplay(color, startSquare))
320 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
321 // Next line add moving piece just after current image
322 // (required for Crazyhouse reserve)
323 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
325 mousemove: function(e) {
326 if (!this.selectedPiece) return;
327 // There is an active element: move it around
328 const [offsetX, offsetY] = e.clientX
329 ? [e.clientX, e.clientY] //desktop browser
330 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
331 this.selectedPiece.style.left = offsetX - this.start.x + "px";
332 this.selectedPiece.style.top = offsetY - this.start.y + "px";
334 mouseup: function(e) {
335 if (!this.selectedPiece) return;
336 // There is an active element: obtain the move from start and end squares
337 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
338 const [offsetX, offsetY] = e.clientX
339 ? [e.clientX, e.clientY]
340 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
341 let landing = document.elementFromPoint(offsetX, offsetY);
342 this.selectedPiece.style.zIndex = 3000;
343 // Next condition: classList.contains(piece) fails because of marks
344 while (landing.tagName == "IMG") landing = landing.parentNode;
345 if (this.start.id == landing.id)
346 // One or multi clicks on same piece
348 // OK: process move attempt, landing is a square node
349 let endSquare = getSquareFromId(landing.id);
350 let moves = this.findMatchingMoves(endSquare);
351 this.possibleMoves = [];
352 if (moves.length > 1) this.choices = moves;
353 else if (moves.length == 1) this.play(moves[0]);
354 // Else: impossible move
355 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
356 delete this.selectedPiece;
357 this.selectedPiece = null;
359 findMatchingMoves: function(endSquare) {
360 // Run through moves list and return the matching set (if promotions...)
362 this.possibleMoves.forEach(function(m) {
363 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) moves.push(m);
367 play: function(move) {
368 this.$emit("play-move", move);
374 <style lang="sass" scoped>
384 // NOTE: no variants with reserve of size != 8
397 background-color: rgba(0,0,0,0)
400 background-color: #e6ee9c
402 background-color: skyblue
414 background-color: #00cc66 !important
417 background-color: #cc3300 !important
419 .light-square.lichess
420 background-color: #f0d9b5;
422 background-color: #b58863;
424 .light-square.chesscom
425 background-color: #e5e5ca;
426 .dark-square.chesscom
427 background-color: #6f8f57;
429 .light-square.chesstempo
430 background-color: #fdfdfd;
431 .dark-square.chesstempo
432 background-color: #88a0a8;