Skip to content

Instantly share code, notes, and snippets.

@bdougie
Last active August 29, 2015 13:59
Show Gist options
  • Save bdougie/10530967 to your computer and use it in GitHub Desktop.
Save bdougie/10530967 to your computer and use it in GitHub Desktop.
Failing Conway Test
#// Here is my Failing test //#
Failures:
1) Game of Life #Board should check neighbor to the north for alive
Failure/Error: expect(subject.live_neighbors_around).to change(subject.live_neighbors_around, :count).by(1)
NoMethodError:
undefined method `count' for nil:NilClass
# ./spec/start_spec.rb:48:in `block (3 levels) in <top (required)>'
#// Here is my Rspec test //#
it 'should check neighbor to the north for alive' do
subject.grid[cell.y + 1][cell.x].alive = true
expect(subject.live_neighbors_around).to change(subject.live_neighbors_around, :count).by(1)
end
#// Here is my file ~/lib/start.rb //#
def live_neighbors_around(cell)
#added _around to distinguish what is being tested
# count the live_neighbors and add them in the array
live_neighbors = []
# check if all cells in all directions are alive
# checks to the north
if cell.y > 0
spot_check = self.grid[cell.y - 1][cell.x]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the south
if cell.y < 3
spot_check = self.grid[cell.y + 1][cell.x]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the east
if cell.x > 0
spot_check = self.grid[cell.x - 1][cell.y]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the west
if cell.x > 3
spot_check = self.grid[cell.x + 1][cell.y]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the south diagnals
if cell.y < 3
spot_check = self.grid[cell.y + 1][cell.x - 1]
live_neighbors << spot_check if spot_check.alive?
spot_check = self.grid[cell.y + 1][cell.x + 1]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the north diagonals
if cell.y > 0
spot_check = self.grid[cell.y - 1][cell.x - 1]
live_neighbors << spot_check if spot_check.alive?
spot_check = self.grid[cell.y - 1][cell.x + 1]
live_neighbors << spot_check if spot_check.alive?
end
# This is for reference while writing it statements
# [[nill ,nill , nil],
# [[nill ,nill , nil],
# [[nill ,nill , nil]
#
live_neighbors
end
=begin
Conway's Game of Life
1 Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2 Any live cell with two or three live neighbours lives on to the next generation.
3 Any live cell with more than three live neighbours dies, as if by overcrowding.
4 Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
=end
class Board
attr_accessor :rows, :columns, :grid, :live_neighbors_around
def initialize(rows=3, columns=3)
@rows = rows
@columns = columns
@grid = Array.new(rows) { |row| Array.new(columns) { |column| Cell.new(row,column)}}
# This is what I want
# [[nill ,nill , nil],
# [[nill ,nill , nil],
# [[nill ,nill , nil]
#
end
end
class Cell
attr_accessor :alive, :x, :y
def initialize(x=0,y=0)
#each new cell starts out as dead
# alive will be 1 and dead will be 0
@alive = false
@x = x
@y = y
end
# defining what is alive
def alive?
alive
end
# defining what is dead
def dead?
alive == false
end
# identifying what alive neighbor is
def live_neighbors_around(cell)
#added _around to distinguish what is being tested
# count the live_neighbors and add them in the array
live_neighbors = []
# check if all cells in all directions are alive
# checks to the north
if cell.y > 0
spot_check = self.grid[cell.y - 1][cell.x]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the south
if cell.y < 3
spot_check = self.grid[cell.y + 1][cell.x]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the east
if cell.x > 0
spot_check = self.grid[cell.x - 1][cell.y]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the west
if cell.x > 3
spot_check = self.grid[cell.x + 1][cell.y]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the south diagnals
if cell.y < 3
spot_check = self.grid[cell.y + 1][cell.x - 1]
live_neighbors << spot_check if spot_check.alive?
spot_check = self.grid[cell.y + 1][cell.x + 1]
live_neighbors << spot_check if spot_check.alive?
end
# checks to the north diagonals
if cell.y > 0
spot_check = self.grid[cell.y - 1][cell.x - 1]
live_neighbors << spot_check if spot_check.alive?
spot_check = self.grid[cell.y - 1][cell.x + 1]
live_neighbors << spot_check if spot_check.alive?
end
# This is for reference while writing it statements
# [[nill ,nill , nil],
# [[nill ,nill , nil],
# [[nill ,nill , nil]
#
live_neighbors
end
end
class Game
attr_accessor :board, :seeds
def initialize(board=Board.new, seeds=[])
@board = board
@seeds = seeds
seeds.each do |seed|
board.grid[seed[0]][seed[1]].alive = true
end
end
def tick!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment