Class: Life::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/life/ui.rb

Constant Summary collapse

DELAY =
0.05

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ UI

Returns a new instance of UI.



24
25
26
27
28
29
30
# File 'lib/life/ui.rb', line 24

def initialize(board)
  Curses.init_screen()
  Curses.curs_set(0)
  @window = Curses::Window.new(0, 0, 0, 0)

  @board = board
end

Class Method Details

.executeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/life/ui.rb', line 5

def self.execute
  ui = new(load_board("life/glider.txt"))

  loop do
    ui.render()
    sleep(DELAY)
    ui.update()
  end
rescue Interrupt, IRB::Abort
  return
ensure
  ui.close()
end

.load_board(relative_path) ⇒ Object



19
20
21
22
# File 'lib/life/ui.rb', line 19

def self.load_board(relative_path)
  path = File.join(LIB_PATH, relative_path)
  Life::Board.from_text(File.read(path))
end

Instance Method Details

#closeObject



47
48
49
# File 'lib/life/ui.rb', line 47

def close
  Curses::close_screen()
end

#renderObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/life/ui.rb', line 32

def render
  @window.clear()

  @board.each do |x, y, alive|
    @window.setpos(y, x)
    @window.addstr(alive ? "#" : " ")
  end

  @window.refresh()
end

#updateObject



43
44
45
# File 'lib/life/ui.rb', line 43

def update
  @board.tick()
end