@cocacola-lover/knight_path_finder
Version:
Iterative path finding Algorithms for knight on a chessboard
211 lines • 8.38 kB
JavaScript
var ChessPointers;
(function (ChessPointers) {
class BasicPointer {
constructor(x, y, board) {
this.x = x;
this.y = y;
this.board = board;
}
at() {
return this.board[this.x][this.y];
}
getNeighbours() {
const ans = [];
const moveOne = [1, -1];
moveOne.forEach((x) => {
if (this.isWithinBounds(this.x + x, this.y))
ans.push(new BasicPointer(this.x + x, this.y, this.board));
});
moveOne.forEach((y) => {
if (this.isWithinBounds(this.x, this.y + y))
ans.push(new BasicPointer(this.x, this.y + y, this.board));
});
return ans;
}
getPassableNeighbours() {
return this.getNeighbours().filter((pointer) => pointer.at().isPassable);
}
toString() {
return `${this.x},${this.y}`;
}
static areSame(a, b) {
return a.board === b.board && a.x === b.x && a.y === b.y;
}
isWithinBounds(x, y) {
if (this.board[x] === undefined)
return false;
if (this.board[x][y] === undefined)
return false;
return true;
}
}
ChessPointers.BasicPointer = BasicPointer;
class KnightPointer extends BasicPointer {
getNeighbours() {
const ans = [];
const moveOne = [1, -1];
const moveTwo = [2, -2];
moveOne.forEach((y) => {
moveTwo.forEach((x) => {
if (this.isWithinBounds(this.x + x, this.y + y))
ans.push(new KnightPointer(this.x + x, this.y + y, this.board));
if (this.isWithinBounds(this.x + y, this.y + x))
ans.push(new KnightPointer(this.x + y, this.y + x, this.board));
});
});
return ans;
}
}
ChessPointers.KnightPointer = KnightPointer;
class KingPointer extends BasicPointer {
getNeighbours() {
const ans = [];
const moveOne = [1, -1, 0];
moveOne.forEach((y) => {
moveOne.forEach((x) => {
if (this.isWithinBounds(this.x + x, this.y + y))
ans.push(new KingPointer(this.x + x, this.y + y, this.board));
if (this.isWithinBounds(this.x + y, this.y + x))
ans.push(new KingPointer(this.x + y, this.y + x, this.board));
});
});
return ans;
}
}
ChessPointers.KingPointer = KingPointer;
class BishopPointer extends BasicPointer {
getNeighbours() {
const ans = [];
const moveDiagonal = [[1, 1], [-1, -1], [-1, 1], [1, -1]];
moveDiagonal.forEach((pair) => {
let [x, y] = pair;
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y + y * i); i++)
ans.push(new BishopPointer(this.x + x * i, this.y + y * i, this.board));
});
return ans;
}
getPassableNeighbours() {
const ans = [];
const moveDiagonal = [[1, 1], [-1, -1], [-1, 1], [1, -1]];
moveDiagonal.forEach((pair) => {
let [x, y] = pair;
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y + y * i); i++) {
const newPointer = new BishopPointer(this.x + x * i, this.y + y * i, this.board);
if (newPointer.at().isPassable)
ans.push(newPointer);
else
break;
}
});
return ans;
}
}
ChessPointers.BishopPointer = BishopPointer;
class RookPointer extends BasicPointer {
getNeighbours() {
const ans = [];
const moveOne = [1, -1];
moveOne.forEach((x) => {
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y); i++)
ans.push(new RookPointer(this.x + x, this.y, this.board));
});
moveOne.forEach((y) => {
for (let i = 1; this.isWithinBounds(this.x, this.y + y * i); i++)
ans.push(new RookPointer(this.x, this.y + y * i, this.board));
});
return ans;
}
getPassableNeighbours() {
const ans = [];
const moveOne = [1, -1];
moveOne.forEach((x) => {
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y); i++) {
const newPointer = new RookPointer(this.x + x * i, this.y, this.board);
if (newPointer.at().isPassable)
ans.push(newPointer);
else
break;
}
});
moveOne.forEach((y) => {
for (let i = 1; this.isWithinBounds(this.x, this.y + y * i); i++) {
const newPointer = new RookPointer(this.x, this.y + y * i, this.board);
if (newPointer.at().isPassable)
ans.push(newPointer);
else
break;
}
});
return ans;
}
}
ChessPointers.RookPointer = RookPointer;
class PawnPointer extends BasicPointer {
getNeighbours() {
const ans = [];
if (this.isWithinBounds(this.x, this.y + 1))
ans.push(new PawnPointer(this.x, this.y + 1, this.board));
return ans;
}
}
ChessPointers.PawnPointer = PawnPointer;
class QueenPointer extends BasicPointer {
getNeighbours() {
const ans = [];
const moveDiagonal = [[1, 1], [-1, -1], [-1, 1], [1, -1]];
moveDiagonal.forEach((pair) => {
let [x, y] = pair;
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y + y * i); i++)
ans.push(new QueenPointer(this.x + x * i, this.y + y * i, this.board));
});
const moveOne = [1, -1];
moveOne.forEach((x) => {
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y); i++)
ans.push(new QueenPointer(this.x + x, this.y, this.board));
});
moveOne.forEach((y) => {
for (let i = 1; this.isWithinBounds(this.x, this.y + y * i); i++)
ans.push(new QueenPointer(this.x, this.y + y * i, this.board));
});
return ans;
}
getPassableNeighbours() {
const ans = [];
const moveDiagonal = [[1, 1], [-1, -1], [-1, 1], [1, -1]];
moveDiagonal.forEach((pair) => {
let [x, y] = pair;
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y + y * i); i++) {
const newPointer = new QueenPointer(this.x + x * i, this.y + y * i, this.board);
if (newPointer.at().isPassable)
ans.push(newPointer);
else
break;
}
});
const moveOne = [1, -1];
moveOne.forEach((x) => {
for (let i = 1; this.isWithinBounds(this.x + x * i, this.y); i++) {
const newPointer = new QueenPointer(this.x + x * i, this.y, this.board);
if (newPointer.at().isPassable)
ans.push(newPointer);
else
break;
}
});
moveOne.forEach((y) => {
for (let i = 1; this.isWithinBounds(this.x, this.y + y * i); i++) {
const newPointer = new QueenPointer(this.x, this.y + y * i, this.board);
if (newPointer.at().isPassable)
ans.push(newPointer);
else
break;
}
});
return ans;
}
}
ChessPointers.QueenPointer = QueenPointer;
})(ChessPointers || (ChessPointers = {}));
;
export default ChessPointers;
//# sourceMappingURL=chess_pointer.js.map