Comparing version 1.1.0 to 1.2.0
@@ -5,3 +5,3 @@ import { PlayerMatrix, Player, WallMatrix } from './types'; | ||
******************************************************************************/ | ||
declare class Node { | ||
export declare class QuoridorNode { | ||
_value: Player; | ||
@@ -14,4 +14,4 @@ _coordinates: { | ||
_distanceToGoal?: number; | ||
_neighbours: Node[]; | ||
_reachedFrom?: Node; | ||
_neighbours: QuoridorNode[]; | ||
_reachedFrom?: QuoridorNode; | ||
constructor(y: number, x: number, value: Player); | ||
@@ -22,4 +22,4 @@ get distanceFromStart(): number; | ||
set distanceToGoal(distance: number | undefined); | ||
get reachedFrom(): Node | undefined; | ||
set reachedFrom(node: Node | undefined); | ||
get reachedFrom(): QuoridorNode | undefined; | ||
set reachedFrom(node: QuoridorNode | undefined); | ||
get coordinates(): { | ||
@@ -29,4 +29,4 @@ x: number; | ||
}; | ||
get neighbours(): Node[]; | ||
addNeighbour(neighbour: Node): void; | ||
get neighbours(): QuoridorNode[]; | ||
addNeighbour(neighbour: QuoridorNode): void; | ||
get value(): Player; | ||
@@ -36,6 +36,5 @@ set value(value: Player); | ||
} | ||
export declare const aStar: (board: PlayerMatrix, walls: WallMatrix, startPiece: Player) => Node[] | { | ||
export declare const aStar: (board: PlayerMatrix, walls: WallMatrix, startPiece: Player) => QuoridorNode[] | { | ||
x: string; | ||
y: number; | ||
}[] | null; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.aStar = void 0; | ||
exports.aStar = exports.QuoridorNode = void 0; | ||
/******************************************************************************* | ||
* Utility functions for the aStar function and the Node | ||
******************************************************************************/ | ||
var Node = /** @class */ (function () { | ||
function Node(y, x, value) { | ||
var QuoridorNode = /** @class */ (function () { | ||
function QuoridorNode(y, x, value) { | ||
this._value = value; | ||
@@ -14,3 +14,3 @@ this._coordinates = { y: y, x: x }; | ||
} | ||
Object.defineProperty(Node.prototype, "distanceFromStart", { | ||
Object.defineProperty(QuoridorNode.prototype, "distanceFromStart", { | ||
get: function () { | ||
@@ -25,3 +25,3 @@ return this._distanceFromStart; | ||
}); | ||
Object.defineProperty(Node.prototype, "distanceToGoal", { | ||
Object.defineProperty(QuoridorNode.prototype, "distanceToGoal", { | ||
get: function () { | ||
@@ -36,3 +36,3 @@ return this._distanceToGoal; | ||
}); | ||
Object.defineProperty(Node.prototype, "reachedFrom", { | ||
Object.defineProperty(QuoridorNode.prototype, "reachedFrom", { | ||
get: function () { | ||
@@ -47,3 +47,3 @@ return this._reachedFrom; | ||
}); | ||
Object.defineProperty(Node.prototype, "coordinates", { | ||
Object.defineProperty(QuoridorNode.prototype, "coordinates", { | ||
get: function () { | ||
@@ -55,3 +55,3 @@ return this._coordinates; | ||
}); | ||
Object.defineProperty(Node.prototype, "neighbours", { | ||
Object.defineProperty(QuoridorNode.prototype, "neighbours", { | ||
get: function () { | ||
@@ -63,6 +63,6 @@ return this._neighbours; | ||
}); | ||
Node.prototype.addNeighbour = function (neighbour) { | ||
QuoridorNode.prototype.addNeighbour = function (neighbour) { | ||
this._neighbours.push(neighbour); | ||
}; | ||
Object.defineProperty(Node.prototype, "value", { | ||
Object.defineProperty(QuoridorNode.prototype, "value", { | ||
get: function () { | ||
@@ -77,3 +77,3 @@ return this._value; | ||
}); | ||
Node.prototype.relax = function () { | ||
QuoridorNode.prototype.relax = function () { | ||
var cost = { 0: 1, 1: 0, 2: 0 }; | ||
@@ -93,4 +93,5 @@ for (var _i = 0, _a = this._neighbours; _i < _a.length; _i++) { | ||
}; | ||
return Node; | ||
return QuoridorNode; | ||
}()); | ||
exports.QuoridorNode = QuoridorNode; | ||
var quoridorDistance = function (coordinateA, startPiece) { | ||
@@ -117,3 +118,3 @@ var endRowNumber = startPiece === 1 ? 8 : 0; | ||
var x = _c[_b]; | ||
nodeRow.push(new Node(y, x, mapIntMatrix[y][x])); | ||
nodeRow.push(new QuoridorNode(y, x, mapIntMatrix[y][x])); | ||
} | ||
@@ -120,0 +121,0 @@ nodeMatrix.push(nodeRow); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createGameFromMoves = exports.createNewWallMatrix = void 0; | ||
var isValidMove_1 = require("./isValidMove"); | ||
var utils_1 = require("./utils"); | ||
var createNewBoard_1 = require("./createNewBoard"); | ||
var createNewPieceMatrix = function () { return ({ | ||
@@ -101,6 +102,6 @@ a: { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0 }, | ||
var game = { | ||
board: createNewBoard_1.createNewBoard(), | ||
pieceMatrix: createNewPieceMatrix(), | ||
wallMatrix: exports.createNewWallMatrix(), | ||
history: { 1: [], 2: [] }, | ||
turn: 1, | ||
history: [], | ||
playerPositions: { 1: { x: 'e', y: 1 }, 2: { x: 'e', y: 9 } }, | ||
@@ -110,5 +111,5 @@ playerWallCounts: { 1: 10, 2: 10 }, | ||
return moves.reduce(function (game, move) { | ||
return isValidMove_1.unvalidatedMove(game, move); | ||
return utils_1.unvalidatedMove(game, move); | ||
}, game); | ||
}; | ||
exports.createGameFromMoves = createGameFromMoves; |
@@ -0,1 +1,2 @@ | ||
export { createGameFromMoves } from './createGameFromMoves'; | ||
export { isValidMove } from './isValidMove'; | ||
@@ -6,1 +7,3 @@ export { getAsciiRepresentation } from './getAsciiRepresentation'; | ||
export { getWinner } from './getWinner'; | ||
export { getValidMoveArray } from './getValidMoveArray'; | ||
export { getTurn } from './getTurn'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getWinner = exports.isGameOver = exports.makeMove = exports.getAsciiRepresentation = exports.isValidMove = void 0; | ||
exports.getTurn = exports.getValidMoveArray = exports.getWinner = exports.isGameOver = exports.makeMove = exports.getAsciiRepresentation = exports.isValidMove = exports.createGameFromMoves = void 0; | ||
var createGameFromMoves_1 = require("./createGameFromMoves"); | ||
Object.defineProperty(exports, "createGameFromMoves", { enumerable: true, get: function () { return createGameFromMoves_1.createGameFromMoves; } }); | ||
var isValidMove_1 = require("./isValidMove"); | ||
@@ -14,1 +16,5 @@ Object.defineProperty(exports, "isValidMove", { enumerable: true, get: function () { return isValidMove_1.isValidMove; } }); | ||
Object.defineProperty(exports, "getWinner", { enumerable: true, get: function () { return getWinner_1.getWinner; } }); | ||
var getValidMoveArray_1 = require("./getValidMoveArray"); | ||
Object.defineProperty(exports, "getValidMoveArray", { enumerable: true, get: function () { return getValidMoveArray_1.getValidMoveArray; } }); | ||
var getTurn_1 = require("./getTurn"); | ||
Object.defineProperty(exports, "getTurn", { enumerable: true, get: function () { return getTurn_1.getTurn; } }); |
import { Game, Move } from './types'; | ||
export declare const unvalidatedMove: (game: Game, move: Move) => Game; | ||
export declare const isValidMove: (game: Game, move: Move) => boolean; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isValidMove = exports.unvalidatedMove = void 0; | ||
var aStar_1 = require("./aStar"); | ||
var letterToNumber = function (letter) { | ||
return (letter.charCodeAt(0) - 96); | ||
}; | ||
var decrementHorizontalWallPosition = function (horizontalWallPosition) { | ||
switch (horizontalWallPosition) { | ||
case 'b': | ||
return 'a'; | ||
case 'c': | ||
return 'b'; | ||
case 'd': | ||
return 'c'; | ||
case 'e': | ||
return 'd'; | ||
case 'f': | ||
return 'e'; | ||
case 'g': | ||
return 'f'; | ||
case 'h': | ||
return 'g'; | ||
} | ||
throw Error(horizontalWallPosition + " cannot be decremented."); | ||
}; | ||
var decrementHorizontalPiecePosition = function (horizontalPiecePosition) { | ||
switch (horizontalPiecePosition) { | ||
case 'b': | ||
return 'a'; | ||
case 'c': | ||
return 'b'; | ||
case 'd': | ||
return 'c'; | ||
case 'e': | ||
return 'd'; | ||
case 'f': | ||
return 'e'; | ||
case 'g': | ||
return 'f'; | ||
case 'h': | ||
return 'g'; | ||
case 'i': | ||
return 'h'; | ||
} | ||
throw Error(horizontalPiecePosition + " cannot be decremented."); | ||
}; | ||
var incrementHorizontalWallPosition = function (horizontalWallPosition) { | ||
switch (horizontalWallPosition) { | ||
case 'a': | ||
return 'b'; | ||
case 'b': | ||
return 'c'; | ||
case 'c': | ||
return 'd'; | ||
case 'd': | ||
return 'e'; | ||
case 'e': | ||
return 'f'; | ||
case 'f': | ||
return 'g'; | ||
case 'g': | ||
return 'h'; | ||
} | ||
throw Error(horizontalWallPosition + " cannot be incremented."); | ||
}; | ||
var incrementHorizontalPiecePosition = function (horizontalPiecePosition) { | ||
switch (horizontalPiecePosition) { | ||
case 'a': | ||
return 'b'; | ||
case 'b': | ||
return 'c'; | ||
case 'c': | ||
return 'd'; | ||
case 'd': | ||
return 'e'; | ||
case 'e': | ||
return 'f'; | ||
case 'f': | ||
return 'g'; | ||
case 'g': | ||
return 'h'; | ||
case 'h': | ||
return 'i'; | ||
} | ||
throw Error(horizontalPiecePosition + " cannot be incremented."); | ||
}; | ||
var decrementVerticalWallPosition = function (verticalWallPosition) { | ||
switch (verticalWallPosition) { | ||
case 2: | ||
return 1; | ||
case 3: | ||
return 2; | ||
case 4: | ||
return 3; | ||
case 5: | ||
return 4; | ||
case 6: | ||
return 5; | ||
case 7: | ||
return 6; | ||
case 8: | ||
return 7; | ||
} | ||
throw Error(verticalWallPosition + " cannot be decremented."); | ||
}; | ||
var decrementVerticalPiecePosition = function (verticalPiecePosition) { | ||
switch (verticalPiecePosition) { | ||
case 2: | ||
return 1; | ||
case 3: | ||
return 2; | ||
case 4: | ||
return 3; | ||
case 5: | ||
return 4; | ||
case 6: | ||
return 5; | ||
case 7: | ||
return 6; | ||
case 8: | ||
return 7; | ||
case 9: | ||
return 8; | ||
} | ||
throw Error(verticalPiecePosition + " cannot be decremented."); | ||
}; | ||
var incrementVerticalWallPosition = function (verticalWallPosition) { | ||
switch (verticalWallPosition) { | ||
case 1: | ||
return 2; | ||
case 2: | ||
return 3; | ||
case 3: | ||
return 4; | ||
case 4: | ||
return 5; | ||
case 5: | ||
return 6; | ||
case 6: | ||
return 7; | ||
case 7: | ||
return 8; | ||
} | ||
throw Error(verticalWallPosition + " cannot be incremented."); | ||
}; | ||
var incrementVerticalPiecePosition = function (verticalPiecePosition) { | ||
switch (verticalPiecePosition) { | ||
case 1: | ||
return 2; | ||
case 2: | ||
return 3; | ||
case 3: | ||
return 4; | ||
case 4: | ||
return 5; | ||
case 5: | ||
return 6; | ||
case 6: | ||
return 7; | ||
case 7: | ||
return 8; | ||
case 8: | ||
return 9; | ||
} | ||
throw Error(verticalPiecePosition + " cannot be incremented."); | ||
}; | ||
var doesWallMoveOverlapExistingWall = function (game, wallMove) { | ||
var x = wallMove.x; | ||
var y = wallMove.y; | ||
if (wallMove.w === 'h') { | ||
// A horizontal wall | ||
if (game.wallMatrix[x][y].h || | ||
game.wallMatrix[x][y].v || | ||
(letterToNumber(x) > 1 && | ||
game.wallMatrix[decrementHorizontalWallPosition(x)][y].h) || | ||
(letterToNumber(x) < 8 && | ||
game.wallMatrix[incrementHorizontalWallPosition(x)][y].h)) { | ||
return true; | ||
} | ||
} | ||
if (wallMove.w === 'v') { | ||
if (game.wallMatrix[x][y].h || | ||
game.wallMatrix[x][y].v || | ||
(y > 1 && game.wallMatrix[x][decrementVerticalWallPosition(y)].v) || | ||
(y < 8 && game.wallMatrix[x][incrementVerticalWallPosition(y)].v)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
var isWallMove = function (move) { | ||
return Boolean(move.w); | ||
}; | ||
var getOppositePlayer = function (player) { | ||
return player === 1 ? 2 : 1; | ||
}; | ||
var unvalidatedMove = function (game, move) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; | ||
var currentPosition = game.playerPositions[game.turn]; | ||
if (isWallMove(move)) { | ||
// If wall move | ||
return __assign(__assign({}, game), { wallMatrix: __assign(__assign({}, game.wallMatrix), (_a = {}, _a[move.x] = __assign(__assign({}, game.wallMatrix[move.x]), (_b = {}, _b[move.y] = __assign(__assign({}, game.wallMatrix[move.x][move.y]), (_c = {}, _c[move.w] = true, _c)), _b)), _a)), playerWallCounts: __assign(__assign({}, game.playerWallCounts), (_d = {}, _d[game.turn] = game.playerWallCounts[game.turn] - 1, _d)), history: __assign(__assign({}, game.history), (_e = {}, _e[game.turn] = __spreadArray(__spreadArray([], game.history[game.turn]), [move]), _e)), turn: game.turn === 1 ? 2 : 1 }); | ||
} | ||
else { | ||
var pieceMatrixWithRemovedPiece = __assign(__assign({}, game.pieceMatrix), (_f = {}, _f[currentPosition.x] = __assign(__assign({}, game.pieceMatrix[currentPosition.x]), (_g = {}, _g[currentPosition.y] = 0, _g)), _f)); | ||
return __assign(__assign({}, game), { playerPositions: __assign(__assign({}, game.playerPositions), (_h = {}, _h[game.turn] = __assign(__assign({}, move), { previousPosition: game.playerPositions[game.turn] }), _h)), history: __assign(__assign({}, game.history), (_j = {}, _j[game.turn] = __spreadArray(__spreadArray([], game.history[game.turn]), [move]), _j)), pieceMatrix: __assign(__assign({}, pieceMatrixWithRemovedPiece), (_k = {}, _k[move.x] = __assign(__assign({}, pieceMatrixWithRemovedPiece[move.x]), (_l = {}, _l[move.y] = game.turn, _l)), _k)), turn: getOppositePlayer(game.turn) }); | ||
} | ||
}; | ||
exports.unvalidatedMove = unvalidatedMove; | ||
var isSingleUpMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === -1 && currentPosition.x === move.x) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasWallAbove = function (game, _a) { | ||
var x = _a.x, y = _a.y; | ||
if (game.wallMatrix[x][y].h || | ||
(letterToNumber(x) > 1 && | ||
game.wallMatrix[decrementHorizontalPiecePosition(x)][y].h)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isDoubleUpMove = function (currentPosition, move) { | ||
if (letterToNumber(currentPosition.x) - letterToNumber(move.x) === -2 && | ||
currentPosition.x === move.x) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasOpponentAbove = function (game, position) { | ||
if (game.pieceMatrix[position.x][incrementVerticalPiecePosition(position.y)] === | ||
getOppositePlayer(game.turn)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isUpLeftMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === -1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === 1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasWallToTheRight = function (game, _a) { | ||
var x = _a.x, y = _a.y; | ||
if ((y < 9 && | ||
game.wallMatrix[x][y] | ||
.v) || | ||
(y > 1 && | ||
game.wallMatrix[x][(y - 1)].v)) { | ||
return true; | ||
} | ||
}; | ||
var isUpRightMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === -1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === -1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
// Why do I have this one that is almost identical to the previous one? I don't know. | ||
var isRightUpMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === -1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === -1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasWallToTheLeft = function (game, _a) { | ||
var x = _a.x, y = _a.y; | ||
if ((y < 9 && | ||
game.wallMatrix[decrementHorizontalPiecePosition(x)][y].v) || | ||
(y > 1 && | ||
game.wallMatrix[decrementHorizontalPiecePosition(x)][(y - 1)].v)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isSingleRightMove = function (currentPosition, move) { | ||
if (letterToNumber(currentPosition.x) - letterToNumber(move.x) === -1 && | ||
currentPosition.y === move.y) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isDoubleRightMove = function (currentPosition, move) { | ||
if (letterToNumber(currentPosition.x) - letterToNumber(move.x) === -2 && | ||
currentPosition.y === move.y) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasOpponentToTheRight = function (game, position) { | ||
if (game.pieceMatrix[incrementHorizontalPiecePosition(position.x)][position.y] === getOppositePlayer(game.turn)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isRightDownMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === 1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === -1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasWallBelow = function (game, _a) { | ||
var x = _a.x, y = _a.y; | ||
if (game.wallMatrix[x][(y - 1)].h || | ||
(letterToNumber(x) > 1 && | ||
game.wallMatrix[decrementHorizontalPiecePosition(x)][(y - 1)].h)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isSingleDownMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === 1 && currentPosition.x === move.x) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isDoubleDownMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === 2 && currentPosition.x === move.x) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasOpponentBelow = function (game, position) { | ||
if (game.pieceMatrix[position.x][decrementVerticalPiecePosition(position.y)] === | ||
getOppositePlayer(game.turn)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isDownRightMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === 1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === -1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isDownLeftMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === 1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === 1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isSingleLeftMove = function (currentPosition, move) { | ||
if (letterToNumber(currentPosition.x) - letterToNumber(move.x) === 1 && | ||
currentPosition.y === move.y) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isDoubleLeftMove = function (currentPosition, move) { | ||
if (letterToNumber(currentPosition.x) - letterToNumber(move.x) === 2 && | ||
currentPosition.y === move.y) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var hasOpponentToTheLeft = function (game, position) { | ||
if (game.pieceMatrix[decrementHorizontalPiecePosition(position.x)][position.y] === getOppositePlayer(game.turn)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isLeftDownMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === 1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === 1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
// Why do I have isLeftUpMove when I already have isUpLeftMove? I don't know. | ||
var isLeftUpMove = function (currentPosition, move) { | ||
if (currentPosition.y - move.y === -1 && | ||
letterToNumber(currentPosition.x) - letterToNumber(move.x) === 1) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isValidNormalMove = function (game, currentPosition, move) { | ||
var x = currentPosition.x; | ||
var y = currentPosition.y; | ||
// If move is outside board | ||
if (letterToNumber(move.x) < 1 || letterToNumber(move.x) > 9) | ||
return false; | ||
if (move.y < 1 || move.y > 9) | ||
return false; | ||
// If the move lands on top of the opponent | ||
if (game.pieceMatrix[move.x][move.y] === getOppositePlayer(game.turn)) { | ||
return false; | ||
} | ||
// If up move | ||
if (isSingleUpMove(currentPosition, move)) { | ||
if (hasWallAbove(game, currentPosition)) | ||
return false; | ||
return true; | ||
} | ||
if (isDoubleUpMove(currentPosition, move) && | ||
hasOpponentAbove(game, currentPosition)) { | ||
if (hasWallAbove(game, currentPosition)) | ||
return false; | ||
if (hasWallAbove(game, { x: x, y: incrementVerticalPiecePosition(y) })) | ||
return false; | ||
return true; | ||
} | ||
if (isUpLeftMove(currentPosition, move) && | ||
hasOpponentAbove(game, currentPosition)) { | ||
if (!hasWallAbove(game, { x: x, y: incrementVerticalPiecePosition(y) })) | ||
return false; | ||
if (hasWallAbove(game, currentPosition)) | ||
return false; | ||
if (hasWallToTheRight(game, { x: x, y: incrementVerticalPiecePosition(y) })) | ||
return false; | ||
return true; | ||
} | ||
if (isUpRightMove(currentPosition, move) && | ||
hasOpponentAbove(game, currentPosition)) { | ||
if (!hasWallAbove(game, { x: x, y: incrementVerticalPiecePosition(y) })) | ||
return false; | ||
if (hasWallAbove(game, currentPosition)) | ||
return false; | ||
if (hasWallToTheLeft(game, { x: x, y: incrementVerticalPiecePosition(y) })) | ||
return false; | ||
return true; | ||
} | ||
// If right move | ||
if (isSingleRightMove(currentPosition, move)) { | ||
if (hasWallToTheRight(game, currentPosition)) | ||
return false; | ||
return true; | ||
} | ||
if (isDoubleRightMove(currentPosition, move) && | ||
hasOpponentToTheRight(game, currentPosition)) { | ||
if (hasWallToTheRight(game, currentPosition)) | ||
return false; | ||
if (hasWallToTheRight(game, { x: incrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
return true; | ||
} | ||
if (isRightUpMove(currentPosition, move) && | ||
hasOpponentToTheRight(game, currentPosition)) { | ||
if (!hasWallToTheRight(game, { x: incrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
if (hasWallToTheRight(game, currentPosition)) | ||
return false; | ||
if (hasWallAbove(game, { x: incrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
return true; | ||
} | ||
if (isRightDownMove(currentPosition, move) && | ||
hasOpponentToTheRight(game, currentPosition)) { | ||
if (!hasWallToTheRight(game, { x: incrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
if (hasWallToTheRight(game, currentPosition)) | ||
return false; | ||
if (hasWallBelow(game, { x: incrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
return true; | ||
} | ||
// If down move | ||
if (isSingleDownMove(currentPosition, move)) { | ||
if (hasWallBelow(game, { x: x, y: y })) | ||
return false; | ||
return true; | ||
} | ||
if (isDoubleDownMove(currentPosition, move) && | ||
hasOpponentBelow(game, currentPosition)) { | ||
if (hasWallBelow(game, currentPosition)) | ||
return false; | ||
if (hasWallBelow(game, { x: x, y: decrementVerticalPiecePosition(y) })) | ||
return false; | ||
return true; | ||
} | ||
if (isDownRightMove(currentPosition, move) && | ||
hasOpponentBelow(game, currentPosition)) { | ||
if (!hasWallBelow(game, { x: x, y: decrementVerticalPiecePosition(y) })) | ||
return false; | ||
if (hasWallBelow(game, currentPosition)) | ||
return false; | ||
if (hasWallToTheRight(game, { x: x, y: decrementVerticalPiecePosition(y) })) | ||
return false; | ||
return true; | ||
} | ||
if (isDownLeftMove(currentPosition, move) && | ||
hasOpponentBelow(game, currentPosition)) { | ||
if (!hasWallBelow(game, { x: x, y: decrementVerticalPiecePosition(y) })) | ||
return false; | ||
if (hasWallBelow(game, currentPosition)) | ||
return false; | ||
if (hasWallToTheLeft(game, { x: x, y: decrementVerticalPiecePosition(y) })) | ||
return false; | ||
return true; | ||
} | ||
// If left move | ||
if (isSingleLeftMove(currentPosition, move)) { | ||
if (hasWallToTheLeft(game, currentPosition)) | ||
return false; | ||
return true; | ||
} | ||
if (isDoubleLeftMove(currentPosition, move) && | ||
hasOpponentToTheLeft(game, currentPosition)) { | ||
if (hasWallToTheLeft(game, currentPosition)) | ||
return false; | ||
if (hasWallToTheLeft(game, { x: decrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
return true; | ||
} | ||
if (isLeftDownMove(currentPosition, move) && | ||
hasOpponentToTheLeft(game, currentPosition)) { | ||
if (!hasWallToTheLeft(game, { x: decrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
if (hasWallToTheLeft(game, currentPosition)) | ||
return false; | ||
if (hasWallBelow(game, { x: decrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
return true; | ||
} | ||
if (isLeftUpMove(currentPosition, move) && | ||
hasOpponentToTheLeft(game, currentPosition)) { | ||
if (!hasWallToTheLeft(game, { x: decrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
if (hasWallToTheLeft(game, currentPosition)) | ||
return false; | ||
if (hasWallAbove(game, { x: decrementHorizontalPiecePosition(x), y: y })) | ||
return false; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var shortestPath = function (game, player) { | ||
return aStar_1.aStar(game.pieceMatrix, game.wallMatrix, player); | ||
}; | ||
exports.isValidMove = void 0; | ||
var getTurn_1 = require("./getTurn"); | ||
var utils_1 = require("./utils"); | ||
var isValidMove = function (game, move) { | ||
// Handle wall moves | ||
var currentPosition = game.playerPositions[game.turn]; | ||
if (isWallMove(move)) { | ||
if (game.playerWallCounts[game.turn] < 1) | ||
var currentPosition = game.playerPositions[getTurn_1.getTurn(game)]; | ||
if (utils_1.isWallPosition(move)) { | ||
if (game.playerWallCounts[getTurn_1.getTurn(game)] < 1) | ||
return false; // Check that player has enough walls | ||
if (doesWallMoveOverlapExistingWall(game, move)) | ||
if (utils_1.doesWallMoveOverlapExistingWall(game, move)) | ||
return false; // Check that wall does not overlap other walls | ||
var gameWithUnvalidatedMove = exports.unvalidatedMove(game, move); | ||
if (shortestPath(gameWithUnvalidatedMove, game.turn) && | ||
shortestPath(gameWithUnvalidatedMove, getOppositePlayer(game.turn))) { | ||
var gameWithUnvalidatedMove = utils_1.unvalidatedMove(game, move); | ||
if (utils_1.shortestPath(gameWithUnvalidatedMove, getTurn_1.getTurn(game)) && | ||
utils_1.shortestPath(gameWithUnvalidatedMove, utils_1.getOppositePlayer(getTurn_1.getTurn(game)))) { | ||
return true; | ||
@@ -570,4 +21,4 @@ } | ||
} | ||
return isValidNormalMove(game, currentPosition, move); | ||
return utils_1.isValidNormalMove(game, utils_1.moveObjectToMove(currentPosition), move); | ||
}; | ||
exports.isValidMove = isValidMove; |
@@ -5,7 +5,8 @@ "use strict"; | ||
var isValidMove_1 = require("./isValidMove"); | ||
var utils_1 = require("./utils"); | ||
var makeMove = function (game, move) { | ||
if (!isValidMove_1.isValidMove(game, move)) | ||
return game; | ||
return isValidMove_1.unvalidatedMove(game, move); | ||
return utils_1.unvalidatedMove(game, move); | ||
}; | ||
exports.makeMove = makeMove; |
@@ -9,7 +9,15 @@ export declare type HorizontalPiecePosition = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i'; | ||
}; | ||
export declare type PieceMove = { | ||
export declare type PawnPosition = 'a1' | 'a2' | 'a3' | 'a4' | 'a5' | 'a6' | 'a7' | 'a8' | 'a9' | 'b1' | 'b2' | 'b3' | 'b4' | 'b5' | 'b6' | 'b7' | 'b8' | 'b9' | 'c1' | 'c2' | 'c3' | 'c4' | 'c5' | 'c6' | 'c7' | 'c8' | 'c9' | 'd1' | 'd2' | 'd3' | 'd4' | 'd5' | 'd6' | 'd7' | 'd8' | 'd9' | 'e1' | 'e2' | 'e3' | 'e4' | 'e5' | 'e6' | 'e7' | 'e8' | 'e9' | 'f1' | 'f2' | 'f3' | 'f4' | 'f5' | 'f6' | 'f7' | 'f8' | 'f9' | 'g1' | 'g2' | 'g3' | 'g4' | 'g5' | 'g6' | 'g7' | 'g8' | 'g9' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'h7' | 'h8' | 'h9' | 'i1' | 'i2' | 'i3' | 'i4' | 'i5' | 'i6' | 'i7' | 'i8' | 'i9'; | ||
export declare type PawnMove = PawnPosition; | ||
export declare type WallPosition = 'a1h' | 'a1v' | 'a2h' | 'a2v' | 'a3h' | 'a3v' | 'a4h' | 'a4v' | 'a5h' | 'a5v' | 'a6h' | 'a6v' | 'a7h' | 'a7v' | 'a8h' | 'a8v' | 'b1h' | 'b1v' | 'b2h' | 'b2v' | 'b3h' | 'b3v' | 'b4h' | 'b4v' | 'b5h' | 'b5v' | 'b6h' | 'b6v' | 'b7h' | 'b7v' | 'b8h' | 'b8v' | 'c1h' | 'c1v' | 'c2h' | 'c2v' | 'c3h' | 'c3v' | 'c4h' | 'c4v' | 'c5h' | 'c5v' | 'c6h' | 'c6v' | 'c7h' | 'c7v' | 'c8h' | 'c8v' | 'd1h' | 'd1v' | 'd2h' | 'd2v' | 'd3h' | 'd3v' | 'd4h' | 'd4v' | 'd5h' | 'd5v' | 'd6h' | 'd6v' | 'd7h' | 'd7v' | 'd8h' | 'd8v' | 'e1h' | 'e1v' | 'e2h' | 'e2v' | 'e3h' | 'e3v' | 'e4h' | 'e4v' | 'e5h' | 'e5v' | 'e6h' | 'e6v' | 'e7h' | 'e7v' | 'e8h' | 'e8v' | 'f1h' | 'f1v' | 'f2h' | 'f2v' | 'f3h' | 'f3v' | 'f4h' | 'f4v' | 'f5h' | 'f5v' | 'f6h' | 'f6v' | 'f7h' | 'f7v' | 'f8h' | 'f8v' | 'g1h' | 'g1v' | 'g2h' | 'g2v' | 'g3h' | 'g3v' | 'g4h' | 'g4v' | 'g5h' | 'g5v' | 'g6h' | 'g6v' | 'g7h' | 'g7v' | 'g8h' | 'g8v' | 'h1h' | 'h1v' | 'h2h' | 'h2v' | 'h3h' | 'h3v' | 'h4h' | 'h4v' | 'h5h' | 'h5v' | 'h6h' | 'h6v' | 'h7h' | 'h7v' | 'h8h' | 'h8v'; | ||
export declare type Board = Record<PawnPosition, Player | null> & Record<WallPosition, boolean>; | ||
export declare type WallMove = WallPosition; | ||
export declare type Move = PawnMove | WallMove; | ||
export declare type Position = PawnPosition | WallPosition; | ||
export declare type PawnMoveObject = { | ||
x: HorizontalPiecePosition; | ||
y: VerticalPiecePosition; | ||
}; | ||
export declare type WallMove = { | ||
export declare type WallOrientation = 'h' | 'v'; | ||
export declare type WallMoveObject = { | ||
x: HorizontalWallPosition; | ||
@@ -19,3 +27,3 @@ y: VerticalWallPosition; | ||
}; | ||
export declare type Move = PieceMove | WallMove; | ||
export declare type MoveObject = PawnMoveObject | WallMoveObject; | ||
export declare type Player = 1 | 2; | ||
@@ -27,9 +35,8 @@ export declare type PlayerMatrix = Record<HorizontalPiecePosition, Record<VerticalPiecePosition, Player | 0>>; | ||
}>>; | ||
declare type History = Record<Player, Move[]>; | ||
declare type WallCount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; | ||
export declare type Game = { | ||
board: Board; | ||
pieceMatrix: PlayerMatrix; | ||
wallMatrix: WallMatrix; | ||
history: History; | ||
turn: Player; | ||
history: Move[]; | ||
playerPositions: Record<Player, PiecePosition & { | ||
@@ -36,0 +43,0 @@ previousPosition?: PiecePosition; |
{ | ||
"name": "quoridor", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A JavaScipt Quoridor library for move validation etc.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,3 +0,5 @@ | ||
### Installation | ||
# quoridor.js | ||
## Installation | ||
```bash | ||
@@ -7,4 +9,43 @@ npm install quoridor | ||
### Publishing a new version | ||
## API | ||
### createGameFromMoves: (moves: Move[]) => Game | ||
Generates a game from an array of moves. Does not validate the moves. | ||
```TypeScript | ||
import { createGameFromMoves, getAsciiRepresentation } from 'quoridor'; | ||
const game = createGameFromMoves(['e2', 'e8', 'd7v']); | ||
console.log(getAsciiRepresentation(game)); | ||
┌───╫───╫───╫───╫───╫───╫───╫───╫───╫───╫───┐ | ||
│ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ │ | ||
│ ┌───┬───┬───┬───┬───┬───┬───┬───┬───┐ │ | ||
│ 9 │ │ │ │ │ │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 8 │ │ │ │ │ │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 7 │ │ │ │ ║ 2 │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───╫───┼───┼───┼───┼───┤ │ | ||
│ 6 │ │ │ │ ║ │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 5 │ │ │ │ │ │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 4 │ │ │ │ │ │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 3 │ │ │ │ │ 1 │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 2 │ │ │ │ │ │ │ │ │ │ │ | ||
│ ├───┼───┼───┼───┼───┼───┼───┼───┼───┤ │ | ||
│ 1 │ │ │ │ │ │ │ │ │ │ │ | ||
│ └───┴───┴───┴───┴───┴───┴───┴───┴───┘ │ | ||
│ ║ A ║ B ║ C ║ D ║ E ║ F ║ G ║ H ║ I │ | ||
└───╫───╫───╫───╫───╫───╫───╫───╫───╫───────┘ | ||
``` | ||
## Publishing a new version | ||
Check that linting, formatting, build and tests pass | ||
@@ -11,0 +52,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80297
29
2186
70
1