2 // This can work for squared boards (2 or 4 players), with some adaptations (TODO)
3 // TODO: for 3 players, write a "board3.js"
5 // TODO: current clicked square + moving square as parameters, + highlight
7 import { getSquareId, getSquareFromId } from "@/utils/squareId";
8 import { ArrayFun } from "@/utils/array";
12 // Last move cannot be guessed from here, and is required to highlight squares
13 // vr: object to check moves, print board...
14 // userColor: for mode HH or HC
15 props: ["vr","lastMove","analyze","orientation","userColor","vname"],
18 hints: (!localStorage["hints"] ? true : localStorage["hints"] === "1"),
19 bcolor: localStorage["bcolor"] || "lichess", //lichess, chesscom or chesstempo
20 possibleMoves: [], //filled after each valid click/dragstart
21 choices: [], //promotion pieces, or checkered captures... (as moves)
22 selectedPiece: null, //moving piece (or clicked piece)
24 start: {}, //pixels coordinates + id of starting square (click or drag)
30 const [sizeX,sizeY] = [V.size.x,V.size.y];
31 // Precompute hints squares to facilitate rendering
32 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
33 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
34 // Also precompute in-check squares
35 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
36 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
37 const squareWidth = 40; //TODO: compute this
41 attrs: { "id": "choices" },
42 'class': { 'row': true },
44 "display": this.choices.length>0?"block":"none",
45 "top": "-" + ((sizeY/2)*squareWidth+squareWidth/2) + "px",
46 "width": (this.choices.length * squareWidth) + "px",
47 "height": squareWidth + "px",
50 this.choices.map(m => { //a "choice" is a move
55 ['board'+sizeY]: true,
58 'width': (100/this.choices.length) + "%",
59 'padding-bottom': (100/this.choices.length) + "%",
64 attrs: { "src": '/images/pieces/' +
65 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
66 'class': { 'choice-piece': true },
68 "click": e => { this.play(m); this.choices=[]; },
69 // NOTE: add 'touchstart' event to fix a problem on smartphones
70 "touchstart": e => { this.play(m); this.choices=[]; },
77 // Create board element (+ reserves if needed by variant or mode)
78 const lm = this.lastMove;
79 const showLight = this.hints && this.vname != "Dark";
88 [...Array(sizeX).keys()].map(i => {
89 let ci = (this.orientation=='w' ? i : sizeX-i-1);
96 style: { 'opacity': this.choices.length>0?"0.5":"1" },
98 [...Array(sizeY).keys()].map(j => {
99 let cj = (this.orientation=='w' ? j : sizeY-j-1);
101 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
102 || this.gameOver || this.analyze
103 || this.vr.enlightened[this.userColor][ci][cj]))
111 'ghost': !!this.selectedPiece
112 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
115 src: "/images/pieces/" +
116 V.getPpath(this.vr.board[ci][cj]) + ".svg",
122 if (this.hints && hintSquares[ci][cj])
132 src: "/images/mark.svg",
143 ['board'+sizeY]: true,
144 'light-square': (i+j)%2==0,
145 'dark-square': (i+j)%2==1,
147 'in-shadow': this.vname=="Dark" && !this.gameOver
148 && !this.analyze && !this.vr.enlightened[this.userColor][ci][cj],
149 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
150 'incheck': showLight && incheckSq[ci][cj],
153 id: getSquareId({x:ci,y:cj}),
162 let elementArray = [choices, gameDiv];
163 if (!!this.vr.reserve)
165 const shiftIdx = (this.userColor=="w" ? 0 : 1);
166 let myReservePiecesArray = [];
167 for (let i=0; i<V.RESERVE_PIECES.length; i++)
169 myReservePiecesArray.push(h('div',
171 'class': {'board':true, ['board'+sizeY]:true},
172 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
177 'class': {"piece":true, "reserve":true},
179 "src": "/images/pieces/" +
180 this.vr.getReservePpath(this.userColor,i) + ".svg",
184 {"class": { "reserve-count": true } },
185 [ this.vr.reserve[this.userColor][V.RESERVE_PIECES[i]] ]
189 let oppReservePiecesArray = [];
190 const oppCol = V.GetOppCol(this.userColor);
191 for (let i=0; i<V.RESERVE_PIECES.length; i++)
193 oppReservePiecesArray.push(h('div',
195 'class': {'board':true, ['board'+sizeY]:true},
196 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
201 'class': {"piece":true, "reserve":true},
203 "src": "/images/pieces/" +
204 this.vr.getReservePpath(oppCol,i) + ".svg",
208 {"class": { "reserve-count": true } },
209 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
213 let reserves = h('div',
225 "reserve-row-1": true,
231 { 'class': { 'row': true }},
232 oppReservePiecesArray
236 elementArray.push(reserves);
244 "col-md-offset-1": true,
246 "col-lg-offset-2": true,
248 // NOTE: click = mousedown + mouseup
250 mousedown: this.mousedown,
251 mousemove: this.mousemove,
252 mouseup: this.mouseup,
253 touchstart: this.mousedown,
254 touchmove: this.mousemove,
255 touchend: this.mouseup,
262 mousedown: function(e) {
263 e = e || window.event;
266 while (!ingame && elem !== null)
268 if (elem.classList.contains("game"))
273 elem = elem.parentElement;
275 if (!ingame) //let default behavior (click on button...)
277 e.preventDefault(); //disable native drag & drop
278 if (!this.selectedPiece && e.target.classList.contains("piece"))
280 // Next few lines to center the piece on mouse cursor
281 let rect = e.target.parentNode.getBoundingClientRect();
283 x: rect.x + rect.width/2,
284 y: rect.y + rect.width/2,
285 id: e.target.parentNode.id
287 this.selectedPiece = e.target.cloneNode();
288 this.selectedPiece.style.position = "absolute";
289 this.selectedPiece.style.top = 0;
290 this.selectedPiece.style.display = "inline-block";
291 this.selectedPiece.style.zIndex = 3000;
292 const startSquare = getSquareFromId(e.target.parentNode.id);
293 this.possibleMoves = [];
294 const color = (this.analyze || this.gameOver ? this.vr.turn : this.userColor);
295 if (this.vr.canIplay(color,startSquare))
296 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
297 // Next line add moving piece just after current image
298 // (required for Crazyhouse reserve)
299 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
302 mousemove: function(e) {
303 if (!this.selectedPiece)
305 e = e || window.event;
306 // If there is an active element, move it around
307 if (!!this.selectedPiece)
309 const [offsetX,offsetY] = !!e.clientX
310 ? [e.clientX,e.clientY] //desktop browser
311 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
312 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
313 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
316 mouseup: function(e) {
317 if (!this.selectedPiece)
319 e = e || window.event;
320 // Read drop target (or parentElement, parentNode... if type == "img")
321 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
322 const [offsetX,offsetY] = !!e.clientX
323 ? [e.clientX,e.clientY]
324 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
325 let landing = document.elementFromPoint(offsetX, offsetY);
326 this.selectedPiece.style.zIndex = 3000;
327 // Next condition: classList.contains(piece) fails because of marks
328 while (landing.tagName == "IMG")
329 landing = landing.parentNode;
330 if (this.start.id == landing.id)
332 // A click: selectedPiece and possibleMoves are already filled
335 // OK: process move attempt
336 let endSquare = getSquareFromId(landing.id);
337 let moves = this.findMatchingMoves(endSquare);
338 this.possibleMoves = [];
339 if (moves.length > 1)
340 this.choices = moves;
341 else if (moves.length==1)
343 // Else: impossible move
344 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
345 delete this.selectedPiece;
346 this.selectedPiece = null;
348 findMatchingMoves: function(endSquare) {
349 // Run through moves list and return the matching set (if promotions...)
351 this.possibleMoves.forEach(function(m) {
352 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
357 play: function(move) {
358 this.$emit('play-move', move);
377 display: inline-block
382 padding-bottom: 12.5%
392 // NOTE: no variants with reserve of size != 8
399 @media screen and (max-width: 767px)
404 margin: 0 auto 0 auto
408 background-color: rgba(0,0,0,0)
411 background-color: #e6ee9c
413 background-color: skyblue
422 img.piece, img.mark-square
441 background-color: #00cc66 !important
444 filter: brightness(50%)
447 background-color: #cc3300 !important
449 .light-square.lichess
450 background-color: #f0d9b5;
452 background-color: #b58863;
454 .light-square.chesscom
455 background-color: #e5e5ca;
456 .dark-square.chesscom
457 background-color: #6f8f57;
459 .light-square.chesstempo
460 background-color: #fdfdfd;
461 .dark-square.chesstempo
462 background-color: #88a0a8;