jungle-board-service
Version:
Jungle board service
139 lines (138 loc) • 5.82 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Game = exports.GameStatus = void 0;
const gameLogic = __importStar(require("./gameLogic"));
const ai = __importStar(require("./ai"));
const klona_1 = require("klona");
var GameStatus;
(function (GameStatus) {
GameStatus["READY"] = "Ready";
GameStatus["PLAYING"] = "Playing";
GameStatus["PAUSE"] = "Pause";
GameStatus["END"] = "End";
GameStatus["TIE"] = "Tie";
})(GameStatus = exports.GameStatus || (exports.GameStatus = {}));
class Game {
constructor() {
this.state = { board: [] };
this.playerTurn = '';
this.moveCount = 0;
this.maxMove = 0;
this.gameStatus = GameStatus.READY;
this.isSinglePlay = false;
this.history = { moves: [] };
this.initBoard();
}
initBoard() {
this.state.board = gameLogic.getEmptyBoard();
this.gameStatus = GameStatus.READY;
}
startGame(maxMove, isSinglePlay) {
this.gameStatus = GameStatus.PLAYING;
this.state.board = gameLogic.getInitialBoard();
this.history = { moves: [] };
this.playerTurn = gameLogic.PlayerSymbol.B;
this.isSinglePlay = Boolean(isSinglePlay);
this.maxMove = maxMove;
this.moveCount = 0;
}
getAllMoves(board) {
return gameLogic.getAllMoves(board, this.playerTurn);
}
getRotatedBoard() {
const rotatedBoard = klona_1.klona(this.state.board).reverse();
for (let row = 0; row < gameLogic.ROWS; row++) {
for (let col = 0; col < gameLogic.COLS / 2; col++) {
const oppositeCol = gameLogic.COLS - col - 1;
const piece = rotatedBoard[row][col];
const oppositePiece = rotatedBoard[row][oppositeCol];
if (piece.includes(gameLogic.PlayerSymbol.B)) {
rotatedBoard[row][col] = gameLogic.PlayerSymbol.W + piece.substring(1);
}
else if (piece.includes(gameLogic.PlayerSymbol.W)) {
rotatedBoard[row][col] = gameLogic.PlayerSymbol.B + piece.substring(1);
}
if (oppositePiece.includes(gameLogic.PlayerSymbol.B)) {
rotatedBoard[row][oppositeCol] = gameLogic.PlayerSymbol.W + oppositePiece.substring(1);
}
else if (oppositePiece.includes(gameLogic.PlayerSymbol.W)) {
rotatedBoard[row][oppositeCol] = gameLogic.PlayerSymbol.B + oppositePiece.substring(1);
}
const temp = rotatedBoard[row][col];
rotatedBoard[row][col] = rotatedBoard[row][oppositeCol];
rotatedBoard[row][oppositeCol] = temp;
}
}
return rotatedBoard;
}
move(deltaFrom, deltaTo, shouldRotateBoard) {
if (!this.state.board) {
return false;
}
const moveFrom = Object.assign({}, deltaFrom);
const moveTo = Object.assign({}, deltaTo);
if (shouldRotateBoard) {
moveFrom.row = gameLogic.ROWS - moveFrom.row - 1;
moveFrom.col = gameLogic.COLS - moveFrom.col - 1;
moveTo.row = gameLogic.ROWS - moveTo.row - 1;
moveTo.col = gameLogic.COLS - moveTo.col - 1;
}
const isInvalidPiece = gameLogic.noChessPiece(this.state.board, moveFrom);
if (isInvalidPiece) {
return false;
}
const { nextBoard, winner } = gameLogic.makeMove(this.state.board, moveFrom, moveTo);
// const { prevBoard, nextBoard, winner } = gameLogic.makeMove(this.state.board, deltaFrom, deltaTo)
// this.history.moves.push(prevBoard)
this.state.board = nextBoard;
this.moveCount += 1;
if (winner !== '') {
if (winner === this.playerTurn || winner === gameLogic.getOpponentTurn(this.playerTurn)) {
this.gameStatus = GameStatus.END;
}
}
else if (this.moveCount === this.maxMove) {
this.gameStatus = GameStatus.TIE;
}
return true;
}
computerMove() {
setTimeout(() => {
if (this.state.board) {
const [deltaFrom, deltaTo] = ai.createComputerMove(this.state.board, gameLogic.PlayerSymbol.W);
const { prevBoard, nextBoard, winner } = gameLogic.makeMove(this.state.board, deltaFrom, deltaTo);
this.history.moves.push(prevBoard);
this.state.board = nextBoard;
if (winner !== '') {
if (winner === this.playerTurn || winner === gameLogic.getOpponentTurn(this.playerTurn)) {
this.gameStatus = GameStatus.END;
}
else {
this.gameStatus = GameStatus.TIE;
}
return;
}
}
}, 1000);
}
}
exports.Game = Game;