Class: LifeGrid
- Inherits:
-
Object
- Object
- LifeGrid
- Defined in:
- lib/NanoLife.rb
Instance Method Summary collapse
-
#check_neighbors(x, y) ⇒ Object
Given an x and y, calculate how many neighbors cell has.
-
#clear ⇒ Object
Utility function to randomize grid.
-
#create_grid ⇒ Object
Create an empty grid array.
-
#draw ⇒ Object
Draw the cell grid to the window.
-
#initialize(window) ⇒ LifeGrid
constructor
Manages the underlying array of cells and their life process.
-
#invert_cell(x, y) ⇒ Object
Flip the state of a cell.
-
#randomize ⇒ Object
Utility function to randomize grid.
-
#update ⇒ Object
Ipdate state of all cells based on Conway’s Game of Life rules.
Constructor Details
#initialize(window) ⇒ LifeGrid
Manages the underlying array of cells and their life process
71 72 73 74 75 76 |
# File 'lib/NanoLife.rb', line 71 def initialize(window) @num_cols = WIN_WIDTH / CELL_SIZE @num_rows = WIN_HEIGHT / CELL_SIZE @grid = create_grid @window = window end |
Instance Method Details
#check_neighbors(x, y) ⇒ Object
Given an x and y, calculate how many neighbors cell has
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/NanoLife.rb', line 111 def check_neighbors(x, y) num_neighbors = 0 # Previous row if y > 0 and x > 0 num_neighbors += @grid[x-1][y-1] end if y > 0 num_neighbors += @grid[x][y-1] end if x < @num_cols - 1 and y > 0 num_neighbors += @grid[x+1][y-1] end # Current row if x > 0 num_neighbors += @grid[x-1][y] end if x < @num_cols - 1 num_neighbors += @grid[x+1][y] end # Lower row if x > 0 and y < @num_rows - 1 num_neighbors += @grid[x-1][y+1] end if y < @num_rows - 1 num_neighbors += @grid[x][y+1] end if x < @num_cols - 1 and y < @num_rows - 1 num_neighbors += @grid[x+1][y+1] end return num_neighbors end |
#clear ⇒ Object
Utility function to randomize grid
184 185 186 187 188 189 190 |
# File 'lib/NanoLife.rb', line 184 def clear (0...@num_cols).each do |x| (0...@num_rows).each do |y| @grid[x][y] = 0 end end end |
#create_grid ⇒ Object
Create an empty grid array
79 80 81 |
# File 'lib/NanoLife.rb', line 79 def create_grid return Array.new(@num_cols) {Array.new(@num_rows, 0)} end |
#draw ⇒ Object
Draw the cell grid to the window
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/NanoLife.rb', line 148 def draw # draw grid (0...@num_cols).each do |x| (0...@num_rows).each do |y| if @grid[x][y] == 0 color = EMPTY_COLOR else color = CELL_COLOR end realx = x * CELL_SIZE realy = y * CELL_SIZE @window.draw_quad( realx, realy, color, realx + CELL_SIZE, realy, color, realx, realy + CELL_SIZE, color, realx + CELL_SIZE, realy + CELL_SIZE, color, ) end end end |
#invert_cell(x, y) ⇒ Object
Flip the state of a cell. Useful for manual click manipulation
170 171 172 |
# File 'lib/NanoLife.rb', line 170 def invert_cell(x, y) @grid[x][y] = (@grid[x][y] == 0 ? 1 : 0) end |
#randomize ⇒ Object
Utility function to randomize grid
175 176 177 178 179 180 181 |
# File 'lib/NanoLife.rb', line 175 def randomize (0...@num_cols).each do |x| (0...@num_rows).each do |y| @grid[x][y] = rand(2) end end end |
#update ⇒ Object
Ipdate state of all cells based on Conway’s Game of Life rules
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/NanoLife.rb', line 84 def update # For all cells check neighbors and kill or birth tmp_grid = create_grid (0...@num_cols).each do |x| (0...@num_rows).each do |y| num_neighbors = check_neighbors(x, y) if num_neighbors < 2 and @grid[x][y] == 1 tmp_grid[x][y] = 0 end if num_neighbors == 2 or num_neighbors == 3 if @grid[x][y] == 1 tmp_grid[x][y] = 1 end end if num_neighbors == 3 and @grid[x][y] == 0 tmp_grid[x][y] = 1 end if num_neighbors > 3 and @grid[x][y] == 1 tmp_grid[x][y] = 0 end end end @grid = tmp_grid end |