UNPKG

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

Version:
58 lines 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MatchFactory = void 0; const Board_1 = require("./Board"); class MatchFactory { } exports.MatchFactory = MatchFactory; MatchFactory.createFromObject = (object) => new MatchImpl(object.player1, object.player2, object.creationDate, Board_1.BoardFactory.createFromObject(object.initialState), object.moves, object.ratingDelta); MatchFactory.createWithDefaultInitialState = (player1, player2, creationDate) => new MatchImpl(player1, player2, creationDate, Board_1.BoardFactory.createDefault(player1, player2)); MatchFactory.createWithCustomInitialState = (player1, player2, creationDate, initialState) => new MatchImpl(player1, player2, creationDate, initialState); class MatchImpl { constructor(player1, player2, creationDate, initialState, moves = [], ratingDelta = null) { this.player1 = player1; this.player2 = player2; this.creationDate = creationDate; this.initialState = initialState; this.moves = moves; this.ratingDelta = ratingDelta; } get winner() { let winner = null; const currentState = this.computeCurrentState().state; for (const row of currentState) { for (const cell of row) { if (cell.pile) { if (JSON.stringify(cell.pile) != '{}') { if (winner == null) { winner = cell.pile.owner; } else if (cell.pile.owner != winner) { return null; } } } } } return winner; } addMove(newMove) { const currentState = this.computeCurrentState(); const movingPlayer = this.moves.length % 2 == 0 ? this.player1 : this.player2; if (currentState.getCell(newMove.x, newMove.y).pile != null && currentState.getCell(newMove.x, newMove.y).pile.owner == movingPlayer) { this.moves.push(newMove); return true; } return false; } computeCurrentState() { const ret = this.initialState.copy(); for (let i = 0; i < this.moves.length; i++) { const move = this.moves[i]; ret.applyMove(i % 2 == 0 ? this.player1 : this.player2, move); } return ret; } } //# sourceMappingURL=Match.js.map