class NLife::UI

Constants

RENDER_DEAD
RENDER_LIVE

Public Class Methods

new() click to toggle source
# File lib/nlife/ui.rb, line 10
def initialize
  init_settings
  init_window
  init_life
end

Public Instance Methods

dispatch_key(key) click to toggle source
# File lib/nlife/ui.rb, line 46
def dispatch_key(key)
  case key
  when 'p' then @pause = !@pause
  when 's' then @life.seed
  when 'q' then return false
  end
  true
end
init_life() click to toggle source
# File lib/nlife/ui.rb, line 27
def init_life
  @life = NLife::Game.new(@window_lines - 2, @window_cols - 2) # padding
  @life.seed
end
init_settings() click to toggle source
# File lib/nlife/ui.rb, line 32
def init_settings
  @pause = false
  @delay = 0.04
end
init_window() click to toggle source
# File lib/nlife/ui.rb, line 16
def init_window
  Curses.init_screen
  Curses.curs_set(0)
  Curses.noecho
  @window_lines = Curses.lines
  @window_cols = Curses.cols
  @window = Curses::Window.new(@window_lines, @window_cols, 0, 0)
  @window.box('|', '-')
  @window.timeout = 0
end
render() click to toggle source
# File lib/nlife/ui.rb, line 59
def render
  @life.rows.times do |i|
    string = ''
    @life.cols.times do |j|
      string += @life.state[j, i] > 0 ? RENDER_LIVE : RENDER_DEAD
    end
    @window.setpos(i + 1, 1)
    @window.addstr(string)
  end
end
start() click to toggle source
# File lib/nlife/ui.rb, line 37
def start
  loop do
    break unless dispatch_key(@window.getch)
    step
    render
    sleep(@delay)
  end
end
step() click to toggle source
# File lib/nlife/ui.rb, line 55
def step
  @life.step unless @pause
end