-
Notifications
You must be signed in to change notification settings - Fork 12
/
strategy.cr
66 lines (52 loc) · 1.4 KB
/
strategy.cr
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
# Allows a set of similar algorithms to be defined and encaplusated
# in their own classes. The algorithm to be used for a particular
# purpose may then be selected at run-time.
class Fighter
getter health, name
setter health
def initialize(@name : String, @fight_strategy : FightStrategy)
@health = 100
end
def attack(opponent)
@fight_strategy.attack self, opponent
puts "#{opponent.name} is dead" if opponent.dead?
end
def dead?
health <= 0
end
def damage(rate)
if @health > rate
@health -= rate
else
@health = 0
end
end
end
abstract class FightStrategy
HITS = {:punch => 40, :kick => 12}
abstract def attack(fighter, opponent)
end
class Puncher < FightStrategy
def attack(fighter, opponent)
puts "#{fighter.name} attacks #{opponent.name} with 1 punch."
opponent.damage(HITS[:punch])
end
end
class Combo < FightStrategy
def attack(fighter, opponent)
puts "#{fighter.name} attacks #{opponent.name} with 2 kicks and 1 punch."
opponent.damage(HITS[:kick])
opponent.damage(HITS[:kick])
opponent.damage(HITS[:punch])
end
end
# Sample
scor = Fighter.new("Scorpion", Puncher.new)
noob = Fighter.new("Noob", Combo.new)
noob.attack scor
# Noob attacks Scorpion with 2 kicks and 1 punch.
scor.attack noob
# Scorpion attacks Noob with 1 punch.
noob.attack scor
# Noob attacks Scorpion with 2 kicks and 1 punch.
# Scorpion is dead