X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=variants%2FDiamond%2Fclass.js;fp=variants%2FDiamond%2Fclass.js;h=5145184e21c7a7237aa6ff7df887c99118424c77;hp=0000000000000000000000000000000000000000;hb=5212758164eaa08e382b7bc281f87999c8af352b;hpb=a55fde41f82207bf401c54b158dd6d0182799fd3 diff --git a/variants/Diamond/class.js b/variants/Diamond/class.js new file mode 100644 index 0000000..5145184 --- /dev/null +++ b/variants/Diamond/class.js @@ -0,0 +1,96 @@ +import ChessRules from "/base_rules.js"; +import {ArrayFun} from "/utils/array.js"; +import {Random} from "/utils/alea.js"; + +export default class DiamondRules extends ChessRules { + + get hasFlags() { + return false; + } + + get hasEnpassant() { + return false; + } + + getSvgChessboard() { + const diagonal = 10 * this.size.y * Math.sqrt(2); + const halfDiag = 0.5 * diagonal; + const deltaTrans = 10 * this.size.y * (Math.sqrt(2) - 1) / 2; + let board = ` + `; + board += ``; + board += this.getBaseSvgChessboard(); + board += ""; + return board; + } + + getPieceWidth(rwidth) { + return (0.95 * rwidth / (Math.sqrt(2) * this.size.y)); + } + + getPixelPosition(i, j, r) { + if (i < 0 || j < 0 || typeof i == "string") + return super.getPixelPosition(i, j, r); + const sqSize = this.getPieceWidth(r.width) / 0.95; + const flipped = this.flippedBoard; + i = (flipped ? this.size.x - 1 - i : i); + j = (flipped ? this.size.y - 1 - j : j); + const sq2 = Math.sqrt(2); + const shift = [- sqSize / 2, sqSize * (sq2 - 1) / 2]; + const x = (j - i) * sqSize / sq2 + shift[0] + r.width / 2; + const y = (i + j) * sqSize / sq2 + shift[1]; + return [r.x + x, r.y + y]; + } + + genRandInitBaseFen() { + if (this.options["randomness"] == 0) { + return { + fen: "krbp4/rqnp4/nbpp4/pppp4/4PPPP/4PPBN/4PNQR/4PBRK", + o: {} + }; + } + let pieces = { w: new Array(8), b: new Array(8) }; + for (let c of ["w", "b"]) { + if (c == 'b' && options.randomness == 1) { + pieces['b'] = pieces['w']; + break; + } + // Get random squares for every piece, totally freely + let positions = Random.shuffle(ArrayFun.range(8)); + const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; + const rem2 = positions[0] % 2; + if (rem2 == positions[1] % 2) { + // Fix bishops (on different colors) + for (let i=2; i<8; i++) { + if (positions[i] % 2 != rem2) { + [positions[1], positions[i]] = [positions[i], positions[1]]; + break; + } + } + } + for (let i = 0; i < 8; i++) + pieces[c][positions[i]] = composition[i]; + } + const fen = ( + pieces["b"].slice(0, 3).join("") + "p4/" + + pieces["b"].slice(3, 6).join("") + "p4/" + + pieces["b"].slice(6, 8).join("") + "pp4/" + + "pppp4/4PPPP/" + + "4PP" + pieces["w"].slice(6, 8).reverse().join("").toUpperCase() + "/" + + "4P" + pieces["w"].slice(3, 6).reverse().join("").toUpperCase() + "/" + + "4P" + pieces["w"].slice(0, 3).reverse().join("").toUpperCase()); + return { fen: fen, o: {} }; + } + + pieces(color, x, y) { + let res = super.pieces(color, x, y); + const pawnShift = this.getPawnShift(color || 'w'); + res['p'].moves = [{steps: [[pawnShift, pawnShift]], range: 1}]; + res['p'].attack = [{steps: [[0, pawnShift], [pawnShift, 0]], range: 1}]; + return res; + } + +};