generic-min-max
Version:
This node.js module exports a generic min-max algorithm, alongside some implementations This package comes with full typescript support!
85 lines • 3.35 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printState = exports.Square = void 0;
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
var Square;
(function (Square) {
Square["Empty"] = "-";
Square["X"] = "X";
Square["O"] = "O";
})(Square = exports.Square || (exports.Square = {}));
const GET_EMPTY_ROW = () => [Square.Empty, Square.Empty, Square.Empty];
const GET_EMPTY_BOARD = () => [[Square.X, Square.O, Square.Empty], GET_EMPTY_ROW(), GET_EMPTY_ROW()];
const STARTING_TicTacToe_STATE = {
player1Turn: true,
board: GET_EMPTY_BOARD()
};
class TicTacToeGame {
constructor(initialState = STARTING_TicTacToe_STATE) {
this.initialState = initialState;
}
getAllNextStates(state) {
if (isSomeoneWinning(state)) {
return [];
}
const allNextStates = [];
state.board.forEach((row, rowIndex) => {
row.forEach((square, columnIndex) => {
if (square === Square.Empty) {
const newNextState = cloneDeep_1.default(state);
newNextState.board[rowIndex][columnIndex] = state.player1Turn ? Square.X : Square.O;
newNextState.player1Turn = !state.player1Turn;
allNextStates.push(newNextState);
}
});
});
return allNextStates;
}
getHeuristic(state) {
if (isXWinning(state)) {
return 1;
}
else if (isOWinning(state)) {
return -1;
}
else {
return 0;
}
}
}
function printState(state) {
const { board } = state;
console.log(`${board[0]}\n${board[1]}\n${board[2]}`.split(',').join(''));
}
exports.printState = printState;
function isSomeoneWinning(state) {
return isXWinning(state) || isOWinning(state);
}
function isXWinning(state) {
return isPlayerWinning(state, Square.X);
}
function isOWinning(state) {
return isPlayerWinning(state, Square.O);
}
function areThreeSquaresOccupiedByPlayer(board, player, squaresLocations) {
const [squareLocation1, squareLocation2, squareLocation3] = squaresLocations;
return board[squareLocation1[0]][squareLocation1[1]] === player &&
board[squareLocation2[0]][squareLocation2[1]] === player &&
board[squareLocation3[0]][squareLocation3[1]] === player;
}
function isPlayerWinning(state, player) {
const { board } = state;
return areThreeSquaresOccupiedByPlayer(board, player, [[0, 0], [0, 1], [0, 2]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[1, 0], [1, 1], [1, 2]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[2, 0], [2, 1], [2, 2]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[0, 0], [1, 0], [2, 0]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[0, 1], [1, 1], [2, 1]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[0, 2], [1, 2], [2, 2]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[0, 0], [1, 1], [2, 2]]) ||
areThreeSquaresOccupiedByPlayer(board, player, [[0, 2], [1, 1], [2, 0]]);
}
exports.default = TicTacToeGame;
//# sourceMappingURL=TicTacToe.js.map