Class: Conwaymp::Board
- Inherits:
-
Object
- Object
- Conwaymp::Board
- Defined in:
- lib/conwaymp/board.rb
Instance Attribute Summary collapse
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
Instance Method Summary collapse
- #clear_grid ⇒ Object
- #display ⇒ Object
- #generate_grid ⇒ Object
-
#initialize(opts) ⇒ Board
constructor
A new instance of Board.
- #neighbours_of(row, col) ⇒ Object
- #population ⇒ Object
-
#possible_neighbours_of(row, col) ⇒ Object
TODO: This method feels wrong.
- #print_board ⇒ Object
-
#print_stats ⇒ Object
TODO: Tidy?.
- #seed_grid(path, is_included) ⇒ Object
- #tick ⇒ Object
Constructor Details
#initialize(opts) ⇒ Board
Returns a new instance of Board.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/conwaymp/board.rb', line 7 def initialize(opts) @opts = opts @sleep_time = 0.5 # Seconds # Initialize the board @grid = [] # Initialize the stats @state = { tick: 0 } generate_grid seed_file = @opts[:load] || @opts[:seed] seed_grid(seed_file, !@opts[:seed].nil?) if seed_file end |
Instance Attribute Details
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
5 6 7 |
# File 'lib/conwaymp/board.rb', line 5 def grid @grid end |
Instance Method Details
#clear_grid ⇒ Object
32 33 34 |
# File 'lib/conwaymp/board.rb', line 32 def clear_grid @grid.flatten.map(&:die!) # Kill all cells end |
#display ⇒ Object
107 108 109 110 |
# File 'lib/conwaymp/board.rb', line 107 def display print_board print_stats end |
#generate_grid ⇒ Object
26 27 28 29 30 |
# File 'lib/conwaymp/board.rb', line 26 def generate_grid @opts[:rows].times do @grid << Array.new(@opts[:cols]) { Cell.new } end end |
#neighbours_of(row, col) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/conwaymp/board.rb', line 72 def neighbours_of(row, col) cell = @grid[row][col] neighbours = [] possible_neighbours_of(row, col).each do |r, c| neighbours << @grid[r][c] end cell.living_neighbours = neighbours.select(&:alive?).count neighbours end |
#population ⇒ Object
86 87 88 |
# File 'lib/conwaymp/board.rb', line 86 def population @grid.flatten.select(&:alive?).count end |
#possible_neighbours_of(row, col) ⇒ Object
TODO: This method feels wrong. I’m sure there’s something here that can be done with permutations?
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/conwaymp/board.rb', line 54 def possible_neighbours_of(row, col) [ [row - 1, col], # N [row, col + 1], # E [row + 1, col], # S [row, col - 1], # W [row + 1, col + 1], # SE [row - 1, col - 1], # NW [row + 1, col - 1], # SW [row - 1, col + 1], # NE ] .select do |r, c| # Ensure they're within bounds (0..@opts[:rows] - 1).cover?(r) && (0..@opts[:cols] - 1).cover?(c) end end |
#print_board ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/conwaymp/board.rb', line 90 def print_board puts "\e[H\e[2J" # Clear the screen map = @grid.map do |row| row.map(&:print).join('') end.join("\n") puts map end |
#print_stats ⇒ Object
TODO: Tidy?
101 102 103 104 105 |
# File 'lib/conwaymp/board.rb', line 101 def print_stats puts ' + ' * @opts[:cols] puts "Tick: #{@state[:tick]}\t Pop.: #{population}" .center(@opts[:cols] * 3) end |
#seed_grid(path, is_included) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/conwaymp/board.rb', line 36 def seed_grid(path, is_included) clear_grid if is_included path = File.("../../../seeds/#{path}", __FILE__) end file = File.read(File.(path)) file.split.each_with_index do |r, ri| r.split(',').each_with_index do |c, ci| @grid[ri][ci].rebirth! if c == '1' end end end |
#tick ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/conwaymp/board.rb', line 112 def tick @state[:tick] += 1 (0..(@opts[:rows] - 1)).each do |row| (0..(@opts[:cols] - 1)).each do |col| neighbours_of(row, col) end end @grid = @grid.map do |row| row.map do |cell| cell.tick cell end end sleep @sleep_time end |