Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const isEqual = (array1, array2) => {
- if (array1.length !== array2.length) return false;
- let equal = true;
- array1.forEach((item, x) => {
- if (item !== array2[x]) equal = false;
- });
- return equal;
- }
- // We are checking for invalid coordinates, coords must follow this statement
- // 0 < coord < 9
- // If they don't follow that we delete the coordinate
- const isValid = (moves) => {
- // Moves is an array of moves of possible positions
- // Each position holds two coordinates
- return moves.filter(position =>
- position[0] > 0 && position[0] < 9
- && position[1] > 0 && position[1] < 9);
- }
- // This function will use the current position and display
- // all the possible moves for the given knight
- const possibleMoves = (position) => {
- const moves = [
- // [X, Y]
- // FIRST UP
- // THEN LEFT
- [position[0] - 1, position[1] + 2],
- // THEN RIGHT
- [position[0] + 1, position[1] + 2],
- // FIRST DOWN
- // THEN LEFT
- [position[0] - 1, position[1] - 2],
- // THEN RIGHT
- [position[0] + 1, position[1] - 2],
- // FIRST LEFT
- // THEN UP
- [position[0] - 2, position[1] + 1],
- // THEN DOWN
- [position[0] - 2, position[1] - 1],
- // FIRST RIGHT
- // THEN UP
- [position[0] + 2, position[1] + 1],
- // THEN DOWN
- [position[0] + 2, position[1] - 1]
- ]
- return moves;
- }
- const createBoard = () => {
- let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
- let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
- let board = {};
- // Row
- for (let i = 0; i < 8; i++) {
- // Column
- for (let x = 0; x < 8; x++) {
- board[`${letters[i]}${numbers[x]}`] = {
- position: [numbers[i], numbers[x]],
- distance: Infinity,
- }
- }
- }
- return board;
- }
- // Here i will try to wire up everything into djikstras shortest path algorithm
- const shortestPath = (start, end) => {
- let visited = start;
- let unvisited = createBoard();
- for(position in unvisited) {
- if(isEqual(unvisited[position], visited) === true) delete unvisited[position];
- }
- // Mark all nodes unvisted
- // Assign all nodes an tentative distance value
- // Our currentNode (starting point) is 0, the rest is infinity
- // tentative distance is the shortest path between node N and the starting node
- // For the current node consider all it's neighbors through the current node
- }
- // shortestPath([8,7], [1, 1]);
- // Three Moves
- // 2 with Some Invalid
- // 1 Completely Valid
- // [1, 1], [8, 8], [3, 3]
- // let test = [[1, 1], [8, 8], [3, 3]];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement