-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmy.rb
67 lines (56 loc) · 1.32 KB
/
army.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
class Army
attr_accessor :gold, :civilization, :units, :battles
DEFAULT_GOLD = 1000
def initialize(civilization)
@civilization = civilization
@units = []
@battles = []
@gold = Army::DEFAULT_GOLD
setting
end
def total_points
units.inject(0){ |sum,u| sum + u.current_points }
end
def greater_subtract_unit( quantity )
(1..quantity).each do |q|
destroy_unit
end
end
def attack( army )
results = BattleManager.new(self, army).war.results_battle
register_battle(results)
army.register_battle(results)
end
def register_battle( battle )
@battles << battle
end
def winner_battle
@battles.inject(0){ |sum, b| (b.wars_won == self) ? sum + 1 : sum }
end
def loser_battle
@battles.inject(0){ |sum, b| (b.wars_lost == self) ? sum + 1 : sum }
end
def find_unit(unit_type)
result = nil
@units.each do |unit|
if unit.unit_type.name.to_s == unit_type
return unit
end
end
result
end
private
def destroy_unit
unit = @units.max_by do |element|
element.current_points
end
@units.delete(unit)
end
def setting
@civilization.unit_settings.each do |setting|
(1..setting.unit_quantity).each do
units << Unit.new(self, setting.unit_type)
end
end
end
end