Class: LemonadeStand::Game
- Inherits:
-
Object
- Object
- LemonadeStand::Game
- Defined in:
- lib/lemonade_stand/game.rb
Instance Method Summary collapse
- #days ⇒ Object
-
#initialize(number_of_players) ⇒ Game
constructor
A new instance of Game.
- #make_choice(choice, options) ⇒ Object
- #players ⇒ Object
- #sales_results_for(player, day) ⇒ Object
- #start_a_new_day ⇒ Object
- #store_sales_results_for(results, player, day) ⇒ Object
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
#days ⇒ Object
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, player = [:player] day = [:day] || days.last results = day.sales_for choice player.assets += results.profit store_sales_results_for results, player, day end |
#players ⇒ Object
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_day ⇒ Object
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 |