Class: GameOfLife::World

Inherits:
Object
  • Object
show all
Defined in:
lib/game-of-life/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ World

Returns a new instance of World.



7
8
9
10
# File 'lib/game-of-life/world.rb', line 7

def initialize(width,height)
  @width, @height = width, height
  setup(false)
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



5
6
7
# File 'lib/game-of-life/world.rb', line 5

def board
  @board
end

#cellsObject

Returns the value of attribute cells.



5
6
7
# File 'lib/game-of-life/world.rb', line 5

def cells
  @cells
end

Instance Method Details

#dead_cellsObject



21
22
23
# File 'lib/game-of-life/world.rb', line 21

def dead_cells
  @cells.select { |cell| cell.dead? }
end

#get(x, y) ⇒ Object



29
30
31
# File 'lib/game-of-life/world.rb', line 29

def get(x,y)
  @board.element(x,y)
end

#kill(x, y) ⇒ Object



25
26
27
# File 'lib/game-of-life/world.rb', line 25

def kill(x,y)
  @board.element(x,y).die!
end

#live_cellsObject



17
18
19
# File 'lib/game-of-life/world.rb', line 17

def live_cells
  @cells.select { |cell| cell.alive? }
end

#live_neighbours_of(x, y) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/game-of-life/world.rb', line 37

def live_neighbours_of(x,y)
  live_neighbours = []

  # up-left
  if x-1 >= 0 and y+1 <= @height-1
    neighbour = get(x-1,y+1)
    live_neighbours << neighbour if neighbour.alive?
  end

  # up
  if y+1 <= @height-1
    neighbour = get(x,y+1)
    live_neighbours << neighbour if neighbour.alive?
  end

  # up-right
  if x+1 <= @width-1 and y+1 <= @height-1
    neighbour = get(x+1,y+1)
    live_neighbours << neighbour if neighbour.alive?
  end

  # left
  if x-1 >= 0
    neighbour = get(x-1,y)
    live_neighbours << neighbour if neighbour.alive?
  end

  # right
  if x+1 <= @width-1
    neighbour = get(x+1,y)
    live_neighbours << neighbour if neighbour.alive?
  end

  # down-left
  if x-1 >= 0 and y-1 >= 0
    neighbour = get(x-1,y-1)
    live_neighbours << neighbour if neighbour.alive?
  end

  # down
  if y-1 >= 0
    neighbour = get(x,y-1)
    live_neighbours << neighbour if neighbour.alive?
  end

  # down-right
  if x+1 <= @width-1 and y-1 >= 0
    neighbour = get(x+1,y-1)
    live_neighbours << neighbour if neighbour.alive?
  end

  live_neighbours
end

#revive(x, y) ⇒ Object



33
34
35
# File 'lib/game-of-life/world.rb', line 33

def revive(x,y)
  @board.element(x,y).reborn!
end

#rotate!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/game-of-life/world.rb', line 91

def rotate!
  future_alive_cells = []
  future_dead_cells = []

  @cells.each do |cell|
    live_neighbours = live_neighbours_of(cell.x,cell.y)

    future_dead_cells << cell if apply_rule_1(cell,live_neighbours)
    future_alive_cells << cell if apply_rule_2(cell,live_neighbours)
    future_dead_cells << cell if apply_rule_3(cell,live_neighbours)
    future_alive_cells << cell if apply_rule_4(cell,live_neighbours)
  end

  update_board(future_alive_cells,future_dead_cells)
  update_cells
end

#seed!Object



12
13
14
15
# File 'lib/game-of-life/world.rb', line 12

def seed!
  clean_cells
  setup(true)
end