This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This solves the maze using regular expressions, because I am too | |
#lazy to worry about arrays, darn it. It is inefficient as heck and designed | |
#purely to demonstrate perversion of sound CS principals. | |
# | |
# Code by Patrick McKenzie, 2010. I release this work unto the public domain. | |
class Maze | |
def initialize(maze_string) | |
@width = maze_string.split("\n")[0].length #width of maze in characters | |
@guts = maze_string.gsub("\n", "") #maze as a flat string -- no newlines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Maze | |
Cardinals = Proc.new{|(x,y)| [[x-1,y],[x,y+1],[x+1,y],[x,y-1]].select{|c| c.min >= 0}} | |
MazeSeperator, MazeStart, MazeWall, MazeEnd, MazeOOB = "\n", 'A', '#', 'B', nil | |
Infinity = 1.0/0 | |
X, Y = 0, 1 | |
def initialize(maze) | |
raise ArgumentError, 'No end point' unless maze.include? MazeEnd | |
raise ArgumentError, 'No start point' unless maze.include? MazeStart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Maze | |
attr_reader :maze_array | |
def initialize(maze) | |
@maze_array = maze.split("\n") #create array of lines of the string | |
@maze_array.map! { |line| line.chars.to_a } #converts array to 2-d array of characters in maze | |
raise ArgumentError, "All lines must be of the same length", caller if !@maze_array.all? {|line| line.length == @maze_array[0].length} | |
@height = @maze_array.length #store the height of the maze in an instance variable | |
@width = @maze_array[0].length #store the length of the maze in an instance variable (assumes each line is the same length - should really test for this if being thorough) | |
@maze_array.each_with_index do |line, index1| #search array to find start and end of maze | |
line.each_with_index do |char, index2| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
alias_method :is_a, :== | |
alias_method :is_an, :== | |
end | |
class Maze | |
NAVIGABLE = ' ' | |
WALL = '#' | |
POINT_A = 'A' |