-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.rb
49 lines (39 loc) · 846 Bytes
/
fish.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
class Fish
TIME_GRANULARITY = 1
REPRODUCTION_CYCLE = 4
def initialize(position ,generator=RandomGenerator)
@position= position
@generator= generator
@age = 0
@children=[]
end
def act(allowed_positions)
age_increases
reproduce
move(allowed_positions)
end
def where
@position
end
private
def reproduce
return unless is_time_to_give_birth?
@children << Fish.new(@position)
end
def move(allowed_positions)
return if allowed_positions.empty?
random = @generator.random(allowed_positions.size)
@position = allowed_positions[random]
end
def age_increases
@age += TIME_GRANULARITY
end
def is_time_to_give_birth?
return ((@age % REPRODUCTION_CYCLE) == 0)
end
class RandomGenerator
def self.random limit
rand(limit)
end
end
end