Created
November 7, 2015 17:56
-
-
Save nyarly/6aacd822f19f9bca843d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Conway | |
| LIVE = true | |
| DEAD = false | |
| def initialize(width, height) | |
| @width, @height = width, height | |
| @board = build_board | |
| end | |
| attr_accessor :width, :height | |
| attr_reader :board | |
| def build_board | |
| board = [] | |
| @height.times do | |
| board << [DEAD] * @width | |
| end | |
| board | |
| end | |
| def cell(x,y,liveness) | |
| @board[y][x] = liveness | |
| end | |
| def step | |
| next_board = build_board | |
| @board = next_board | |
| end | |
| def get_cell(x,y) | |
| @board[y][x] | |
| end | |
| end | |
| if $0 == __FILE__ | |
| game = Conway.new(3,3) | |
| # ooo | |
| # oxo | |
| # oox | |
| game.cell(2,2,true) | |
| game.cell(1,1,true) | |
| game.step | |
| # ooo | |
| # oox | |
| # oxo | |
| p game.board | |
| puts "1,1 should be dead" unless game.get_cell(1,1) == false | |
| puts "2,2 should be dead" unless game.get_cell(1,1) == false | |
| puts "1,2 should be live" unless game.get_cell(1,1) == true | |
| puts "2,1 should be live" unless game.get_cell(1,1) == true | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment