2 import { getSquareId, getSquareFromId } from "@/utils/squareId";
3 import { ArrayFun } from "@/utils/array";
4 import { store } from "@/store";
7 // Last move cannot be guessed from here, and is required to highlight squares
8 // vr: object to check moves, print board...
9 // userColor is left undefined for an external observer
21 possibleMoves: [], //filled after each valid click/dragstart
22 choices: [], //promotion pieces, or checkered captures... (as moves)
23 selectedPiece: null, //moving piece (or clicked piece)
24 start: {}, //pixels coordinates + id of starting square (click or drag)
25 settings: store.state.settings
30 // Return empty div of class 'game' to avoid error when setting size
37 const [sizeX, sizeY] = [V.size.x, V.size.y];
38 // Precompute hints squares to facilitate rendering
39 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
40 this.possibleMoves.forEach(m => {
41 hintSquares[m.end.x][m.end.y] = true;
43 // Also precompute in-check squares
44 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
45 this.incheck.forEach(sq => {
46 incheckSq[sq[0]][sq[1]] = true;
49 // Create board element (+ reserves if needed by variant or mode)
50 const lm = this.lastMove;
51 const showLight = this.settings.highlight && this.vname != "Dark";
60 [...Array(sizeX).keys()].map(i => {
61 let ci = this.orientation == "w" ? i : sizeX - i - 1;
68 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
70 [...Array(sizeY).keys()].map(j => {
71 let cj = this.orientation == "w" ? j : sizeY - j - 1;
74 this.vr.board[ci][cj] != V.EMPTY &&
75 (this.vname != "Dark" ||
78 this.vr.enlightened[this.userColor][ci][cj]))
85 !!this.selectedPiece &&
86 this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj
91 V.getPpath(this.vr.board[ci][cj]) +
97 if (this.settings.hints && hintSquares[ci][cj]) {
104 src: "/images/mark.svg"
114 ["board" + sizeY]: true,
115 "light-square": (i + j) % 2 == 0,
116 "dark-square": (i + j) % 2 == 1,
117 [this.settings.bcolor]: true,
119 this.vname == "Dark" &&
122 !this.vr.enlightened[this.userColor][ci][cj]),
124 showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
125 incheck: showLight && incheckSq[ci][cj]
128 id: getSquareId({ x: ci, y: cj })
137 let elementArray = [gameDiv];
138 const playingColor = this.userColor || "w"; //default for an observer
139 if (this.vr.reserve) {
140 const shiftIdx = playingColor == "w" ? 0 : 1;
141 let myReservePiecesArray = [];
142 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
143 myReservePiecesArray.push(
147 class: { board: true, ["board" + sizeY]: true },
148 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }
152 class: { piece: true, reserve: true },
156 this.vr.getReservePpath(playingColor, i) +
160 h("sup", { class: { "reserve-count": true } }, [
161 this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
167 let oppReservePiecesArray = [];
168 const oppCol = V.GetOppCol(playingColor);
169 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
170 oppReservePiecesArray.push(
174 class: { board: true, ["board" + sizeY]: true },
175 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }
179 class: { piece: true, reserve: true },
183 this.vr.getReservePpath(oppCol, i) +
187 h("sup", { class: { "reserve-count": true } }, [
188 this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]
208 "reserve-row-1": true
213 h("div", { class: { row: true } }, oppReservePiecesArray)
216 elementArray.push(reserves);
218 const boardElt = document.querySelector(".game");
219 if (this.choices.length > 0 && !!boardElt) {
220 //no choices to show at first drawing
221 const squareWidth = boardElt.offsetWidth / sizeY;
222 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
226 attrs: { id: "choices" },
227 class: { row: true },
229 top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
232 (squareWidth * (sizeY - this.choices.length)) / 2 +
234 width: this.choices.length * squareWidth + "px",
235 height: squareWidth + "px"
238 this.choices.map(m => {
239 //a "choice" is a move
245 ["board" + sizeY]: true
248 width: 100 / this.choices.length + "%",
249 "padding-bottom": 100 / this.choices.length + "%"
257 V.getPpath(m.appear[0].c + m.appear[0].p) +
260 class: { "choice-piece": true },
272 elementArray.unshift(choices);
275 // NOTE: click = mousedown + mouseup
276 if ("ontouchstart" in window) {
279 touchstart: this.mousedown,
280 touchmove: this.mousemove,
281 touchend: this.mouseup
287 mousedown: this.mousedown,
288 mousemove: this.mousemove,
289 mouseup: this.mouseup
293 return h("div", onEvents, elementArray);
296 mousedown: function(e) {
297 // Abort if a piece is already being processed, or target is not a piece.
298 // NOTE: just looking at classList[0] because piece is the first assigned class
299 if (!!this.selectedPiece || e.target.classList[0] != "piece") return;
300 e.preventDefault(); //disable native drag & drop
301 let parent = e.target.parentNode; //the surrounding square
302 // Next few lines to center the piece on mouse cursor
303 let rect = parent.getBoundingClientRect();
305 x: rect.x + rect.width / 2,
306 y: rect.y + rect.width / 2,
309 this.selectedPiece = e.target.cloneNode();
310 let spStyle = this.selectedPiece.style;
311 spStyle.position = "absolute";
313 spStyle.display = "inline-block";
314 spStyle.zIndex = 3000;
315 const startSquare = getSquareFromId(parent.id);
316 this.possibleMoves = [];
317 const color = this.analyze ? this.vr.turn : this.userColor;
318 if (this.vr.canIplay(color, startSquare))
319 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
320 // Next line add moving piece just after current image
321 // (required for Crazyhouse reserve)
322 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
324 mousemove: function(e) {
325 if (!this.selectedPiece) return;
326 // There is an active element: move it around
327 const [offsetX, offsetY] = e.clientX
328 ? [e.clientX, e.clientY] //desktop browser
329 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
330 this.selectedPiece.style.left = offsetX - this.start.x + "px";
331 this.selectedPiece.style.top = offsetY - this.start.y + "px";
333 mouseup: function(e) {
334 if (!this.selectedPiece) return;
335 // There is an active element: obtain the move from start and end squares
336 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
337 const [offsetX, offsetY] = e.clientX
338 ? [e.clientX, e.clientY]
339 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
340 let landing = document.elementFromPoint(offsetX, offsetY);
341 this.selectedPiece.style.zIndex = 3000;
342 // Next condition: classList.contains(piece) fails because of marks
343 while (landing.tagName == "IMG") landing = landing.parentNode;
344 if (this.start.id == landing.id)
345 //one or multi clicks on same piece
347 // OK: process move attempt, landing is a square node
348 let endSquare = getSquareFromId(landing.id);
349 let moves = this.findMatchingMoves(endSquare);
350 this.possibleMoves = [];
351 if (moves.length > 1) this.choices = moves;
352 else if (moves.length == 1) this.play(moves[0]);
353 // Else: impossible move
354 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
355 delete this.selectedPiece;
356 this.selectedPiece = null;
358 findMatchingMoves: function(endSquare) {
359 // Run through moves list and return the matching set (if promotions...)
361 this.possibleMoves.forEach(function(m) {
362 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) moves.push(m);
366 play: function(move) {
367 this.$emit("play-move", move);
373 <style lang="sass" scoped>
383 // NOTE: no variants with reserve of size != 8
396 background-color: rgba(0,0,0,0)
399 background-color: #e6ee9c
401 background-color: skyblue
413 background-color: #00cc66 !important
416 background-color: #cc3300 !important
418 .light-square.lichess
419 background-color: #f0d9b5;
421 background-color: #b58863;
423 .light-square.chesscom
424 background-color: #e5e5ca;
425 .dark-square.chesscom
426 background-color: #6f8f57;
428 .light-square.chesstempo
429 background-color: #fdfdfd;
430 .dark-square.chesstempo
431 background-color: #88a0a8;