Эх сурвалжийг харах

initial code -- core classes, binary tree, and ascii output

john melesky 3 жил өмнө
parent
commit
41de043c7f
4 өөрчлөгдсөн 154 нэмэгдсэн , 0 устгасан
  1. 17 0
      src/binary_tree.rb
  2. 8 0
      src/binary_tree_demo.rb
  3. 39 0
      src/cell.rb
  4. 90 0
      src/grid.rb

+ 17 - 0
src/binary_tree.rb

@@ -0,0 +1,17 @@
+class BinaryTree
+
+  def self.on(grid)
+    grid.each_cell do |cell|
+      neighbors = []
+      neighbors << cell.north if cell.north
+      neighbors << cell.east if cell.east
+
+      neighbor = neighbors.sample
+
+      cell.link(neighbor) if neighbor
+    end
+
+    grid
+  end
+
+end

+ 8 - 0
src/binary_tree_demo.rb

@@ -0,0 +1,8 @@
+require 'grid'
+require 'binary_tree'
+
+grid = Grid.new(8,8)
+BinaryTree.on(grid)
+
+puts grid
+

+ 39 - 0
src/cell.rb

@@ -0,0 +1,39 @@
+class Cell
+  attr_reader :row, :column
+  attr_accessor :north, :south, :east, :west
+
+  def initialize(row, column)
+    @row, @column = row, column
+    @links = {}
+  end
+
+  def link(cell, bidi=true)
+    @links[cell] = true
+    cell.link(self, false) if bidi
+    self
+  end
+
+  def unlink(cell, bidi=true)
+    @links.delete(cell)
+    cell.unlink(self, false) if bidi
+    self
+  end
+
+  def links
+    @links.keys
+  end
+
+  def linked?(cell)
+    @links.key?(cell)
+  end
+
+  def neighbors
+    list = []
+    list << north if north
+    list << south if south
+    list <<  east if  east
+    list <<  west if  west
+    list
+  end
+
+end

+ 90 - 0
src/grid.rb

@@ -0,0 +1,90 @@
+require 'cell'
+
+class Grid
+  attr_reader :rows, :columns
+
+  def initialize(rows, columns)
+    @rows = rows
+    @columns = columns
+
+    @grid = prepare_grid
+    configure_cells
+  end
+
+  def prepare_grid
+    Array.new(rows) do |row|
+      Array.new(columns) do |column|
+        Cell.new(row, column)
+      end
+    end
+  end
+
+  def configure_cells
+    each_cell do |cell|
+      row, col = cell.row, cell.column
+
+      cell.north = self[row - 1, col]
+      cell.south = self[row + 1, col]
+      cell.west  = self[row, col - 1]
+      cell.east  = self[row, col + 1]
+    end
+  end
+
+  def [](row, column)
+    return nil unless row.between?(0, @rows - 1)
+    return nil unless column.between?(0, @grid[row].count - 1)
+    @grid[row][column]
+  end
+
+  def random_cell
+    row = rand(@rows)
+    column = rand(@grid[row].count)
+
+    self[row, column]
+  end
+
+  def size
+    @rows * @columns
+  end
+
+  def each_row
+    @grid.each do |row|
+      yield row
+    end
+  end
+
+  def each_cell
+    each_row do |row|
+      row.each do |cell|
+        yield cell if cell
+      end
+    end
+  end
+
+  def to_s
+    output = "+" + "---+" * columns + "\n"
+
+    each_row do |row|
+      top = "|"
+      bottom = "+"
+
+      row.each do |cell|
+        cell = Cell.new(-1, -1) unless cell
+
+        body = "   "
+        east_boundary = (cell.linked?(cell.east) ? " " : "|")
+        top << body << east_boundary
+
+        south_boundary = (cell.linked?(cell.south) ? "   " : "---")
+        corner = "+"
+        bottom << south_boundary << corner
+      end
+
+      output << top << "\n"
+      output << bottom << "\n"
+    end
+
+    output
+  end
+
+end