-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule.rb
70 lines (53 loc) · 1.24 KB
/
rule.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
class Rule
def evaluate state, population
raise "Abstract Rule represents the interface"
end
end
class Flourishing<Rule
FERTILE_POBLATION = 3
def evaluate state, population
return State.alive if flourishing? state,population
State.null
end
def flourishing? state , population
state.dead? and fertile? population
end
def fertile? population
population == FERTILE_POBLATION
end
end
class Overcrowded<Rule
OVERCROW_TRESHOLD = 3
def evaluate state, population
return State.dead if overcrowded? population
State.null
end
def overcrowded? population
population > OVERCROW_TRESHOLD
end
end
class Underpopulated<Rule
UNDERPOPULATION_TRESHOLD = 2
def evaluate state, population
return State.dead if underpopulated? population
State.null
end
def underpopulated? population
population < UNDERPOPULATION_TRESHOLD
end
end
class Healthy<Rule
MIN_POPULATION=2
MAX_POPULATION=3
BALANCED=(MIN_POPULATION..MAX_POPULATION)
def evaluate state, population
return State.alive if healthy? state, population
State.null
end
def healthy? state, population
state.alive? and balanced? population
end
def balanced? population
BALANCED.include? population
end
end