/
life.rb
executable file
·96 lines (75 loc) · 1.75 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
class World
attr_accessor :size, :terrain
def initialize(length = 10, height = 10)
@length = length
@height = height
@size = [length, height]
@terrain = []
height.times {@terrain << []}
@terrain.each do |derp|
length.times {derp << ' '}
end
end
def print
@terrain.each do |derp|
puts derp.join
end
end
def pop(x = 20)
x.times do
@terrain[rand(@height)][rand(@length)] = "# "
end
end
def tick
temparray = Marshal.load(Marshal.dump(@terrain))
x = 0
until x == @terrain.length
y = 0
until y == @terrain[0].length
neigh = 0
neigh += 1 if @terrain[(x+1)% @height][(y+1) % @length] == "# "
neigh += 1 if @terrain[(x+1)% @height][y-1] == "# "
neigh += 1 if @terrain[(x+1)% @height][y] == "# "
neigh += 1 if @terrain[x-1][(y+1) % @length] == "# "
neigh += 1 if @terrain[x][(y+1) % @length] == "# "
neigh += 1 if @terrain[x-1][y] == "# "
neigh += 1 if @terrain[x][y-1] == "# "
neigh += 1 if @terrain[x-1][y-1] == "# "
if @terrain[x][y] == " "
if neigh == 3
temparray[x][y] = "# "
end
end
if @terrain[x][y] == "# "
if neigh == 2 || neigh == 3
temparray[x][y] = "# "
else
temparray[x][y] = " "
end
end
y += 1
end
x += 1
end
@terrain = temparray
end
end
x = `tput cols`.to_i
y = `tput lines`.to_i
pop = ((x*y) * 0.8).to_i
pop = ((x*y) * 0.2).to_i
x = World.new(x/2, y)
x.pop(pop)
x.print
if ARGV[0]
sleeptime = ARGV[0].to_f
else
sleeptime = 0.1
end
loop do
system("clear")
x.tick
x.print
sleep sleeptime
end