Method: Amaze::Script#run

Defined in:
lib/amaze/script.rb

#run(args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/amaze/script.rb', line 133

def run args
  parser.parse!(args)
  
  initialize_random_seed
  
  # Run the algorithm on the grid
  if visualize?
    algorithm.on grid do |stat|
      # print the maze
      ascii = factory.create_ascii_formatter grid,
        ascii_options(path_color: :blue, path_cells: stat.current)
        
      puts ascii.render
      
      puts stat.info if stat.info
      sleep algorithm.speed
      sleep 1 if options[:visualize] == :autopause && stat.pause?

      # wait for keystroke ?
      if (options[:visualize] == :pause && stat.pause? || options[:visualize] == :step)
        case read_char
        when "\e"
          break
        when "r"
          options[:visualize] = :run
        end
      end
    end
  else
    algorithm.on grid
  end
  
  ascii_runtime_options = {}
  image_runtime_options = {}
  
  # Calculate the distances from a given start cell
  if distances?
    distances = start_cell.distances
    ascii_runtime_options[:distances] = distances
    image_runtime_options[:distances] = distances
  end

  # And the solution to a given end cell
  if solution?
    distances = start_cell.distances.path_to finish_cell
    ascii_runtime_options[:path_cells] = distances.cells
    image_runtime_options[:path_cells] = distances.cells
    image_runtime_options[:path_start] = start_cell
    image_runtime_options[:path_finish] = finish_cell
    path_length = distances[finish_cell]
  end
  
  if longest?
    new_start, distance = start_cell.distances.max
    new_distances = new_start.distances
    new_finish, distance = new_distances.max
    distances = new_distances.path_to new_finish
    image_runtime_options[:distances] = new_distances if distances?
    ascii_runtime_options[:path_cells] = distances.cells
    image_runtime_options[:path_cells] = distances.cells
    image_runtime_options[:path_start] = new_start
    image_runtime_options[:path_finish] = new_finish
    path_length = distance
  end

  # Render the maze, set defaults for missing options
  if ascii?
    ascii = factory.create_ascii_formatter grid, ascii_options(ascii_runtime_options)
    puts ascii.render
  end
  
  puts algorithm.status
  puts "Dead ends: #{grid.deadends.size} of #{grid.size} (#{(100.to_f / grid.size * grid.deadends.size).to_i}%)"
  puts "Path length: #{path_length}" if path_length
  puts "Random seed: #{seed}"

  if image?
    image = factory.create_image_formatter grid,
      image_options(image_runtime_options)
    image.render
    
    # TODO: write multiple images with solution and distances
    #       or a psd file with layers
    
    image.write "maze.png"
    puts "Maze 'maze.png' saved."
  end
end