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
10 props: ["vr","lastMove","analyze","incheck","orientation","userColor","vname"],
13 possibleMoves: [], //filled after each valid click/dragstart
14 choices: [], //promotion pieces, or checkered captures... (as moves)
15 selectedPiece: null, //moving piece (or clicked piece)
16 start: {}, //pixels coordinates + id of starting square (click or drag)
17 settings: store.state.settings,
23 // Return empty div of class 'game' to avoid error when setting size
31 const [sizeX,sizeY] = [V.size.x,V.size.y];
32 // Precompute hints squares to facilitate rendering
33 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
34 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
35 // Also precompute in-check squares
36 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
37 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
39 let boardElt = document.querySelector(".game");
40 const squareWidth = (!!boardElt
41 ? boardElt.offsetWidth / sizeY
42 : 40); //arbitrary value (not relevant)
43 const offset = (!!boardElt
44 ? [boardElt.offsetTop, boardElt.offsetLeft]
49 attrs: { "id": "choices" },
50 'class': { 'row': true },
52 "display": (this.choices.length > 0 ? "block" : "none"),
53 "top": (offset[0] + (sizeY/2)*squareWidth-squareWidth/2) + "px",
54 "left": (offset[1] + squareWidth*(sizeY - this.choices.length)/2) + "px",
55 "width": (this.choices.length * squareWidth) + "px",
56 "height": squareWidth + "px",
59 this.choices.map(m => { //a "choice" is a move
64 ['board'+sizeY]: true,
67 'width': (100/this.choices.length) + "%",
68 'padding-bottom': (100/this.choices.length) + "%",
73 attrs: { "src": '/images/pieces/' +
74 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
75 'class': { 'choice-piece': true },
77 "click": e => { this.play(m); this.choices=[]; },
84 // Create board element (+ reserves if needed by variant or mode)
85 const lm = this.lastMove;
86 const showLight = this.settings.highlight && this.vname != "Dark";
95 [...Array(sizeX).keys()].map(i => {
96 let ci = (this.orientation=='w' ? i : sizeX-i-1);
103 style: { 'opacity': this.choices.length>0?"0.5":"1" },
105 [...Array(sizeY).keys()].map(j => {
106 let cj = (this.orientation=='w' ? j : sizeY-j-1);
108 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
109 || this.analyze || (!!this.userColor
110 && this.vr.enlightened[this.userColor][ci][cj])))
118 'ghost': !!this.selectedPiece
119 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
122 src: "/images/pieces/" +
123 V.getPpath(this.vr.board[ci][cj]) + ".svg",
129 if (this.settings.hints && hintSquares[ci][cj])
139 src: "/images/mark.svg",
150 ['board'+sizeY]: true,
151 'light-square': (i+j)%2==0,
152 'dark-square': (i+j)%2==1,
153 [this.settings.bcolor]: true,
154 'in-shadow': this.vname=="Dark" && !this.analyze
156 || !this.vr.enlightened[this.userColor][ci][cj]),
157 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
158 'incheck': showLight && incheckSq[ci][cj],
161 id: getSquareId({x:ci,y:cj}),
170 const playingColor = this.userColor || "w"; //default for an observer
171 let elementArray = [choices, gameDiv];
172 if (!!this.vr.reserve)
174 const shiftIdx = (playingColor=="w" ? 0 : 1);
175 let myReservePiecesArray = [];
176 for (let i=0; i<V.RESERVE_PIECES.length; i++)
178 myReservePiecesArray.push(h('div',
180 'class': {'board':true, ['board'+sizeY]:true},
181 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
186 'class': {"piece":true, "reserve":true},
188 "src": "/images/pieces/" +
189 this.vr.getReservePpath(playingColor,i) + ".svg",
193 {"class": { "reserve-count": true } },
194 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
198 let oppReservePiecesArray = [];
199 const oppCol = V.GetOppCol(playingColor);
200 for (let i=0; i<V.RESERVE_PIECES.length; i++)
202 oppReservePiecesArray.push(h('div',
204 'class': {'board':true, ['board'+sizeY]:true},
205 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
210 'class': {"piece":true, "reserve":true},
212 "src": "/images/pieces/" +
213 this.vr.getReservePpath(oppCol,i) + ".svg",
217 {"class": { "reserve-count": true } },
218 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
222 let reserves = h('div',
234 "reserve-row-1": true,
240 { 'class': { 'row': true }},
241 oppReservePiecesArray
245 elementArray.push(reserves);
248 // NOTE: click = mousedown + mouseup
249 if ('ontouchstart' in window)
253 touchstart: this.mousedown,
254 touchmove: this.mousemove,
255 touchend: this.mouseup,
263 mousedown: this.mousedown,
264 mousemove: this.mousemove,
265 mouseup: this.mouseup,
276 mousedown: function(e) {
277 e = e || window.event;
280 while (!ingame && elem !== null)
282 if (elem.classList.contains("game"))
287 elem = elem.parentElement;
289 if (!ingame) //let default behavior (click on button...)
291 e.preventDefault(); //disable native drag & drop
292 if (!this.selectedPiece && e.target.classList.contains("piece"))
294 let parent = e.target.parentNode;
295 // Next few lines to center the piece on mouse cursor
296 let rect = parent.getBoundingClientRect();
298 x: rect.x + rect.width/2,
299 y: rect.y + rect.width/2,
302 this.selectedPiece = e.target.cloneNode();
303 let spStyle = this.selectedPiece.style
304 spStyle.position = "absolute";
306 spStyle.display = "inline-block";
307 spStyle.zIndex = 3000;
308 const startSquare = getSquareFromId(parent.id);
309 this.possibleMoves = [];
310 const color = (this.analyze ? this.vr.turn : this.userColor);
311 if (this.vr.canIplay(color,startSquare))
312 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
313 // Next line add moving piece just after current image
314 // (required for Crazyhouse reserve)
315 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
318 mousemove: function(e) {
319 if (!this.selectedPiece)
321 e = e || window.event;
322 // If there is an active element, move it around
323 if (!!this.selectedPiece)
325 const [offsetX,offsetY] = !!e.clientX
326 ? [e.clientX,e.clientY] //desktop browser
327 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
328 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
329 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
332 mouseup: function(e) {
333 if (!this.selectedPiece)
335 e = e || window.event;
336 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
337 const [offsetX,offsetY] = !!e.clientX
338 ? [e.clientX,e.clientY]
339 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
340 let landing = document.elementFromPoint(offsetX, offsetY);
341 this.selectedPiece.style.zIndex = 3000;
342 // Next condition: classList.contains(piece) fails because of marks
343 while (landing.tagName == "IMG")
344 landing = landing.parentNode;
345 if (this.start.id == landing.id)
347 // A click: selectedPiece and possibleMoves are already filled
350 // OK: process move attempt, landing is a square node
351 let endSquare = getSquareFromId(landing.id);
352 let moves = this.findMatchingMoves(endSquare);
353 this.possibleMoves = [];
354 if (moves.length > 1)
355 this.choices = moves;
356 else if (moves.length==1)
358 // Else: impossible move
359 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
360 delete this.selectedPiece;
361 this.selectedPiece = null;
363 findMatchingMoves: function(endSquare) {
364 // Run through moves list and return the matching set (if promotions...)
366 this.possibleMoves.forEach(function(m) {
367 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
372 play: function(move) {
373 this.$emit('play-move', move);
379 <style lang="sass" scoped>
389 // NOTE: no variants with reserve of size != 8
402 background-color: rgba(0,0,0,0)
405 background-color: #e6ee9c
407 background-color: skyblue
419 background-color: #00cc66 !important
422 background-color: #cc3300 !important
424 .light-square.lichess
425 background-color: #f0d9b5;
427 background-color: #b58863;
429 .light-square.chesscom
430 background-color: #e5e5ca;
431 .dark-square.chesscom
432 background-color: #6f8f57;
434 .light-square.chesstempo
435 background-color: #fdfdfd;
436 .dark-square.chesstempo
437 background-color: #88a0a8;