Created
April 9, 2014 13:02
-
-
Save jmscholen/10267631 to your computer and use it in GitHub Desktop.
Testing HW - cell game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
################creating the world class#################### | |
class World | |
attr_reader :cells, :cells_with_neighbors, :living_neighbors, :dead_neighbors | |
def initialize | |
#@cells = Array.new(10, Array.new(10, Cell.new)) optional method for creating multidimentional array | |
@cells = Array.new(10) { Array.new(10) {Cell.new}} | |
end | |
def locate_cell_and_neighbors | |
@cells_with_neighbors = [] | |
@cells.each_with_index do |cell_row, y| | |
cell_row.each_with_index do |cell,x| | |
row = y | |
column = x | |
neighbors = find_neighbors(cell, row, column) | |
@cells_with_neighbors << {cell: cell, neighbors: neighbors} | |
end | |
end | |
return @cells_with_neighbors | |
end | |
def find_neighbors(cell, row, column) | |
neighbors = [] | |
neighbors << @cells[row-1][column-1] rescue nil | |
neighbors << @cells[row-1][column] rescue nil | |
neighbors << @cells[row-1][column+1] rescue nil | |
neighbors << @cells[row][column+1] rescue nil | |
neighbors << @cells[row+1][column+1] rescue nil | |
neighbors << @cells[row+1][column] rescue nil | |
neighbors << @cells[row+1][column-1]rescue nil | |
neighbors << @cells[row][column-1] rescue nil | |
neighbors.compact! | |
return neighbors | |
end | |
def display_cells(cell) | |
display_array = [] | |
cell.each do |cell| | |
if cell.alive == true | |
display_array.push("o") | |
else | |
display_array.push("-") | |
end | |
end | |
puts display_array[0..9] | |
puts display_cells[10-19] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
puts display_cells[20-29] | |
end | |
end | |
# if living.count < 2 | |
# print "-" | |
# puts "" | |
# @alive = false | |
# elsif living.count == 2 || living.count == 3 | |
# print "e" | |
# puts"" | |
# @alive = true | |
# elsif living.count > 3 | |
# print "f" | |
# puts "" | |
# @alive = false | |
# elsif dead.count > 3 | |
# print "o" | |
# puts "" | |
# @alive = true | |
# end | |
def cells_with_neighbors_status | |
living_neighbors = [] | |
dead_neighbors = [] | |
living_cells = [] | |
dead_cells = [] | |
cell = [] | |
neighbors = [] | |
@cells_with_neighbors.each do |cell_hash| | |
cell.push(cell_hash[:cell]) | |
neighbors = cell_hash[:neighbors] | |
neighbors.each do |neighbor| | |
if neighbor.alive == true | |
living_neighbors.push(neighbor) | |
else | |
dead_neighbors.push(neighbor) | |
end | |
end | |
end | |
cell.each do |cell| | |
if cell.alive == true | |
living_cells.push(cell) | |
else | |
dead_cells.push(cell) | |
end | |
end | |
display_cells(cell) | |
end | |
def start_culture | |
locate_cell_and_neighbors | |
cells_with_neighbors_status | |
# look_at_cell | |
end | |
end | |
#####################creating the cell classs ######################## | |
class Cell | |
attr_reader :alive | |
def initialize | |
answer = [true, false] | |
@alive = answer.sample | |
end | |
def resurrect | |
@alive = true | |
end | |
def die | |
@alive = false | |
end | |
end | |
World.new.start_culture | |
################test the world creation and cell birthing and death####################### | |
describe World do | |
let(:world) {World.new} | |
it 'should exist' do | |
world.should_not eq(nil) | |
end | |
it 'should contain cells' do | |
world.cells.should_not be_empty | |
end | |
# #####################creation of array of cells##################### | |
it 'should contain 100 cells' do | |
world.cells.flatten.length.should eq(100) | |
end | |
# #################counting neighbors################## | |
it 'should identify cells and neighbors' do | |
cell = world.cells.flatten.first | |
world.locate_cell_and_neighbors | |
world.cells_with_neighbors.count.should eq(100) | |
end | |
it 'should identify all live neighbors' do | |
world.locate_cell_and_neighbors | |
world.cells_with_neighbors_status | |
world.cells_with_neighbors.length.should eq(100) | |
world.living_neighbors.uniq.length.should eq(world.cells_with_neighbors.length - world.dead_neighbors.uniq.length) | |
end | |
end | |
# ########################test cell creation########################### | |
describe Cell do | |
context 'dead cell' do | |
before do | |
@cell = Cell.new | |
end | |
it 'should exist' do | |
@cell.should_not eq(nil) | |
end | |
# it 'should be alive or dead' do | |
# @cell.alive.should eq(true) || @cell.alive.should eq(false) | |
# end | |
it 'should resurrect' do | |
@cell.resurrect.should eq(true) | |
end | |
end | |
context 'living cell' do | |
before do | |
@cell = Cell.new | |
@cell.resurrect | |
end | |
it 'should die' do | |
@cell.die | |
@cell.alive.should eq(false) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment