-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarehouse.rb
More file actions
36 lines (32 loc) · 815 Bytes
/
Copy pathwarehouse.rb
File metadata and controls
36 lines (32 loc) · 815 Bytes
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
require './truck.rb'
class Warehouse
attr_accessor :position
def initialize(position: , trucks: )
@position = position
@trucks = trucks
end
def check_trucks
if @trucks.all?{|truck| truck.position == @position}
exchange
return
end
if @trucks.any?{|truck| truck.should_gather?}
@trucks.each do |truck|
truck.warehouse = self
end
end
out_of_bound_cargos = @trucks.inject(0){|sum, truck| sum + truck.not_in_charge_cargos.length}
puts out_of_bound_cargos
if out_of_bound_cargos > @trucks.length * 5
@trucks.each do |truck|
truck.warehouse = self
end
end
end
def exchange
all_cargos = @trucks.map{|truck| truck.cargos}.flatten
@trucks.each do |truck|
truck.pick_mine(all_cargos)
end
end
end