UNPKG

@andreabiagini5/applicazioni-e-servizi-web-project

Version:
133 lines 5 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoardFactory = void 0; const Cell_1 = require("./Cell"); const Pile_1 = require("./Pile"); class BoardFactory { } exports.BoardFactory = BoardFactory; _a = BoardFactory; // Based on the default initial piles positioning, width and height must be greater than 5 BoardFactory.DEFAULT_WIDTH = 9; BoardFactory.DEFAULT_HEIGHT = 9; BoardFactory.INITIAL_STATE = (player1, player2) => [ { x: 1, y: 2, pile: Pile_1.PileFactory.create(player1, 1) }, { x: 3, y: 2, pile: Pile_1.PileFactory.create(player1, 1) }, { x: 2, y: 1, pile: Pile_1.PileFactory.create(player1, 1) }, { x: 2, y: 3, pile: Pile_1.PileFactory.create(player1, 1) }, { x: _a.DEFAULT_WIDTH - 4, y: _a.DEFAULT_HEIGHT - 3, pile: Pile_1.PileFactory.create(player2, 1), }, { x: _a.DEFAULT_WIDTH - 2, y: _a.DEFAULT_HEIGHT - 3, pile: Pile_1.PileFactory.create(player2, 1), }, { x: _a.DEFAULT_WIDTH - 3, y: _a.DEFAULT_HEIGHT - 4, pile: Pile_1.PileFactory.create(player2, 1), }, { x: _a.DEFAULT_WIDTH - 3, y: _a.DEFAULT_HEIGHT - 2, pile: Pile_1.PileFactory.create(player2, 1), }, ]; /** * Board factory. Takes as input an object containing all board properties, * and returns an object of type `Board` with the given properties. * @param object an object containing the board properties * @returns a board with the provided properties */ BoardFactory.createFromObject = (object) => new BoardImpl(object.width, object.height, object.state); /** * Board factory. Returns a board with the default initial state. * @param player1 the username of player1 * @param player2 the username of player2 * @returns a board with the default initial state */ BoardFactory.createDefault = (player1, player2) => _a.createCustom(_a.DEFAULT_WIDTH, _a.DEFAULT_HEIGHT, _a.INITIAL_STATE(player1, player2)); /** * Board factory. Returns a board with the provided dimensions and the * given piles specified as `BoardEntry`s. * @param width the custom board width * @param height the custom board height * @param piles a list of piles tobe included in the board * @returns a board with the given dimensions and the given piles */ BoardFactory.createCustom = (width, height, piles) => { if (width > 5 && height > 5) { return new BoardImpl(width, height, Array(height) .fill(null) .map((_, row) => Array(width) .fill(null) .map((_, col) => { var ret = Cell_1.CellFactory.createEmpty(); piles.forEach(p => { if (row === p.x && col === p.y) { ret = Cell_1.CellFactory.create(Pile_1.PileFactory.create(p.pile.owner, p.pile.numberOfGrains)); } }); return ret; }))); } else { throw new Error('Board width and heignt must be greater than 5'); } }; class BoardImpl { constructor(width, height, state) { this.width = width; this.height = height; this.state = state; } applyMove(movingPlayer, move) { this.state[move.x][move.y].addGrain(movingPlayer); const collapsingPiles = []; do { // Empties array collapsingPiles.length = 0; for (let i = 0; i < this.height; i++) { for (let j = 0; j < this.width; j++) { if (this.state[i][j].pile != null && this.state[i][j].pile.numberOfGrains >= 4) collapsingPiles.push([i, j]); } } collapsingPiles.forEach(([i, j]) => { this.state[i][j].collapse(); this.state[(i - 1 + this.width) % this.width][j].addGrain(movingPlayer); this.state[(i + 1 + this.width) % this.width][j].addGrain(movingPlayer); this.state[i][(j - 1 + this.height) % this.height].addGrain(movingPlayer); this.state[i][(j + 1 + this.height) % this.height].addGrain(movingPlayer); }); } while (collapsingPiles.length != 0); } getCell(x, y) { return this.state[x][y]; } setCell(x, y, cell) { this.state[x][y] = Object.assign({}, cell); } copy() { // Estrae le pile esistenti per riutilizzare createCustom const existingPiles = []; for (let x = 0; x < this.height; x++) { for (let y = 0; y < this.width; y++) { const cell = this.state[x][y]; if (cell.pile) { existingPiles.push({ x, y, pile: Pile_1.PileFactory.create(cell.pile.owner, cell.pile.numberOfGrains), }); } } } return BoardFactory.createCustom(this.width, this.height, existingPiles); } } //# sourceMappingURL=Board.js.map