-
Notifications
You must be signed in to change notification settings - Fork 16
/
life_ncurses.rb
49 lines (42 loc) · 1.18 KB
/
life_ncurses.rb
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
#
# This is a class to allow you to visualize your game of life with ncurses.
#
# howto
# if your class is called GameOfLife
# LifeNcurses.new(GameOfLife.new(...))
# with ... the paramters that your initialize method takes
#
# this should show you the game of life evolving in your terminal
# there is a second optional parameter for the number of generations you want to see
# LifeNcurses.new(GameOfLife.new(...),5)
# if you want to see only 5 generations
require 'rubygems'
require 'ffi-ncurses'
class LifeNcurses
# spaces from the border of the terminal
MARGIN = 2
include FFI::NCurses
def initialize(game_of_life,iterations=100)
@stdscr = initscr
cbreak
(1..iterations).each do |generation|
clear
display_title(generation)
show game_of_life.evolve
end
ensure
endwin
end
def show(state)
state.each_with_index do |row,row_index|
row.each_with_index do |col, col_index|
mvwaddstr @stdscr, row_index+MARGIN, col_index+MARGIN, '#' if state[row_index][col_index] == 1
end
end
refresh
sleep 1
end
def display_title(generation)
mvwaddstr @stdscr, 0, 1, "Game of life: Generation #{generation}"
end
end