-
Notifications
You must be signed in to change notification settings - Fork 1
/
life.rb
executable file
·78 lines (68 loc) · 1.37 KB
/
life.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
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
#! /usr/bin/env ruby
require 'terminfo'
def clear()
puts "e[H\e[2J"
end
def show(board)
clear
board.each do |row|
puts row.map{|cell|" +#"[cell]}.join
end
end
def alive?(cell)
cell==2?1:0
end
def count_neighbors(board, x, y)
right_edge = board[0].length-1;
bottom_edge = board.length-1
_x = (x > 0)?(x - 1):right_edge
x_ = (x < right_edge)?(x+1):0
_y = (y > 0)?(y - 1):bottom_edge
y_ = (y < bottom_edge)?(y + 1):0
alive?(board[_y ][_x ]) + alive?(board[_y ][ x ]) + alive?(board[_y ][ x_]) + \
alive?(board[ y ][_x ]) + alive?(board[ y ][ x_]) + \
alive?(board[ y_][_x ]) + alive?(board[ y_][ x ]) + alive?(board[ y_][ x_])
end
def step(board)
board.each_with_index.map do |row,y|
row.each_with_index.map do |cell,x|
neighbors = count_neighbors board, x, y
case(cell)
when 2
case(neighbors)
when 2,3
2
else
1
end
when 0,1
neighbors==3?2:0
end
end
end
end
def rand_life(total=7,alive=6)
if rand(total) < alive
0
else
2
end
end
def gen_world(width, height)
world=[]
height.times do
row=[]
width.times do
row << rand_life
end
world << row
end
world
end
height, width = TermInfo.screen_size
world = gen_world width, (height - 1)
loop do
show world
world = step world
sleep 0.05
end