Class: LemonadeStand::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/lemonade_stand/game.rb

Instance Method Summary collapse

Constructor Details

#initialize(number_of_players) ⇒ Game

Returns a new instance of Game.



5
6
7
# File 'lib/lemonade_stand/game.rb', line 5

def initialize number_of_players
  @number_of_players = number_of_players
end

Instance Method Details

#daysObject



17
18
19
# File 'lib/lemonade_stand/game.rb', line 17

def days
  @days ||= []
end

#make_choice(choice, options) ⇒ Object



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

def make_choice choice, options
  player  = options[:player]
  day     = options[:day] || days.last
  results = day.sales_for choice 
  player.assets += results.profit
  store_sales_results_for results, player, day
end

#playersObject



41
42
43
44
45
46
47
48
# File 'lib/lemonade_stand/game.rb', line 41

def players
  @players ||= (0...@number_of_players).map do |i|
                 p = LemonadeStand::Player.new
                 p.index = i
                 p.game = self
                 p
               end
end

#sales_results_for(player, day) ⇒ Object



34
35
36
37
38
39
# File 'lib/lemonade_stand/game.rb', line 34

def sales_results_for player, day
  @sales_results ||= []
  @sales_results.select do |record|
    return record[:results] if record[:player].object_id == player.object_id && record[:day].object_id == day.object_id
  end
end

#start_a_new_dayObject



21
22
23
24
25
26
27
# File 'lib/lemonade_stand/game.rb', line 21

def start_a_new_day
  @days ||= []
  day = LemonadeStand::Day.new
  @days << day
  day.number = @days.count
  day
end

#store_sales_results_for(results, player, day) ⇒ Object



29
30
31
32
# File 'lib/lemonade_stand/game.rb', line 29

def store_sales_results_for results, player, day
  @sales_results ||= []
  @sales_results << { player: player, day: day, results: results }
end