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)
47 attrs: { "id": "choices" },
48 'class': { 'row': true },
50 "display": (this.choices.length > 0 ? "block" : "none"),
51 "top": ((sizeY/2)*squareWidth+squareWidth/2) + "px",
52 "width": (this.choices.length * squareWidth) + "px",
53 "height": squareWidth + "px",
56 this.choices.map(m => { //a "choice" is a move
61 ['board'+sizeY]: true,
64 'width': (100/this.choices.length) + "%",
65 'padding-bottom': (100/this.choices.length) + "%",
70 attrs: { "src": '/images/pieces/' +
71 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
72 'class': { 'choice-piece': true },
74 "click": e => { this.play(m); this.choices=[]; },
75 // NOTE: add 'touchstart' event to fix a problem on smartphones
76 "touchstart": e => { this.play(m); this.choices=[]; },
83 // Create board element (+ reserves if needed by variant or mode)
84 const lm = this.lastMove;
85 const showLight = this.settings.highlight && this.vname != "Dark";
94 [...Array(sizeX).keys()].map(i => {
95 let ci = (this.orientation=='w' ? i : sizeX-i-1);
102 style: { 'opacity': this.choices.length>0?"0.5":"1" },
104 [...Array(sizeY).keys()].map(j => {
105 let cj = (this.orientation=='w' ? j : sizeY-j-1);
107 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
108 || this.analyze || (!!this.userColor
109 && this.vr.enlightened[this.userColor][ci][cj])))
117 'ghost': !!this.selectedPiece
118 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
121 src: "/images/pieces/" +
122 V.getPpath(this.vr.board[ci][cj]) + ".svg",
128 if (this.settings.hints && hintSquares[ci][cj])
138 src: "/images/mark.svg",
149 ['board'+sizeY]: true,
150 'light-square': (i+j)%2==0,
151 'dark-square': (i+j)%2==1,
152 [this.settings.bcolor]: true,
153 'in-shadow': this.vname=="Dark" && !this.analyze
155 || !this.vr.enlightened[this.userColor][ci][cj]),
156 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
157 'incheck': showLight && incheckSq[ci][cj],
160 id: getSquareId({x:ci,y:cj}),
169 const playingColor = this.userColor || "w"; //default for an observer
170 let elementArray = [choices, gameDiv];
171 if (!!this.vr.reserve)
173 const shiftIdx = (playingColor=="w" ? 0 : 1);
174 let myReservePiecesArray = [];
175 for (let i=0; i<V.RESERVE_PIECES.length; i++)
177 myReservePiecesArray.push(h('div',
179 'class': {'board':true, ['board'+sizeY]:true},
180 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
185 'class': {"piece":true, "reserve":true},
187 "src": "/images/pieces/" +
188 this.vr.getReservePpath(playingColor,i) + ".svg",
192 {"class": { "reserve-count": true } },
193 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
197 let oppReservePiecesArray = [];
198 const oppCol = V.GetOppCol(playingColor);
199 for (let i=0; i<V.RESERVE_PIECES.length; i++)
201 oppReservePiecesArray.push(h('div',
203 'class': {'board':true, ['board'+sizeY]:true},
204 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
209 'class': {"piece":true, "reserve":true},
211 "src": "/images/pieces/" +
212 this.vr.getReservePpath(oppCol,i) + ".svg",
216 {"class": { "reserve-count": true } },
217 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
221 let reserves = h('div',
233 "reserve-row-1": true,
239 { 'class': { 'row': true }},
240 oppReservePiecesArray
244 elementArray.push(reserves);
249 // NOTE: click = mousedown + mouseup
251 mousedown: this.mousedown,
252 mousemove: this.mousemove,
253 mouseup: this.mouseup,
254 touchstart: this.mousedown,
255 touchmove: this.mousemove,
256 touchend: this.mouseup,
263 mousedown: function(e) {
264 e = e || window.event;
267 while (!ingame && elem !== null)
269 if (elem.classList.contains("game"))
274 elem = elem.parentElement;
276 if (!ingame) //let default behavior (click on button...)
278 e.preventDefault(); //disable native drag & drop
279 if (!this.selectedPiece && e.target.classList.contains("piece"))
281 // Next few lines to center the piece on mouse cursor
282 let rect = e.target.parentNode.getBoundingClientRect();
284 x: rect.x + rect.width/2,
285 y: rect.y + rect.width/2,
286 id: e.target.parentNode.id
288 this.selectedPiece = e.target.cloneNode();
289 this.selectedPiece.style.position = "absolute";
290 this.selectedPiece.style.top = 0;
291 this.selectedPiece.style.display = "inline-block";
292 this.selectedPiece.style.zIndex = 3000;
293 const startSquare = getSquareFromId(e.target.parentNode.id);
294 this.possibleMoves = [];
295 const color = (this.analyze ? this.vr.turn : this.userColor);
296 if (this.vr.canIplay(color,startSquare))
297 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
298 // Next line add moving piece just after current image
299 // (required for Crazyhouse reserve)
300 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
303 mousemove: function(e) {
304 if (!this.selectedPiece)
306 e = e || window.event;
307 // If there is an active element, move it around
308 if (!!this.selectedPiece)
310 const [offsetX,offsetY] = !!e.clientX
311 ? [e.clientX,e.clientY] //desktop browser
312 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
313 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
314 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
317 mouseup: function(e) {
318 if (!this.selectedPiece)
320 e = e || window.event;
321 // Read drop target (or parentElement, parentNode... if type == "img")
322 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
323 const [offsetX,offsetY] = !!e.clientX
324 ? [e.clientX,e.clientY]
325 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
326 let landing = document.elementFromPoint(offsetX, offsetY);
327 this.selectedPiece.style.zIndex = 3000;
328 // Next condition: classList.contains(piece) fails because of marks
329 while (landing.tagName == "IMG")
330 landing = landing.parentNode;
331 if (this.start.id == landing.id)
333 // A click: selectedPiece and possibleMoves are already filled
336 // OK: process move attempt
337 let endSquare = getSquareFromId(landing.id);
338 let moves = this.findMatchingMoves(endSquare);
339 this.possibleMoves = [];
340 if (moves.length > 1)
341 this.choices = moves;
342 else if (moves.length==1)
344 // Else: impossible move
345 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
346 delete this.selectedPiece;
347 this.selectedPiece = null;
349 findMatchingMoves: function(endSquare) {
350 // Run through moves list and return the matching set (if promotions...)
352 this.possibleMoves.forEach(function(m) {
353 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
358 play: function(move) {
359 this.$emit('play-move', move);
365 <style lang="sass" scoped>
375 // NOTE: no variants with reserve of size != 8
380 display: inline-block
385 padding-bottom: 12.5%
402 margin: 0 auto 0 auto
406 background-color: rgba(0,0,0,0)
409 background-color: #e6ee9c
411 background-color: skyblue
420 img.piece, img.mark-square
439 background-color: #00cc66 !important
442 filter: brightness(50%)
445 background-color: #cc3300 !important
447 .light-square.lichess
448 background-color: #f0d9b5;
450 background-color: #b58863;
452 .light-square.chesscom
453 background-color: #e5e5ca;
454 .dark-square.chesscom
455 background-color: #6f8f57;
457 .light-square.chesstempo
458 background-color: #fdfdfd;
459 .dark-square.chesstempo
460 background-color: #88a0a8;