-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.rb
107 lines (91 loc) · 2.53 KB
/
world.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
97
98
99
100
101
102
103
104
105
106
107
class World
attr_reader :pods, :embryos, :display_settings
def initialize
@pods = {}
@embryos = {}
@display_settings = {
:origin => Position.new(0,0),
:width => 30,
:height => 30,
:live_as => "#",
:dead_as => " "
}
@cycle_count = 0
@died_last_cycle = 0
@born_last_cycle = 0
@statistics = {
:pods_alive => 0,
:pods_born => 0,
:pods_died => 0,
:cycle_count => 0
}
end
def update_statistics
# More readable method used to update statistics
statistics
end
def statistics
@statistics[:pods_alive] = @pods.count
@statistics[:pods_born] = @born_last_cycle
@statistics[:pods_died] = @died_last_cycle
@statistics[:cycle_count] = @cycle_count
@statistics
end
def evolve
@pods.each { |pod_key, pod| pod.throw_seeds }
@pods.each { |pod_key, pod| pod.try_to_survive }
@pods.each { |pod_key, pod| pod.gets_hungry }
@embryos.each { |embryo_key, embryo| embryo.try_birth }
@cycle_count += 1
@died_last_cycle = @pods.count < @statistics[:pods_alive] ? @statistics[:pods_alive] - @pods.count : 0
@born_last_cycle = @pods.count > @statistics[:pods_alive] ? @pods.count - @statistics[:pods_alive] : 0
update_statistics
end
def pod_at(x,y)
@pods[Position.xy_to_s(x,y)]
end
def add_pod(pod)
pod.world = self unless pod.world.equal? self
@pods[pod.position.to_s] = pod
end
def remove_pod(pod)
pod.world = nil
@pods.delete(pod.position.to_s)
end
def add_embryo(embryo)
embryo.world = self unless embryo.world.equal? self
embryos[embryo.position.to_s] = embryo
end
def remove_embryo(embryo)
embryo.world = nil
@embryos.delete(embryo.position.to_s)
end
def add_seed_at(position)
if @pods[position.to_s].nil?
if @embryos[position.to_s].nil?
embryo = Embryo.new(position.x, position.y)
embryo.fertilize
add_embryo(embryo)
else
@embryos[position.to_s].fertilize
end
else
@pods[position.to_s].fertilize
end
end
def display_settings=(options)
@display_settings.merge!(options)
end
def view_bounds_as_string
result = ""
@display_settings[:height].times do |row|
cursor = Position.new(@display_settings[:origin].x, (@display_settings[:origin].y + row))
@display_settings[:width].times do
result += @pods[cursor.to_s].nil? ? @display_settings[:dead_as] : @display_settings[:live_as]
cursor.move.east_by(1)
end
result += "\n"
end
result
end
end