2 // TODO: BoardHex for hexagonal variants (2 players)
4 import { getSquareId, getSquareFromId } from "@/utils/squareId";
5 import { ArrayFun } from "@/utils/array";
9 // Last move cannot be guessed from here, and is required to highlight squares
10 // vr: object to check moves, print board...
11 // userColor is left undefined for an external observer
12 props: ["vr","lastMove","analyze","orientation","userColor","vname"],
15 hints: (!localStorage["hints"] ? true : localStorage["hints"] === "1"),
16 bcolor: localStorage["bcolor"] || "lichess", //lichess, chesscom or chesstempo
17 possibleMoves: [], //filled after each valid click/dragstart
18 choices: [], //promotion pieces, or checkered captures... (as moves)
19 selectedPiece: null, //moving piece (or clicked piece)
21 start: {}, //pixels coordinates + id of starting square (click or drag)
27 const [sizeX,sizeY] = [V.size.x,V.size.y];
28 // Precompute hints squares to facilitate rendering
29 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
30 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
31 // Also precompute in-check squares
32 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
33 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
34 const squareWidth = 40; //TODO: compute this
38 attrs: { "id": "choices" },
39 'class': { 'row': true },
41 "display": (this.choices.length > 0 ? "block" : "none"),
42 "top": "-" + ((sizeY/2)*squareWidth+squareWidth/2) + "px",
43 "width": (this.choices.length * squareWidth) + "px",
44 "height": squareWidth + "px",
47 this.choices.map(m => { //a "choice" is a move
52 ['board'+sizeY]: true,
55 'width': (100/this.choices.length) + "%",
56 'padding-bottom': (100/this.choices.length) + "%",
61 attrs: { "src": '/images/pieces/' +
62 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
63 'class': { 'choice-piece': true },
65 "click": e => { this.play(m); this.choices=[]; },
66 // NOTE: add 'touchstart' event to fix a problem on smartphones
67 "touchstart": e => { this.play(m); this.choices=[]; },
74 // Create board element (+ reserves if needed by variant or mode)
75 const lm = this.lastMove;
76 const showLight = this.hints && this.vname != "Dark";
85 [...Array(sizeX).keys()].map(i => {
86 let ci = (this.orientation=='w' ? i : sizeX-i-1);
93 style: { 'opacity': this.choices.length>0?"0.5":"1" },
95 [...Array(sizeY).keys()].map(j => {
96 let cj = (this.orientation=='w' ? j : sizeY-j-1);
98 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
99 || this.analyze || (!!this.userColor
100 && this.vr.enlightened[this.userColor][ci][cj])))
108 'ghost': !!this.selectedPiece
109 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
112 src: "/images/pieces/" +
113 V.getPpath(this.vr.board[ci][cj]) + ".svg",
119 if (this.hints && hintSquares[ci][cj])
129 src: "/images/mark.svg",
140 ['board'+sizeY]: true,
141 'light-square': (i+j)%2==0,
142 'dark-square': (i+j)%2==1,
144 'in-shadow': this.vname=="Dark" && !this.analyze
146 || !this.vr.enlightened[this.userColor][ci][cj]),
147 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
148 'incheck': showLight && incheckSq[ci][cj],
151 id: getSquareId({x:ci,y:cj}),
160 const playingColor = this.userColor || "w"; //default for an observer
161 let elementArray = [choices, gameDiv];
162 if (!!this.vr.reserve)
164 const shiftIdx = (playingColor=="w" ? 0 : 1);
165 let myReservePiecesArray = [];
166 for (let i=0; i<V.RESERVE_PIECES.length; i++)
168 myReservePiecesArray.push(h('div',
170 'class': {'board':true, ['board'+sizeY]:true},
171 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
176 'class': {"piece":true, "reserve":true},
178 "src": "/images/pieces/" +
179 this.vr.getReservePpath(playingColor,i) + ".svg",
183 {"class": { "reserve-count": true } },
184 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
188 let oppReservePiecesArray = [];
189 const oppCol = V.GetOppCol(playingColor);
190 for (let i=0; i<V.RESERVE_PIECES.length; i++)
192 oppReservePiecesArray.push(h('div',
194 'class': {'board':true, ['board'+sizeY]:true},
195 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
200 'class': {"piece":true, "reserve":true},
202 "src": "/images/pieces/" +
203 this.vr.getReservePpath(oppCol,i) + ".svg",
207 {"class": { "reserve-count": true } },
208 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
212 let reserves = h('div',
224 "reserve-row-1": true,
230 { 'class': { 'row': true }},
231 oppReservePiecesArray
235 elementArray.push(reserves);
240 // NOTE: click = mousedown + mouseup
242 mousedown: this.mousedown,
243 mousemove: this.mousemove,
244 mouseup: this.mouseup,
245 touchstart: this.mousedown,
246 touchmove: this.mousemove,
247 touchend: this.mouseup,
254 mousedown: function(e) {
255 e = e || window.event;
258 while (!ingame && elem !== null)
260 if (elem.classList.contains("game"))
265 elem = elem.parentElement;
267 if (!ingame) //let default behavior (click on button...)
269 e.preventDefault(); //disable native drag & drop
270 if (!this.selectedPiece && e.target.classList.contains("piece"))
272 // Next few lines to center the piece on mouse cursor
273 let rect = e.target.parentNode.getBoundingClientRect();
275 x: rect.x + rect.width/2,
276 y: rect.y + rect.width/2,
277 id: e.target.parentNode.id
279 this.selectedPiece = e.target.cloneNode();
280 this.selectedPiece.style.position = "absolute";
281 this.selectedPiece.style.top = 0;
282 this.selectedPiece.style.display = "inline-block";
283 this.selectedPiece.style.zIndex = 3000;
284 const startSquare = getSquareFromId(e.target.parentNode.id);
285 this.possibleMoves = [];
286 const color = (this.analyze ? this.vr.turn : this.userColor);
287 if (this.vr.canIplay(color,startSquare))
288 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
289 // Next line add moving piece just after current image
290 // (required for Crazyhouse reserve)
291 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
294 mousemove: function(e) {
295 if (!this.selectedPiece)
297 e = e || window.event;
298 // If there is an active element, move it around
299 if (!!this.selectedPiece)
301 const [offsetX,offsetY] = !!e.clientX
302 ? [e.clientX,e.clientY] //desktop browser
303 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
304 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
305 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
308 mouseup: function(e) {
309 if (!this.selectedPiece)
311 e = e || window.event;
312 // Read drop target (or parentElement, parentNode... if type == "img")
313 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
314 const [offsetX,offsetY] = !!e.clientX
315 ? [e.clientX,e.clientY]
316 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
317 let landing = document.elementFromPoint(offsetX, offsetY);
318 this.selectedPiece.style.zIndex = 3000;
319 // Next condition: classList.contains(piece) fails because of marks
320 while (landing.tagName == "IMG")
321 landing = landing.parentNode;
322 if (this.start.id == landing.id)
324 // A click: selectedPiece and possibleMoves are already filled
327 // OK: process move attempt
328 let endSquare = getSquareFromId(landing.id);
329 let moves = this.findMatchingMoves(endSquare);
330 this.possibleMoves = [];
331 if (moves.length > 1)
332 this.choices = moves;
333 else if (moves.length==1)
335 // Else: impossible move
336 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
337 delete this.selectedPiece;
338 this.selectedPiece = null;
340 findMatchingMoves: function(endSquare) {
341 // Run through moves list and return the matching set (if promotions...)
343 this.possibleMoves.forEach(function(m) {
344 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
349 play: function(move) {
350 this.$emit('play-move', move);
366 // NOTE: no variants with reserve of size != 8
371 display: inline-block
376 padding-bottom: 12.5%
391 @media screen and (max-width: 767px)
396 margin: 0 auto 0 auto
400 background-color: rgba(0,0,0,0)
403 background-color: #e6ee9c
405 background-color: skyblue
414 img.piece, img.mark-square
433 background-color: #00cc66 !important
436 filter: brightness(50%)
439 background-color: #cc3300 !important
441 .light-square.lichess
442 background-color: #f0d9b5;
444 background-color: #b58863;
446 .light-square.chesscom
447 background-color: #e5e5ca;
448 .dark-square.chesscom
449 background-color: #6f8f57;
451 .light-square.chesstempo
452 background-color: #fdfdfd;
453 .dark-square.chesstempo
454 background-color: #88a0a8;