2 // This can work for squared boards (2 or 4 players), with some adaptations (TODO)
3 // TODO: for 3 players, write a "board3.js"
6 // Last move cannot be guessed from here, and is required to highlight squares
7 // vr: object to check moves, print board...
8 // mode: HH, HC or analyze
9 // userColor: for mode HH or HC
10 props: ["vr","lastMove","mode","orientation","userColor"],
13 hints: (!localStorage["hints"] ? true : localStorage["hints"] === "1"),
14 bcolor: localStorage["bcolor"] || "lichess", //lichess, chesscom or chesstempo
15 possibleMoves: [], //filled after each valid click/dragstart
16 choices: [], //promotion pieces, or checkered captures... (as moves)
17 selectedPiece: null, //moving piece (or clicked piece)
19 start: {}, //pixels coordinates + id of starting square (click or drag)
25 const [sizeX,sizeY] = [V.size.x,V.size.y];
26 // Precompute hints squares to facilitate rendering
27 let hintSquares = doubleArray(sizeX, sizeY, false);
28 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
29 // Also precompute in-check squares
30 let incheckSq = doubleArray(sizeX, sizeY, false);
31 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
32 const squareWidth = 40; //TODO: compute this
36 attrs: { "id": "choices" },
37 'class': { 'row': true },
39 "display": this.choices.length>0?"block":"none",
40 "top": "-" + ((sizeY/2)*squareWidth+squareWidth/2) + "px",
41 "width": (this.choices.length * squareWidth) + "px",
42 "height": squareWidth + "px",
45 this.choices.map(m => { //a "choice" is a move
50 ['board'+sizeY]: true,
53 'width': (100/this.choices.length) + "%",
54 'padding-bottom': (100/this.choices.length) + "%",
59 attrs: { "src": '/images/pieces/' +
60 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
61 'class': { 'choice-piece': true },
63 "click": e => { this.play(m); this.choices=[]; },
64 // NOTE: add 'touchstart' event to fix a problem on smartphones
65 "touchstart": e => { this.play(m); this.choices=[]; },
72 // Create board element (+ reserves if needed by variant or mode)
73 const lm = this.lastMove;
74 const showLight = this.hints && variant.name != "Dark";
83 [...Array(sizeX).keys()].map(i => {
84 let ci = (this.orientation=='w' ? i : sizeX-i-1);
91 style: { 'opacity': this.choices.length>0?"0.5":"1" },
93 [...Array(sizeY).keys()].map(j => {
94 let cj = (this.orientation=='w' ? j : sizeY-j-1);
96 if (this.vr.board[ci][cj] != V.EMPTY && (variant.name!="Dark"
97 || this.gameOver || this.mode == "analyze"
98 || this.vr.enlightened[this.userColor][ci][cj]))
106 'ghost': !!this.selectedPiece
107 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
110 src: "/images/pieces/" +
111 V.getPpath(this.vr.board[ci][cj]) + ".svg",
117 if (this.hints && hintSquares[ci][cj])
127 src: "/images/mark.svg",
138 ['board'+sizeY]: true,
139 'light-square': (i+j)%2==0,
140 'dark-square': (i+j)%2==1,
142 'in-shadow': variant.name=="Dark" && !this.gameOver
143 && this.mode != "analyze"
144 && !this.vr.enlightened[this.userColor][ci][cj],
145 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
146 'incheck': showLight && incheckSq[ci][cj],
149 id: getSquareId({x:ci,y:cj}),
158 let elementArray = [choices, gameDiv];
159 if (!!this.vr.reserve)
161 const shiftIdx = (this.userColor=="w" ? 0 : 1);
162 let myReservePiecesArray = [];
163 for (let i=0; i<V.RESERVE_PIECES.length; i++)
165 myReservePiecesArray.push(h('div',
167 'class': {'board':true, ['board'+sizeY]:true},
168 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
173 'class': {"piece":true, "reserve":true},
175 "src": "/images/pieces/" +
176 this.vr.getReservePpath(this.userColor,i) + ".svg",
180 {"class": { "reserve-count": true } },
181 [ this.vr.reserve[this.userColor][V.RESERVE_PIECES[i]] ]
185 let oppReservePiecesArray = [];
186 const oppCol = V.GetOppCol(this.userColor);
187 for (let i=0; i<V.RESERVE_PIECES.length; i++)
189 oppReservePiecesArray.push(h('div',
191 'class': {'board':true, ['board'+sizeY]:true},
192 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
197 'class': {"piece":true, "reserve":true},
199 "src": "/images/pieces/" +
200 this.vr.getReservePpath(oppCol,i) + ".svg",
204 {"class": { "reserve-count": true } },
205 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
209 let reserves = h('div',
221 "reserve-row-1": true,
227 { 'class': { 'row': true }},
228 oppReservePiecesArray
232 elementArray.push(reserves);
240 "col-md-offset-1":true,
242 "col-lg-offset-2":true,
244 // NOTE: click = mousedown + mouseup
246 mousedown: this.mousedown,
247 mousemove: this.mousemove,
248 mouseup: this.mouseup,
249 touchstart: this.mousedown,
250 touchmove: this.mousemove,
251 touchend: this.mouseup,
258 mousedown: function(e) {
259 e = e || window.event;
262 while (!ingame && elem !== null)
264 if (elem.classList.contains("game"))
269 elem = elem.parentElement;
271 if (!ingame) //let default behavior (click on button...)
273 e.preventDefault(); //disable native drag & drop
274 if (!this.selectedPiece && e.target.classList.contains("piece"))
276 // Next few lines to center the piece on mouse cursor
277 let rect = e.target.parentNode.getBoundingClientRect();
279 x: rect.x + rect.width/2,
280 y: rect.y + rect.width/2,
281 id: e.target.parentNode.id
283 this.selectedPiece = e.target.cloneNode();
284 this.selectedPiece.style.position = "absolute";
285 this.selectedPiece.style.top = 0;
286 this.selectedPiece.style.display = "inline-block";
287 this.selectedPiece.style.zIndex = 3000;
288 const startSquare = getSquareFromId(e.target.parentNode.id);
289 this.possibleMoves = [];
290 const color = this.mode=="analyze" || this.gameOver
293 if (this.vr.canIplay(color,startSquare))
294 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
295 // Next line add moving piece just after current image
296 // (required for Crazyhouse reserve)
297 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
300 mousemove: function(e) {
301 if (!this.selectedPiece)
303 e = e || window.event;
304 // If there is an active element, move it around
305 if (!!this.selectedPiece)
307 const [offsetX,offsetY] = !!e.clientX
308 ? [e.clientX,e.clientY] //desktop browser
309 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
310 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
311 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
314 mouseup: function(e) {
315 if (!this.selectedPiece)
317 e = e || window.event;
318 // Read drop target (or parentElement, parentNode... if type == "img")
319 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
320 const [offsetX,offsetY] = !!e.clientX
321 ? [e.clientX,e.clientY]
322 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
323 let landing = document.elementFromPoint(offsetX, offsetY);
324 this.selectedPiece.style.zIndex = 3000;
325 // Next condition: classList.contains(piece) fails because of marks
326 while (landing.tagName == "IMG")
327 landing = landing.parentNode;
328 if (this.start.id == landing.id)
330 // A click: selectedPiece and possibleMoves are already filled
333 // OK: process move attempt
334 let endSquare = getSquareFromId(landing.id);
335 let moves = this.findMatchingMoves(endSquare);
336 this.possibleMoves = [];
337 if (moves.length > 1)
338 this.choices = moves;
339 else if (moves.length==1)
341 // Else: impossible move
342 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
343 delete this.selectedPiece;
344 this.selectedPiece = null;
346 findMatchingMoves: function(endSquare) {
347 // Run through moves list and return the matching set (if promotions...)
349 this.possibleMoves.forEach(function(m) {
350 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
355 play: function(move) {
356 this.$emit('play-move', move);