Created
July 7, 2010 16:30
-
-
Save TestingBytes/466917 to your computer and use it in GitHub Desktop.
Ruby Programming Challenge For Newbiews #11 - Game Of Life
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
# Ruby Programming Challenge For Newbiews #11 - Game Of Life | |
# | |
# game_of_life.rb | |
# | |
# Author: Sam Johnson <samuel.johnson@gmail.com> | |
# Date: July 8, 2010 | |
class GameOfLife | |
attr_accessor :size, :state | |
def initialize(size) | |
@size = size | |
@state = Array.new(@size, Array.new(@size)) | |
@state = @state.map { |row| row.map { |col| 0 } } | |
end | |
def evolve | |
next_state = Array.new(@size, Array.new(@size)) | |
next_state = next_state.map { |row| row.map { |col| 0 }} | |
0.upto(@size-1) { |r| | |
0.upto(@size-1) { |c| | |
case self.neighbors(r,c) | |
when 0..1: next_state[r][c] = 0 | |
when 2: next_state[r][c] = @state[r][c] | |
when 3: next_state[r][c] = 1 | |
when 4..8: next_state[r][c] = 0 | |
end | |
} | |
} | |
@state = Array.new(next_state) | |
end | |
def neighbors(row,col) | |
above = @state[row == 0 ? (@size - 1) : row - 1][col] | |
below = @state[row == (@size - 1) ? 0 : row + 1][col] | |
right = @state[row][col == @size -1 ? 0 : col + 1] | |
left = @state[row][col == 0 ? (@size - 1) : (col - 1)] | |
above_right = @state[row == 0 ? @size - 1 : row - 1][col == @size - 1 ? 0 : col + 1] | |
above_left = @state[row == 0 ? @size - 1 : row - 1][col == 0 ? @size - 1 : col - 1] | |
below_right = @state[row == @size -1 ? 0 : row + 1][col == @size -1 ? 0 : col + 1] | |
below_left = @state[row == @size -1 ? 0 : row + 1][col == 0 ? @size - 1 : col - 1] | |
above + below + right + left + above_right + above_left + below_right + below_left | |
end | |
end |
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
# Ruby Programming Challenge For Newbiews #11 - Game Of Life | |
# | |
# life_processing.rb | |
# | |
# Author: Sam Johnson <samuel.johnson@gmail.com> | |
# Date: July 8, 2010 | |
require 'lib/game_of_life' | |
class Sketch < Processing::App | |
attr_reader :game, :cell_w, :cell_h | |
def setup | |
size width,height | |
background 0 | |
color_mode RGB, 255 | |
frame_rate 10 | |
@size = 100 | |
@game = GameOfLife.new(@size) | |
@game.state = @game.state.map { |row| row.map { |cell| rand(2) } } | |
@cell_w = width / @game.size | |
@cell_h = height / @game.size | |
end | |
def draw | |
0.upto(@game.size-1) { |row| | |
0.upto(@game.size-1) { |col| | |
@game.state[row][col] == 1 ? fill(128) : fill(0) | |
rect row*@cell_w, col*@cell_h, @cell_w, @cell_h | |
} | |
} | |
@game.evolve | |
end | |
def key_pressed | |
case key | |
when 'r': | |
game.state = @game.state.map { |row| row.map { |cell| rand(2) } } | |
end | |
end | |
end | |
Sketch.new(:width => 400, :height => 400, :title => "The Game Of Life") |
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
# Ruby Programming Challenge For Newbiews #11 - Game Of Life | |
# | |
# test_game_of_life.rb | |
# | |
# Author: Sam Johnson <samuel.johnson@gmail.com> | |
# Date: July 8, 2010 | |
# Rules | |
# each cell 2 possible states, life of death | |
# 8 neighbours | |
# - any life cell < 2 neighbours dies | |
# - any life cell > 3 neighbours dies | |
# - any live cell with 2 or 3 neighbours lives to next generation | |
# - any dead cell with exactly 3 live neighbours becomes a live cell | |
# first generation: apply pattern | |
# | |
require File.join(File.dirname(__FILE__), '../lib/game_of_life') | |
require 'rubygems' | |
require 'test/unit' | |
class GameOfLifeTest < Test::Unit::TestCase | |
def setup | |
@game = GameOfLife.new(3) | |
end | |
def test_should_instance_game | |
assert_equal @game.size, 3 | |
end | |
def test_state_should_start_at_zero | |
assert_equal @game.state, | |
[ [0,0,0], | |
[0,0,0], | |
[0,0,0]] | |
end | |
def test_neightbors_sum_starts_at_zero | |
assert_equal @game.neighbors(0,0), 0 | |
end | |
def test_neightbors | |
@game.state = [[1,1,1],[1,1,1],[1,1,1]] | |
assert_equal @game.neighbors(1,1), 8 | |
assert_equal @game.neighbors(2,2), 8 | |
assert_equal @game.neighbors(1,2), 8 | |
assert_equal @game.neighbors(2,0), 8 | |
end | |
def test_neighbors_above | |
@game.state[0][1] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_below | |
@game.state[2][1] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_right | |
@game.state[1][2] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_left | |
@game.state[1][0] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_above_right | |
@game.state[0][2] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_above_left | |
@game.state[0][0] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_below_right | |
@game.state[2][2] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_neighbors_below_left | |
@game.state[2][0] = 1 | |
assert_equal @game.neighbors(1,1), 1 | |
end | |
def test_should_kill_with_no_neighbours | |
@game.state = [[1,0,0],[0,0,0],[0,0,0]] | |
after = @game.evolve | |
assert_equal after[0][0], 0 | |
end | |
def test_should_kill_with_just_one_neighbour | |
@game.state = [[0,0,0],[1,0,0],[1,0,0]] | |
after = @game.evolve | |
assert_equal after[1][0], 0 | |
assert_equal after[2][0], 0 | |
end | |
def test_should_kill_with_more_than_3_neighbours | |
@game.state = [[1,1,1],[1,1,1],[1,1,1]] | |
after = @game.evolve | |
assert_equal after, [[0,0,0],[0,0,0],[0,0,0]] | |
end | |
def test_should_give_birth_if_3_neighbours | |
@game.state = [[1,0,0],[1,1,0],[0,0,0]] | |
after = @game.evolve | |
assert_equal after, [[1,1,1],[1,1,1],[1,1,1]] | |
end | |
def test_full | |
#[0,0,0] [0,0,0] | |
#[0,1,1] ==> [0,1,1] | |
#[0,1,1] [0,1,1] | |
@game.state = [[0,0,0],[0,1,1],[0,1,1]] | |
assert_equal @game.neighbors(0,1), 4 | |
assert_equal @game.neighbors(0,2), 4 | |
after = @game.evolve | |
assert_equal after[0][1], 0 | |
assert_equal after[0][2], 0 | |
assert_equal after, [[0,0,0],[0,1,1],[0,1,1]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment