Class: Violence::Town

Inherits:
Object
  • Object
show all
Defined in:
lib/violence/town.rb

Constant Summary collapse

POPULATION =
42

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTown

Returns a new instance of Town.



9
10
11
12
13
14
15
# File 'lib/violence/town.rb', line 9

def initialize
  @agents = Set.new

  POPULATION.times do
    @agents << Violence::Agent.new
  end
end

Instance Attribute Details

#agentsObject

Returns the value of attribute agents.



5
6
7
# File 'lib/violence/town.rb', line 5

def agents
  @agents
end

Instance Method Details

#inspectObject



33
34
35
36
37
38
39
# File 'lib/violence/town.rb', line 33

def inspect
  agentss = self.agents.sort_by {|agent| agent.greed}
  agentss.map do |agent|
    template = "Bank: %8.2f, Productivity: %2d, Greed: %4.2f, All ever owned: %6d"
    template % [agent.bank, agent.productivity, agent.greed, agent.allconsumed + agent.bank]
  end
end

#tickObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/violence/town.rb', line 17

def tick
  @agents.each do |agent|

    investcost = 2 ** agent.productivity
    if agent.bank >= investcost
      agent.invest(investcost)
    else
      agent.produce
    end

  end

  self.inspect

end