summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-04-21 20:39:35 +0200
committerNoah Loomans <noahloomans@gmail.com>2018-04-21 20:39:35 +0200
commit811e453013ec548a3750c1fcb39f36fd4cd77dcf (patch)
treeed9166ea4aee8619fc979a45e9fefb7b68ae863d
parent6be27e99f15c5668f260ef7fe874400e43616c07 (diff)
Split program into multiple files
-rw-r--r--main.rb122
-rw-r--r--maze.rb81
-rw-r--r--pos.rb34
3 files changed, 118 insertions, 119 deletions
diff --git a/main.rb b/main.rb
index 3f0d701..57f4889 100644
--- a/main.rb
+++ b/main.rb
@@ -1,121 +1,5 @@
-DIRS = [ :up, :right, :down, :left ]
-
-Pos = Struct.new(:x, :y) do
- def move(dir)
- case dir
- when :up
- Pos.new(x, y - 1)
- when :right
- Pos.new(x + 1, y)
- when :down
- Pos.new(x, y + 1)
- when :left
- Pos.new(x - 1, y)
- end
- end
-
- def dir_from(pos)
- diff_x = x - pos.x
- diff_y = y - pos.y
-
- case Pos.new(diff_x, diff_y)
- when Pos.new(0, -1)
- :up
- when Pos.new(1, 0)
- :right
- when Pos.new(0, 1)
- :down
- when Pos.new(-1, 0)
- :left
- end
- end
-
- def to_s()
- "(#{x}, #{y})"
- end
-end
-
-Tile = Struct.new(:pos, :up, :right, :down, :left)
-
-class Maze
- attr_reader :width
- attr_reader :height
-
- def initialize(width, height)
- @width = width
- @height = height
- @h_walls = Array.new(@width) { Array.new(@height + 1, true) }
- @v_walls = Array.new(@width + 1) { Array.new(@height, true) }
- end
-
- def inBounds?(pos)
- x = pos.x
- y = pos.y
-
- x >= 0 && y >= 0 && x < @width && y < @height
- end
-
- def get(pos)
- raise IndexError unless inBounds?(pos)
-
- x = pos.x
- y = pos.y
-
- Tile.new(pos, @h_walls[x][y], @v_walls[x + 1][y], @h_walls[x][y + 1], @v_walls[x][y])
- end
-
- def neighbors(pos)
- neighbors = []
-
- DIRS.each do |dir|
- neighbors.push(pos.move(dir)) if inBounds?(pos.move(dir))
- end
-
- neighbors
- end
-
- def set(pos, dir, state)
- raise IndexError if pos.x >= @width
- raise IndexError if pos.y >= @height
-
- case dir
- when :up
- @h_walls[pos.x][pos.y] = state
- when :right
- @v_walls[pos.x + 1][pos.y] = state
- when :down
- @h_walls[pos.x][pos.y + 1] = state
- when :left
- @v_walls[pos.x][pos.y] = state
- end
- end
-
- def to_s
- drawingField = Array.new(@height * 2 + 1) { " " * (@width * 2 + 1) }
-
- @v_walls.each_index do |x|
- @v_walls[x].each_index do |y|
- next if @v_walls[x][y] == false
-
- drawingField[y * 2][x * 2] = "█"
- drawingField[y * 2 + 1][x * 2] = "█"
- drawingField[y * 2 + 2][x * 2] = "█"
- end
- end
-
- @h_walls.each_index do |x|
- @h_walls[x].each_index do |y|
- next if @h_walls[x][y] == false
-
- drawingField[y * 2][x * 2] = "█"
- drawingField[y * 2][x * 2 + 1] = "█"
- drawingField[y * 2][x * 2 + 2] = "█"
- end
- end
-
- drawingField.join("\n")
- end
-end
+require_relative './pos.rb'
+require_relative './maze.rb'
class MazeGenerator
attr_reader :maze
@@ -152,6 +36,6 @@ class MazeGenerator
end
end
-mazeGenerator = MazeGenerator.new(15, 15)
+mazeGenerator = MazeGenerator.new(118, 30)
mazeGenerator.generate
puts mazeGenerator.maze
diff --git a/maze.rb b/maze.rb
new file mode 100644
index 0000000..d12a8f5
--- /dev/null
+++ b/maze.rb
@@ -0,0 +1,81 @@
+Tile = Struct.new(:pos, :up, :right, :down, :left)
+
+class Maze
+ attr_reader :width
+ attr_reader :height
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @h_walls = Array.new(@width) { Array.new(@height + 1, true) }
+ @v_walls = Array.new(@width + 1) { Array.new(@height, true) }
+ end
+
+ def inBounds?(pos)
+ x = pos.x
+ y = pos.y
+
+ x >= 0 && y >= 0 && x < @width && y < @height
+ end
+
+ def get(pos)
+ raise IndexError unless inBounds?(pos)
+
+ x = pos.x
+ y = pos.y
+
+ Tile.new(pos, @h_walls[x][y], @v_walls[x + 1][y], @h_walls[x][y + 1], @v_walls[x][y])
+ end
+
+ def neighbors(pos)
+ neighbors = []
+
+ [:up, :right, :down, :left].each do |dir|
+ neighbors.push(pos.move(dir)) if inBounds?(pos.move(dir))
+ end
+
+ neighbors
+ end
+
+ def set(pos, dir, state)
+ raise IndexError if pos.x >= @width
+ raise IndexError if pos.y >= @height
+
+ case dir
+ when :up
+ @h_walls[pos.x][pos.y] = state
+ when :right
+ @v_walls[pos.x + 1][pos.y] = state
+ when :down
+ @h_walls[pos.x][pos.y + 1] = state
+ when :left
+ @v_walls[pos.x][pos.y] = state
+ end
+ end
+
+ def to_s
+ drawingField = Array.new(@height * 2 + 1) { " " * (@width * 2 + 1) }
+
+ @v_walls.each_index do |x|
+ @v_walls[x].each_index do |y|
+ next if @v_walls[x][y] == false
+
+ drawingField[y * 2][x * 2] = "█"
+ drawingField[y * 2 + 1][x * 2] = "█"
+ drawingField[y * 2 + 2][x * 2] = "█"
+ end
+ end
+
+ @h_walls.each_index do |x|
+ @h_walls[x].each_index do |y|
+ next if @h_walls[x][y] == false
+
+ drawingField[y * 2][x * 2] = "█"
+ drawingField[y * 2][x * 2 + 1] = "█"
+ drawingField[y * 2][x * 2 + 2] = "█"
+ end
+ end
+
+ drawingField.join("\n")
+ end
+end
diff --git a/pos.rb b/pos.rb
new file mode 100644
index 0000000..2712931
--- /dev/null
+++ b/pos.rb
@@ -0,0 +1,34 @@
+Pos = Struct.new(:x, :y) do
+ def move(dir)
+ case dir
+ when :up
+ Pos.new(x, y - 1)
+ when :right
+ Pos.new(x + 1, y)
+ when :down
+ Pos.new(x, y + 1)
+ when :left
+ Pos.new(x - 1, y)
+ end
+ end
+
+ def dir_from(pos)
+ diff_x = x - pos.x
+ diff_y = y - pos.y
+
+ case Pos.new(diff_x, diff_y)
+ when Pos.new(0, -1)
+ :up
+ when Pos.new(1, 0)
+ :right
+ when Pos.new(0, 1)
+ :down
+ when Pos.new(-1, 0)
+ :left
+ end
+ end
+
+ def to_s()
+ "(#{x}, #{y})"
+ end
+end