0

So far my shortest path method will stop when it reaches the goal position, printing out everything it did along the way. I would like to know how I could go about implementing parent positions so I can print the path along with the goal. This is an assignment.

class Knight 
  attr_accessor :x, :y, :prev_position, :moves
    def initialize(position) 
      @x = position[0]
      @y = position[1]
      @prev_position = nil
      @moves = [
        [-1,-2],
        [-2,-1],
        [-2,+1],
        [-1,+2],
        [+1,-2],
        [+2,-1],
        [+2,+1],
        [+1,+2]]
    end

    def possible
      move_list = Array.new
      @moves.each do |moves| 
        x = @x + moves[0] 
        y = @y + moves[1]
          if x.between?(0,7)  
            if y.between?(0,7)
            move_list << [x,y]  
            end
          end 
      end 
       move_list
      end 

    end 

    def shortest_path(position,goal)
      paths = Array.new
      @start_knight = Knight.new(position)
        until @start_knight.x == goal[0] and
          @start_knight.y == goal[1]
            @start_knight.possible.each do |p| paths << p end
              shifted = paths.shift
              @start_knight.x = shifted[0] 
              @start_knight.y = shifted[1]
              puts "[#{@start_knight.x},#{@start_knight.y}]"
        end 
    end

    shortest_path([0,0],[7,7])
Imran Ali
  • 2,223
  • 2
  • 28
  • 41
Jacob Moore
  • 277
  • 2
  • 12
  • I would find this code would more readable if you cleaned up the indentation. Then I would look at what is left in the "paths" array when the "until" condition has been satisified (after the associated "end"); Is this a stack of the moves which produced the current position? If so, you win. – Peter Sep 13 '17 at 02:41
  • I seen the indentation comment coming a mile away, not sure where I go wrong. 2 spaces every time you do something new? And yes I'm using the array as a stack and using shift to get the next position. What do you mean by "you win"? – Jacob Moore Sep 13 '17 at 02:44
  • If you have the stack of moves, then print the contents of the stack in order, and you get the path. You wanted to "print the path along with the goal", right? – Peter Sep 13 '17 at 02:49
  • In general, with any shortest path algorithm you will be improving upon the shortest distance from an origin to each of the graph's other nodes. Whenever you find a shorter distance, you need to update two quantities: the shortest known to distance from the origin to that node and the previous node on the associated path. You might do that with a hash for each node: `{ path_lenght: 20, prev_node: }`. Then when you find a shortest path you can backtrack to construct the path. – Cary Swoveland Sep 13 '17 at 02:54
  • Every possible move the knight can make gets put into the stack. so if my starting position was [0,0] the possible moves put into the stack would be [2,1] and [1,2]. – Jacob Moore Sep 13 '17 at 02:55
  • @cary Swoveland, I am not implementing any specific algorithm that I know of, I just had an idea and started typing. The first time the goal is found would be the shortest distance. – Jacob Moore Sep 13 '17 at 02:57
  • Sorry, I just noticed it was a Knight's tour. I therefore deleted my first sentence but I think the rest still applies. – Cary Swoveland Sep 13 '17 at 02:58
  • My method finds the path, it just doesn't keep track of which previous positions it was linked to. – Jacob Moore Sep 13 '17 at 03:08
  • https://github.com/jmooree30/Knights_Travels/blob/master/knights_travels.rb Figured it out. – Jacob Moore Sep 14 '17 at 03:04

0 Answers0