Skip to content

Instantly share code, notes, and snippets.

@benneuman
Created February 13, 2014 20:38
Show Gist options
  • Save benneuman/8983346 to your computer and use it in GitHub Desktop.
Save benneuman/8983346 to your computer and use it in GitHub Desktop.
def assert(actual, expected)
if actual == expected
puts "#{actual.inspect} equals #{expected.inspect}"
else
raise "Expected #{actual.inspect} to equal #{expected.inspect}"
end
end
LIVE = 'X'
DEAD = 'O'
def alive?(point, live_neighbor_count)
if point == LIVE
return false if (live_neighbor_count < 2 || live_neighbor_count > 3)
return true if (live_neighbor_count == 2 || live_neighbor_count == 3)
else
live_neighbor_count == 3
end
end
def live_neighbor_count(x, y, board)
neighbors = get_neighbors(x, y)
filter_neighbors(neighbors, board).count { |rindex, cindex| board[rindex][cindex] == LIVE }
end
def filter_neighbors(neighbors, board)
neighbors.select do |rindex, cindex|
rindex >= 0 && rindex < board.length && cindex >= 0 && cindex < board[rindex].length
end
end
def get_neighbors(x, y)
[
[x, (y+1)],
[x, (y-1)],
[(x-1), y],
[(x+1), y],
[(x+1), (y+1)],
[(x-1), (y-1)],
[(x+1), (y-1)],
[(x-1), (y+1)]
]
end
def evolve(board)
board.map.with_index do |row, row_index|
row.map.with_index do |point, column_index|
live_neighbors = live_neighbor_count(row_index, column_index, board)
(alive?(point, live_neighbors)) ? LIVE : DEAD
end
end
end
#testing evolve
board = [%w(O)]
assert(evolve(board), [%w(O)])
board = [%w(X)]
assert(evolve(board), [%w(O)])
board = [%w(X X X)]
assert(evolve(board), [%w(O X O)])
board = [%w(X O X), %w(X X O), %w(X O X)]
expected = [%w(X O O), %w(X O X), %w(X O O)]
assert(evolve(board), expected)
#get_live_count counts horizontal neighbors
board = [%w(O X O)]
assert(live_neighbor_count(0, 0, board), 1)
assert(live_neighbor_count(0, 1, board), 0)
assert(live_neighbor_count(0, 2, board), 1)
#get_live_count counts vertical neighbors
board = [%w(O), %w(X), %w(O)]
assert(live_neighbor_count(0, 0, board), 1)
assert(live_neighbor_count(1, 0, board), 0)
assert(live_neighbor_count(2, 0, board), 1)
#get_live_count counts diagonal neighbors
board = [%w(O X), %w(X O)]
assert(live_neighbor_count(0, 1, board), 1)
assert(live_neighbor_count(1, 0, board), 1)
board = [%w(X O), %w(O X)]
assert(live_neighbor_count(0, 0, board), 1)
assert(live_neighbor_count(1, 1, board), 1)
#Rule #1 - Living cell dies if surrounded by less than 2 living cells
point = LIVE
live_neighbor_count = 0
assert(alive?(point, live_neighbor_count), false)
point = LIVE
live_neighbor_count = 1
assert(alive?(point, live_neighbor_count), false)
# Rule 2 - Living cell stays alive if surrounded by 2 or 3 living cells
point = LIVE
live_neighbor_count = 2
assert(alive?(point, live_neighbor_count), true)
point = LIVE
live_neighbor_count = 3
assert(alive?(point, live_neighbor_count), true)
# Rule 3 - Living cell dies if surrounded by more than 3 living cells
point = LIVE
live_neighbor_count = 4
assert(alive?(point, live_neighbor_count), false)
# Rule 4 - Dead cell comes alive if surrounded by exactly 3 living cells
point = DEAD
live_neighbor_count = 3
assert(alive?(point, live_neighbor_count), true)
#"Rule 5" - Dead cell stays dead if surrounded by anything but 3 living cells
point = DEAD
live_neighbor_count = 2
assert(alive?(point, live_neighbor_count), false)
point = DEAD
live_neighbor_count = 4
assert(alive?(point, live_neighbor_count), false)
# # Rule #1
# # assert(evolve([%w(X O), %w(O O)]), [%w(O O), %w(O O)])
# board = [%w(X O), %w(O O)]
# assert(alive?(0, 0, board), false)
# # assert(evolve([%w(O O), %w(O X)]), [%w(O O), %w(O O)])
# board = [%w(O O), %w(O X)]
# assert(alive?(1, 1, board), false)
# #assert(evolve([%w(O O O), %w(O X O), %w(O O O)]), [%w(O O O), %w(O O O), %w(O O O)])
# board = [%w(O O O), %w(O X O), %w(O O O)]
# assert(alive?(1, 1, board), false)
# # Rule 2
# # assert(evolve([%w(X X X)]), [%w(O X O)])
# board = [%w(X X X)]
# assert(alive?(0, 0, board), false)
# assert(alive?(0, 2, board), false)
# assert(alive?(0, 1, board), true)
# # assert(evolve([%w(O X O), %w(O X O), %w(O X O)]), [%w(O O O), %w(O X O), %w(O O O)])
# board = [%w(O X O), %w(O X O), %w(O X O)]
# assert(alive?(0, 1, board), false)
# assert(alive?(2, 1, board), false)
# assert(alive?(1, 1, board), true)
# # assert(evolve([%w(O X O), %w(O X X)]), [%w(O X O), %w(O X X)])
# board = [%w(O X O), %w(O X X)]
# assert(alive?(0, 2, board), false)
# # assert(evolve([%w(O X O), %w(X X O)]), [%w(O X O), %w(X X O)])
# board = [%w(O X O), %w(X X O)]
# assert(evolve(board), board)
# #Here ends pairing
# # assert(evolve([%w(X O X), %w(O X O), %w(X O O)]), [%w(O O O), %w(O X O), %w(O O O)])
# board = [%w(X O X), %w(O X O), %w(X O O)]
# assert(alive?(0, 0, board), false)
# assert(alive?(0, 2, board), false)
# assert(alive?(2, 0, board), false)
# #Rule 3
# # assert(evolve([%w(X O X), %w(O X O), %w(X O X)]), [%w(O O O), %w(O O O), %w(O O O)])
# board = [%w(X O X), %w(O X O), %w(X O X)]
# assert(alive?(1, 1, board), false)
# puts "Test passed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment