Skip to content

Instantly share code, notes, and snippets.

@iftheshoefritz
Created January 10, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iftheshoefritz/44a287b9307252f19e5b3ca448bde20e to your computer and use it in GitHub Desktop.
Save iftheshoefritz/44a287b9307252f19e5b3ca448bde20e to your computer and use it in GitHub Desktop.
class GameState
attr_accessor :matrix
def initialize(matrix)
@matrix = deep_dup(matrix)
@next_step = deep_dup(matrix)
end
def [](y)
@matrix[y]
end
def advance
iterate_cells do |val, y, x|
kill_cell(y, x) if alive?(val) && count_neighbours(y, x) < 2
kill_cell(y, x) if alive?(val) && count_neighbours(y, x) > 3
resurrect_cell(y, x) if dead?(val) && count_neighbours(y, x) == 3
end
GameState.new(@next_step)
end
private
def iterate_cells(&block)
@matrix.each_with_index do |row, y|
row.each_with_index do |cell, x|
yield(cell, y, x)
end
end
end
def count_neighbours(y, x)
count = 0
((y -1)..(y + 1)).each do |vert|
((x - 1)..(x + 1)).each do |horiz|
count += @matrix[vert][horiz] if in_bounds?(vert, horiz) && !(vert == y && horiz == x)
end
end
count
end
def alive?(val)
val == 1
end
def dead?(val)
val == 0
end
def in_bounds?(y, x)
y >= 0 && y < @matrix.length && x >= 0 && x < @matrix[0].length
end
def kill_cell(y, x)
@next_step[y][x] = 0
end
def resurrect_cell(y, x)
@next_step[y][x] = 1
end
# Safe 2d array handling - array.dup is not sufficient!
def deep_dup(matrix)
Marshal.load(Marshal.dump(matrix))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment