X-Git-Url: https://git.auder.net/assets/rpsls.css?a=blobdiff_plain;f=base_rules.js;h=f9913c44bea0be04c6ba049fed65c97a18053492;hb=1080bc93379cd270e09f7d125e2cb95bda51f95f;hp=b315fd59a94ae77d3e2aa4a366e58245ae6a3805;hpb=9aebe2aac02158a4f1c92ad0ac529f8ed245602e;p=xogo.git diff --git a/base_rules.js b/base_rules.js index b315fd5..f9913c4 100644 --- a/base_rules.js +++ b/base_rules.js @@ -1,5 +1,6 @@ import {Random} from "/utils/alea.js"; import {ArrayFun} from "/utils/array.js"; +import {FenUtil} from "/utils/setupPieces.js"; import PiPo from "/utils/PiPo.js"; import Move from "/utils/Move.js"; @@ -215,66 +216,20 @@ export default class ChessRules { // Setup the initial random-or-not (asymmetric-or-not) position genRandInitBaseFen() { - let fen, flags = "0707"; - if (!this.options.randomness) - // Deterministic: - fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; - - else { - // Randomize - let pieces = {w: new Array(8), b: new Array(8)}; - flags = ""; - // Shuffle pieces on first (and last rank if randomness == 2) - for (let c of ["w", "b"]) { - if (c == 'b' && this.options.randomness == 1) { - pieces['b'] = pieces['w']; - flags += flags; - break; - } - let positions = ArrayFun.range(8); - // Get random squares for bishops - let randIndex = 2 * Random.randInt(4); - const bishop1Pos = positions[randIndex]; - // The second bishop must be on a square of different color - let randIndex_tmp = 2 * Random.randInt(4) + 1; - const bishop2Pos = positions[randIndex_tmp]; - // Remove chosen squares - positions.splice(Math.max(randIndex, randIndex_tmp), 1); - positions.splice(Math.min(randIndex, randIndex_tmp), 1); - // Get random squares for knights - randIndex = Random.randInt(6); - const knight1Pos = positions[randIndex]; - positions.splice(randIndex, 1); - randIndex = Random.randInt(5); - const knight2Pos = positions[randIndex]; - positions.splice(randIndex, 1); - // Get random square for queen - randIndex = Random.randInt(4); - const queenPos = positions[randIndex]; - positions.splice(randIndex, 1); - // Rooks and king positions are now fixed, - // because of the ordering rook-king-rook - const rook1Pos = positions[0]; - const kingPos = positions[1]; - const rook2Pos = positions[2]; - // Finally put the shuffled pieces in the board array - pieces[c][rook1Pos] = "r"; - pieces[c][knight1Pos] = "n"; - pieces[c][bishop1Pos] = "b"; - pieces[c][queenPos] = "q"; - pieces[c][kingPos] = "k"; - pieces[c][bishop2Pos] = "b"; - pieces[c][knight2Pos] = "n"; - pieces[c][rook2Pos] = "r"; - flags += rook1Pos.toString() + rook2Pos.toString(); + const s = FenUtil.setupPieces( + ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], + { + randomness: this.options["randomness"], + between: {p1: 'k', p2: 'r'}, + diffCol: ['b'], + flags: ['r'] } - fen = ( - pieces["b"].join("") + - "/pppppppp/8/8/8/8/PPPPPPPP/" + - pieces["w"].join("").toUpperCase() - ); - } - return { fen: fen, o: {flags: flags} }; + ); + return { + fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + + s.w.join("").toUpperCase(), + o: {flags: s.flags} + }; } // "Parse" FEN: just return untransformed string data @@ -331,9 +286,9 @@ export default class ChessRules { // Position part of the FEN string getPosition() { let position = ""; - for (let i = 0; i < this.size.y; i++) { + for (let i = 0; i < this.size.x; i++) { let emptyCount = 0; - for (let j = 0; j < this.size.x; j++) { + for (let j = 0; j < this.size.y; j++) { if (this.board[i][j] == "") emptyCount++; else { @@ -348,7 +303,7 @@ export default class ChessRules { if (emptyCount > 0) // "Flush remainder" position += C.FenEmptySquares(emptyCount); - if (i < this.size.y - 1) + if (i < this.size.x - 1) position += "/"; //separate rows } return position; @@ -404,9 +359,6 @@ export default class ChessRules { if (this.options[opt.variable] === undefined) this.options[opt.variable] = opt.defaut; }); - if (o.genFenOnly) - // This object will be used only for initial FEN generation - return; // Some variables this.playerColor = o.color; @@ -605,7 +557,7 @@ export default class ChessRules { chessboard.style.top = spaceTop + "px"; // Give sizes instead of recomputing them, // because chessboard might not be drawn yet. - this.setupPieces({ + this.setupVisualPieces({ width: cbWidth, height: cbHeight, x: spaceLeft, @@ -645,7 +597,7 @@ export default class ChessRules { return board; } - setupPieces(r) { + setupVisualPieces(r) { let chessboard = document.getElementById(this.containerId).querySelector(".chessboard"); if (!r) @@ -1013,7 +965,7 @@ export default class ChessRules { // TODO: onpointerdown/move/up ? See reveal.js /controllers/touch.js } - // NOTE: not called if isDiagram, or genFenOnly + // NOTE: not called if isDiagram removeListeners() { let container = document.getElementById(this.containerId); this.windowResizeObs.unobserve(container); @@ -2689,6 +2641,7 @@ export default class ChessRules { let container = document.getElementById(this.containerId); if (document.hidden) { document.onvisibilitychange = () => { + // TODO here: page reload ?! (some issues if tab changed...) document.onvisibilitychange = undefined; checkDisplayThenAnimate(700); };