2 import { getSquareId, getSquareFromId } from "@/utils/squareId";
3 import { ArrayFun } from "@/utils/array";
4 import { store } from "@/store";
8 // Last move cannot be guessed from here, and is required to highlight squares
9 // vr: object to check moves, print board...
10 // userColor is left undefined for an external observer
11 props: ["vr","lastMove","analyze","incheck","orientation","userColor","vname"],
14 possibleMoves: [], //filled after each valid click/dragstart
15 choices: [], //promotion pieces, or checkered captures... (as moves)
16 selectedPiece: null, //moving piece (or clicked piece)
17 start: {}, //pixels coordinates + id of starting square (click or drag)
18 settings: store.state.settings,
24 // Return empty div of class 'game' to avoid error when setting size
32 const [sizeX,sizeY] = [V.size.x,V.size.y];
33 // Precompute hints squares to facilitate rendering
34 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
35 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
36 // Also precompute in-check squares
37 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
38 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
40 let boardElt = document.querySelector(".game");
41 const squareWidth = (!!boardElt
42 ? boardElt.offsetWidth / sizeY
43 : 40); //arbitrary value (not relevant)
44 const offset = (!!boardElt
45 ? [boardElt.offsetTop, boardElt.offsetLeft]
50 attrs: { "id": "choices" },
51 'class': { 'row': true },
53 "display": (this.choices.length > 0 ? "block" : "none"),
54 "top": (offset[0] + (sizeY/2)*squareWidth-squareWidth/2) + "px",
55 "left": (offset[1] + squareWidth*(sizeY - this.choices.length)/2) + "px",
56 "width": (this.choices.length * squareWidth) + "px",
57 "height": squareWidth + "px",
60 this.choices.map(m => { //a "choice" is a move
65 ['board'+sizeY]: true,
68 'width': (100/this.choices.length) + "%",
69 'padding-bottom': (100/this.choices.length) + "%",
74 attrs: { "src": '/images/pieces/' +
75 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
76 'class': { 'choice-piece': true },
78 "click": e => { this.play(m); this.choices=[]; },
79 // NOTE: add 'touchstart' event to fix a problem on smartphones
80 "touchstart": e => { this.play(m); this.choices=[]; },
87 // Create board element (+ reserves if needed by variant or mode)
88 const lm = this.lastMove;
89 const showLight = this.settings.highlight && this.vname != "Dark";
98 [...Array(sizeX).keys()].map(i => {
99 let ci = (this.orientation=='w' ? i : sizeX-i-1);
106 style: { 'opacity': this.choices.length>0?"0.5":"1" },
108 [...Array(sizeY).keys()].map(j => {
109 let cj = (this.orientation=='w' ? j : sizeY-j-1);
111 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
112 || this.analyze || (!!this.userColor
113 && this.vr.enlightened[this.userColor][ci][cj])))
121 'ghost': !!this.selectedPiece
122 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
125 src: "/images/pieces/" +
126 V.getPpath(this.vr.board[ci][cj]) + ".svg",
132 if (this.settings.hints && hintSquares[ci][cj])
142 src: "/images/mark.svg",
153 ['board'+sizeY]: true,
154 'light-square': (i+j)%2==0,
155 'dark-square': (i+j)%2==1,
156 [this.settings.bcolor]: true,
157 'in-shadow': this.vname=="Dark" && !this.analyze
159 || !this.vr.enlightened[this.userColor][ci][cj]),
160 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
161 'incheck': showLight && incheckSq[ci][cj],
164 id: getSquareId({x:ci,y:cj}),
173 const playingColor = this.userColor || "w"; //default for an observer
174 let elementArray = [choices, gameDiv];
175 if (!!this.vr.reserve)
177 const shiftIdx = (playingColor=="w" ? 0 : 1);
178 let myReservePiecesArray = [];
179 for (let i=0; i<V.RESERVE_PIECES.length; i++)
181 myReservePiecesArray.push(h('div',
183 'class': {'board':true, ['board'+sizeY]:true},
184 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
189 'class': {"piece":true, "reserve":true},
191 "src": "/images/pieces/" +
192 this.vr.getReservePpath(playingColor,i) + ".svg",
196 {"class": { "reserve-count": true } },
197 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
201 let oppReservePiecesArray = [];
202 const oppCol = V.GetOppCol(playingColor);
203 for (let i=0; i<V.RESERVE_PIECES.length; i++)
205 oppReservePiecesArray.push(h('div',
207 'class': {'board':true, ['board'+sizeY]:true},
208 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
213 'class': {"piece":true, "reserve":true},
215 "src": "/images/pieces/" +
216 this.vr.getReservePpath(oppCol,i) + ".svg",
220 {"class": { "reserve-count": true } },
221 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
225 let reserves = h('div',
237 "reserve-row-1": true,
243 { 'class': { 'row': true }},
244 oppReservePiecesArray
248 elementArray.push(reserves);
253 // NOTE: click = mousedown + mouseup
255 mousedown: this.mousedown,
256 mousemove: this.mousemove,
257 mouseup: this.mouseup,
258 touchstart: this.mousedown,
259 touchmove: this.mousemove,
260 touchend: this.mouseup,
267 mousedown: function(e) {
268 e = e || window.event;
271 while (!ingame && elem !== null)
273 if (elem.classList.contains("game"))
278 elem = elem.parentElement;
280 if (!ingame) //let default behavior (click on button...)
282 e.preventDefault(); //disable native drag & drop
283 if (!this.selectedPiece && e.target.classList.contains("piece"))
285 // Next few lines to center the piece on mouse cursor
286 let rect = e.target.parentNode.getBoundingClientRect();
288 x: rect.x + rect.width/2,
289 y: rect.y + rect.width/2,
290 id: e.target.parentNode.id
292 this.selectedPiece = e.target.cloneNode();
293 this.selectedPiece.style.position = "absolute";
294 this.selectedPiece.style.top = 0;
295 this.selectedPiece.style.display = "inline-block";
296 this.selectedPiece.style.zIndex = 3000;
297 const startSquare = getSquareFromId(e.target.parentNode.id);
298 this.possibleMoves = [];
299 const color = (this.analyze ? this.vr.turn : this.userColor);
300 if (this.vr.canIplay(color,startSquare))
301 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
302 // Next line add moving piece just after current image
303 // (required for Crazyhouse reserve)
304 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
307 mousemove: function(e) {
308 if (!this.selectedPiece)
310 e = e || window.event;
311 // If there is an active element, move it around
312 if (!!this.selectedPiece)
314 const [offsetX,offsetY] = !!e.clientX
315 ? [e.clientX,e.clientY] //desktop browser
316 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
317 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
318 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
321 mouseup: function(e) {
322 if (!this.selectedPiece)
324 e = e || window.event;
325 // Read drop target (or parentElement, parentNode... if type == "img")
326 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
327 const [offsetX,offsetY] = !!e.clientX
328 ? [e.clientX,e.clientY]
329 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
330 let landing = document.elementFromPoint(offsetX, offsetY);
331 this.selectedPiece.style.zIndex = 3000;
332 // Next condition: classList.contains(piece) fails because of marks
333 while (landing.tagName == "IMG")
334 landing = landing.parentNode;
335 if (this.start.id == landing.id)
337 // A click: selectedPiece and possibleMoves are already filled
340 // OK: process move attempt
341 let endSquare = getSquareFromId(landing.id);
342 let moves = this.findMatchingMoves(endSquare);
343 this.possibleMoves = [];
344 if (moves.length > 1)
345 this.choices = moves;
346 else if (moves.length==1)
348 // Else: impossible move
349 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
350 delete this.selectedPiece;
351 this.selectedPiece = null;
353 findMatchingMoves: function(endSquare) {
354 // Run through moves list and return the matching set (if promotions...)
356 this.possibleMoves.forEach(function(m) {
357 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
362 play: function(move) {
363 this.$emit('play-move', move);
369 <style lang="sass" scoped>
379 // NOTE: no variants with reserve of size != 8
384 display: inline-block
389 padding-bottom: 12.5%
410 background-color: rgba(0,0,0,0)
413 background-color: #e6ee9c
415 background-color: skyblue
424 img.piece, img.mark-square
443 background-color: #00cc66 !important
446 filter: brightness(50%)
449 background-color: #cc3300 !important
451 .light-square.lichess
452 background-color: #f0d9b5;
454 background-color: #b58863;
456 .light-square.chesscom
457 background-color: #e5e5ca;
458 .dark-square.chesscom
459 background-color: #6f8f57;
461 .light-square.chesstempo
462 background-color: #fdfdfd;
463 .dark-square.chesstempo
464 background-color: #88a0a8;