X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=variants%2FHex%2Fclass.js;h=0797c91e42c0498699b8080f3d4bcddf50ba364a;hp=ec14a0632718ee2ac0538649132adebd1b2ec3c5;hb=f31de5e46015a93dca20765da61670035ce8f491;hpb=d621e620e7b568df94c53611f6c71ab318f4ffe3 diff --git a/variants/Hex/class.js b/variants/Hex/class.js index ec14a06..0797c91 100644 --- a/variants/Hex/class.js +++ b/variants/Hex/class.js @@ -1,4 +1,7 @@ -// https://www.boardspace.net/hex/english/Rules%20-%20HexWiki.htm +import ChessRules from "/base_rules.js"; +import PiPo from "/utils/PiPo.js"; +import Move from "/utils/Move.js"; + export default class HexRules extends ChessRules { static get Options() { @@ -6,33 +9,43 @@ export default class HexRules extends ChessRules { input: [ { label: "Board size", + variable: "bsize", type: "number", - defaut: 11, - variable: "bsize" - } - ], - check: [ + defaut: 11 + }, { label: "Swap", - defaut: true, - variable: "swap" + variable: "swap", + type: "checkbox", + defaut: true } ] }; } + get hasFlags() { + return false; + } + get hasEnpassant() { + return false; + } get hasReserve() { return false; } - get noAnimate() { return true; } + get clickOnly() { + return true; + } doClick(coords) { if ( - this.board[coords.x][coords.y] != "" && - (!this.swap || this.movesCount >= 2) + this.playerColor != this.turn || + ( + this.board[coords.x][coords.y] != "" && + (!this.options["swap"] || this.movesCount >= 2) + ) ) { return null; } @@ -61,121 +74,168 @@ export default class HexRules extends ChessRules { return res; } - genRandInitFen() { + genRandInitBaseFen() { // NOTE: size.x == size.y (square boards) - const emptyCount = C.FenEmptySquares(this.size.x.repeat); - return (emptyCount + "/").repeat(this.size.x).slice(0, -1); - } - - getPieceWidth(rwidth) { - return (rwidth / this.size.y); //TODO + const emptyCount = C.FenEmptySquares(this.size.x); + return { + fen: (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0", + o: {} + }; } - // TODO getSvgChessboard() { + // NOTE: with small margin seems nicer + let width = 173.2 * this.size.y + 173.2 * (this.size.y-1) / 2 + 30, + height = 50 + Math.floor(150 * this.size.x) + 30, + min_x = -86.6 - 15, + min_y = -100 - 15; + if (this.size.ratio < 1) { + // Rotate by 30 degrees to display vertically + [width, height] = [height, width]; + [min_x, min_y] = [min_y, min_x]; + } let board = ` `; + board += "`; + board += ` + `; } } - board += ""; + board += ``; return board; } setupPieces() { - // TODO: just scan board and get IDs, and addClass "bg-white" or "bg-black" - } - - // TODO (NOTE: no flip here, always same view) - getPixelPosition(i, j, r) { - if (i < 0 || j < 0) - return [0, 0]; //piece vanishes - let x, y; - const sqSize = r.width / this.size.y; - const flipped = (this.playerColor == 'b'); - const x = (flipped ? this.size.y - 1 - j : j) * sqSize, - y = (flipped ? this.size.x - 1 - i : i) * sqSize; - return [r.x + x, r.y + y]; - } - - initMouseEvents() { - const mousedown = (e) => { - if (e.touches && e.touches.length > 1) - e.preventDefault(); - const cd = this.idToCoords(e.target.id); - if (cd) { - const move = this.doClick(cd); - if (move) - this.playPlusVisual(move); + for (let i=0; i x + "." + y; + // Explore one connected component: + const neighborsSearch = ([x, y], index) => { + // Let's say "white" connects on x and "black" on y + const z = (oppCol == 'w' ? x : y); + if (z < min) + min = z; + if (z > max) + max = z; + explored[index] = true; + component[index] = true; + for (let [dx, dy] of super.pieces()['k'].moves[0].steps) { + const [nx, ny] = [x + dx, y + dy]; + const nidx = getIndex(nx, ny); + if ( + this.onBoard(nx, ny) && + this.getColor(nx, ny) == oppCol && + !component[nidx] + ) { + neighborsSearch([nx, ny], nidx); + } + } + }; + // Explore all components: + for (let i=0; i { -// TODO: just get ID, and remClass "bg-white" or "bg-black" (in CSS: TODO) + let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y})); + elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black")); }); move.appear.forEach(a => { -// TODO: just get ID, and addClass "bg-white" or "bg-black" (in CSS: TODO) -// this.g_pieces[a.x][a.y] = document.createElement("piece"); -// this.g_pieces[a.x][a.y].classList.add(this.pieces()[a.p]["class"]); -// this.g_pieces[a.x][a.y].classList.add(a.c == "w" ? "white" : "black"); -// this.g_pieces[a.x][a.y].style.width = pieceWidth + "px"; -// this.g_pieces[a.x][a.y].style.height = pieceWidth + "px"; + let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y})); + elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black")); }); }