-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_of_life.rb
More file actions
71 lines (56 loc) · 1.09 KB
/
Copy pathgame_of_life.rb
File metadata and controls
71 lines (56 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
require 'terminfo'
require_relative 'cell'
require 'byebug'
require 'curses'
class GameOfLife
attr_reader :height, :width, :cells
def initialize
screen_size = TermInfo.screen_size
@height = screen_size[0]
@width = screen_size[1]
create_cells
end
def output
@cells.collect do |row|
row.collect do |cell|
cell.alive? ? '*' : ' '
end.join
end.join
end
def cycle_cells
@cells = CycleHandler.new(@cells, @height, @width).cycle_cells
end
def setup
Curses.init_screen
init_cells
end
def random_state
[Cell::ALIVE, Cell::DEAD].sample
end
def run(steps = 250)
steps.times do
run_cycle
end
end
def run_cycle
print_output
cycle_cells
sleep 0.05
end
private
def print_output
Curses.setpos(0, 0)
Curses.addstr(output)
Curses.refresh
end
def init_cells
@cells.each_with_index do |row|
row.each_with_index do |cell|
cell.set_state(random_state)
end
end
end
def create_cells
@cells = Array.new(@height) { Array.new(@width) { Cell.new } }
end
end