Class: Maze

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/maze.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*dimensions) ⇒ Maze

Returns a new instance of Maze.



54
55
56
57
58
59
# File 'lib/maze/maze.rb', line 54

def initialize(*dimensions)
	@dimensions = dimensions.map { |d| d.to_i }.freeze
	@raw_dimensions = @dimensions.map { |d| 1 + 2*d }.freeze
	allocate 0
	@hash = @dimensions.reduce(""){ |accum, d| "#{accum}#{d}" }.to_i
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



52
53
54
# File 'lib/maze/maze.rb', line 52

def dimensions
  @dimensions
end

#matrixObject

Returns the value of attribute matrix.



51
52
53
# File 'lib/maze/maze.rb', line 51

def matrix
  @matrix
end

Class Method Details

.from_array(matrix) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/maze/maze.rb', line 61

def self.from_array(matrix)
	dimensions = []
	matrix_aux = matrix
	while matrix_aux.is_a? Array
		dimensions.push matrix_aux.length
		matrix_aux = matrix_aux[0]
	end
	maze = Maze.new *dimensions
	maze.matrix = Marshal.load(Marshal.dump(matrix)) # deep clone
	maze
end

Instance Method Details

#between_cells(cell_a, cell_b) ⇒ Object



128
129
130
131
132
# File 'lib/maze/maze.rb', line 128

def between_cells(cell_a, cell_b)
	indices_a = coords_to_indices *cell_a.coords
	indices_b = coords_to_indices *cell_b.coords
       indices_a.zip(indices_b).map { |pair| (pair[0] + pair[1]) / 2.0 }
end

#cell(*coords) ⇒ Object



118
119
120
121
122
# File 'lib/maze/maze.rb', line 118

def cell(*coords)
	params = coords.clone
	params.unshift self
	MazeCell.new *params
end

#cellsObject



124
125
126
# File 'lib/maze/maze.rb', line 124

def cells
    evaluate_indices(@dimensions).map { |coord| cell *coord }
end

#connect_cells(cell_a, cell_b) ⇒ Object



134
135
136
# File 'lib/maze/maze.rb', line 134

def connect_cells(cell_a, cell_b)
    set_between_cells cell_a, cell_b, 0
end

#coords_to_indices(*coords) ⇒ Object



81
82
83
# File 'lib/maze/maze.rb', line 81

def coords_to_indices(*coords)
	coords.map { |c| 1 + 2 * c }
end

#disconnect_cells(cell_a, cell_b) ⇒ Object



138
139
140
# File 'lib/maze/maze.rb', line 138

def disconnect_cells(cell_a, cell_b)
    set_between_cells cell_a, cell_b, 1
end

#get_raw_value(*indices) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/maze/maze.rb', line 85

def get_raw_value(*indices)
	matrix_aux = @matrix
	indices.each_with_index do |c|
		matrix_aux = matrix_aux[c]
	end
	matrix_aux
end

#get_value(*coords) ⇒ Object



108
109
110
# File 'lib/maze/maze.rb', line 108

def get_value(*coords)
	get_raw_value *self.coords_to_indices(*coords)
end

#hashObject



146
147
148
# File 'lib/maze/maze.rb', line 146

def hash
	@hash
end

#inspectObject



150
151
152
153
154
155
156
157
# File 'lib/maze/maze.rb', line 150

def inspect
	if not @inspect
		d_index = 0
		dimension_list = @dimensions.map { |d| "@d#{d_index+=1}=#{d}" }
		@inspect = "#<#{self.class}: #{dimension_list.join(', ')}>"
	end
	@inspect
end

#set_raw_value(*indices) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/maze/maze.rb', line 93

def set_raw_value(*indices)
	matrix_aux = @matrix
	indices = indices.clone
	value = indices.pop
	indices.each_with_index do |c, c_index|
		matrix_aux[c] = value if c_index == indices.length - 1
		matrix_aux = matrix_aux[c]
	end
	matrix_aux
end

#set_raw_value_all(value) ⇒ Object



104
105
106
# File 'lib/maze/maze.rb', line 104

def set_raw_value_all(value)
	allocate value
end

#set_value(*coords) ⇒ Object



112
113
114
115
116
# File 'lib/maze/maze.rb', line 112

def set_value(*coords)
       coords = coords.clone
	value = coords.pop
	set_raw_value *self.coords_to_indices(*coords).push(value)
end

#to_jsonObject



142
143
144
# File 'lib/maze/maze.rb', line 142

def to_json
	JSON.unparse @matrix
end

#total_cellsObject



73
74
75
# File 'lib/maze/maze.rb', line 73

def total_cells
    dimensions.reduce(1) { |accum, d| accum*d }
end

#total_rawObject



77
78
79
# File 'lib/maze/maze.rb', line 77

def total_raw
    dimensions.reduce(1) { |accum, d| accum*(1 + 2*d) }
end