Advertisement
ptownhero

Am I on the right track of this question?

Nov 2nd, 2022
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const isEqual = (array1, array2) => {
  2.     if (array1.length !== array2.length) return false;
  3.  
  4.     let equal = true;
  5.     array1.forEach((item, x) => {
  6.         if (item !== array2[x]) equal = false;
  7.     });
  8.  
  9.     return equal;
  10. }
  11.  
  12. // We are checking for invalid coordinates, coords must follow this statement
  13. // 0 < coord < 9
  14. // If they don't follow that we delete the coordinate
  15. const isValid = (moves) => {
  16.     // Moves is an array of moves of possible positions
  17.     // Each position holds two coordinates
  18.     return moves.filter(position =>
  19.         position[0] > 0 && position[0] < 9
  20.         && position[1] > 0 && position[1] < 9);
  21.     }
  22.  
  23.    
  24. // This function will use the current position and display
  25. // all the possible moves for the given knight
  26. const possibleMoves = (position) => {
  27.     const moves = [
  28.         // [X, Y]
  29.         // FIRST UP
  30.         // THEN LEFT
  31.         [position[0] - 1, position[1] + 2],
  32.         // THEN RIGHT
  33.         [position[0] + 1, position[1] + 2],
  34.  
  35.         // FIRST DOWN
  36.         // THEN LEFT
  37.         [position[0] - 1, position[1] - 2],
  38.         // THEN RIGHT
  39.         [position[0] + 1, position[1] - 2],
  40.  
  41.         // FIRST LEFT
  42.         // THEN UP
  43.         [position[0] - 2, position[1] + 1],
  44.         // THEN DOWN
  45.         [position[0] - 2, position[1] - 1],
  46.  
  47.         // FIRST RIGHT
  48.         // THEN UP
  49.         [position[0] + 2, position[1] + 1],
  50.         // THEN DOWN
  51.         [position[0] + 2, position[1] - 1]
  52.     ]
  53.     return moves;
  54. }
  55.  
  56. const createBoard = () => {
  57.     let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
  58.     let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
  59.     let board = {};
  60.  
  61.     // Row
  62.     for (let i = 0; i < 8; i++) {
  63.         // Column
  64.         for (let x = 0; x < 8; x++) {
  65.             board[`${letters[i]}${numbers[x]}`] =  {
  66.                 position: [numbers[i], numbers[x]],
  67.                 distance: Infinity,
  68.             }
  69.         }
  70.     }
  71.     return board;
  72. }
  73.  
  74. // Here i will try to wire up everything into djikstras shortest path algorithm
  75. const shortestPath = (start, end) => {
  76.     let visited = start;
  77.     let unvisited = createBoard();
  78.    
  79.     for(position in unvisited) {
  80.         if(isEqual(unvisited[position], visited) === true)  delete unvisited[position];
  81.     }
  82.    
  83.     // Mark all nodes unvisted
  84.    
  85.  
  86.     // Assign all nodes an tentative distance value
  87.     // Our currentNode (starting point) is 0, the rest is infinity
  88.     // tentative distance is the shortest path between node N and the starting node
  89.    
  90.  
  91.     // For the current node consider all it's neighbors through the current node
  92.    
  93.  
  94.  
  95. }
  96.  
  97. // shortestPath([8,7], [1, 1]);
  98.  
  99. // Three Moves
  100. // 2 with Some Invalid
  101. // 1 Completely Valid
  102. // [1, 1], [8, 8], [3, 3]
  103. // let test = [[1, 1], [8, 8], [3, 3]];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement