0

我在解决我的 Ruby 任务时遇到问题。我应该:

1.一旦用户使用鼠标左键创建了至少一条从屏幕一侧到另一侧的路径,用户应该能够在屏幕最左侧的黄色单元格上右键单击鼠标屏幕。

2.然后程序应使用递归搜索() 函数进行搜索,以在用户创建的路径之一之后找到屏幕右侧的路径。

3.路径上的每个单元格应显示为红色。

通过迷宫找到的路径可能类似于:

它应该是什么样子

这是我到目前为止所拥有的,任何人都可以帮助我解决问题:

    def mouse_over_cell(mouse_x, mouse_y)
    if mouse_x <= CELL_DIM
      cell_x = 0
    else
      cell_x = (mouse_x / CELL_DIM).to_i
    end

    if mouse_y <= CELL_DIM
      cell_y = 0
    else
      cell_y = (mouse_y / CELL_DIM).to_i
    end

    [cell_x, cell_y]
  end

  # start a recursive search for paths from the selected cell
  # it searches till it hits the East 'wall' then stops
  # it does not necessarily find the shortest path

  # Completing this function is NOT NECESSARY for the Maze Creation task
  # complete the following for the Maze Search task - after
  # we cover Recusion in the lectures.

  # But you DO need to complete it later for the Maze Search task

  def search(cell_x ,cell_y)

    dead_end = false
    path_found = Array.new

    if (cell_x == ((MAP_WIDTH / CELL_DIM) - 1))
      if (@columns.length > 0) # debug
        puts "End of one path x: " + cell_x.to_s + " y: " + cell_y.to_s
      end
    [[cell_x,cell_y]]  # We are at the east wall - exit
    else

      north_path = nil
      west_path = nil
      east_path = nil
      south_path = nil

      if (@columns.length > 0) # debug
        puts "Searching. In cell x: " + cell_x.to_s + " y: " + cell_y.to_s
      end

      # INSERT MISSING CODE HERE!! You need to have 4 'if' tests to
      # check each surrounding cell. Make use of the attributes for
      # cells such as vacant, visited and on_path.
      # Cells on the outer boundaries will always have a nil on the
      # boundary side
      

      @north = MAP_HEIGHT / CELL_DIM
      @south = MAP_HEIGHT / CELL_DIM - 1
      @west = MAP_WIDTH / CELL_DIM
      @east = MAP_WIDTH / CELL_DIM - 1


    
      if ( @north == (@vacant = true)) 
        north_path = [cell_x, cell_y]
        @visited[cell_x,cell_y] = true
      end
      if (@south == (@vacant = true))  
        south_path = [cell_x, cell_y]
        @visited[cell_x,cell_y] = true
      end
      if (@west == (@vacant = true)) 
        west_path = [cell_x, cell_y]
        @visited[cell_x,cell_y] = true
      end
      if (@east == (@vacant = true))  
        east_path = [cell_x, cell_y]
        @visited[cell_x,cell_y] = true
      end
    

      path = Array.new

      # pick one of the possible paths that is not nil (if any):
      index = 0
      while (index < @columns.length)
        if (north_path != nil)
          path = north_path
        elsif (south_path != nil)
          path = south_path
        elsif (east_path != nil)
          path = east_path
        elsif (west_path != nil)
          path = west_path
        end
      index += 1
      end

      

     

      # A path was found:
      if (path != nil)
        if (path.length > 0) # debug
          puts "Added x: " + cell_x.to_s + " y: " + cell_y.to_s
        end
        [[cell_x,cell_y]].concat(path)
      else
        if (path.length > 0) # debug
          puts "Dead end x: " + cell_x.to_s + " y: " + cell_y.to_s
        end
        nil  # dead end
      end
    end
  end

    # Reacts to button press
    # left button marks a cell vacant
    # Right button starts a path search from the clicked cell
    def button_down(id)
      case id
       when Gosu::MsLeft
          cell = mouse_over_cell(mouse_x, mouse_y)
          if (@columns.length > 0) # debug
           puts("Cell clicked on is x: " + cell[0].to_s + " y: " + cell[1].to_s)
          end
         @columns[cell[0]][cell[1]].vacant = true
       when Gosu::MsRight
        cell = mouse_over_cell(mouse_x, mouse_y)
        @path = search(cell[0],cell[1])
      end
    end

     # This will walk along the path setting the on_path for each cell
     # to true. Then draw checks this and displays them a red color.
     def walk(path)
       index = path.length
       count = 0
       while (count < index)
        cell = path[count]
        @columns[cell[0]][cell[1]].on_path = true
        count += 1
       end
       @path << path
     end

    def update
    if (@path != nil)
      if (ARGV.length > 0) # debug
        puts "Displaying path"
        puts @path.to_s
      end
        walk(@path)
        @path = nil
      end
    end

    # Draw (or Redraw) the window
    # This is procedure i.e the return value is 'undefined'
    def draw
      index = 0
      x_loc = 0;
      y_loc = 0;

      x_cell_count = MAP_WIDTH / CELL_DIM
      y_cell_count = MAP_HEIGHT / CELL_DIM

      column_index = 0
      while (column_index < x_cell_count)
       row_index = 0
       while (row_index < y_cell_count)

        if (@columns[column_index][row_index].vacant)
          color = Gosu::Color::YELLOW
        else
          color = Gosu::Color::GREEN
        end
        if (@columns[column_index][row_index].on_path)
          color = Gosu::Color::RED
        end

        Gosu.draw_rect(column_index * CELL_DIM, row_index * CELL_DIM, CELL_DIM, CELL_DIM, color, 
        ZOrder::TOP, mode=:default)

        row_index += 1
      end
      column_index += 1
      end
      end
      end

4

0 回答 0