Class: FortuneTeller::Game

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

Constant Summary collapse

DEFAULT_SELECTION_GROUPS =
[
  lambda { Chooser.subset([ "dog", "chicken", "mouse", "cat", "dragon", "turtle", "tiger", "snake", "monkey" ], 4) },
  lambda { Chooser.subset([ "3", "9", "5", "7", "10", "27", "100" ], 4) },
  lambda { Chooser.subset([ "pink", "green", "blue", "yellow" ], 2) },
].freeze
DEFAULT_FORTUNES =
[
  "Oops, you're in Jail!",
  "Yuck, you're in the mud!",
  "Wow, you get a cookie!",
  "Ouch, you got bit!",
  "Hey, you get a hug!",
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selection_groups, fortunes, options = {}) ⇒ Game

Returns a new instance of Game.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fortune_teller/game.rb', line 29

def initialize( selection_groups, fortunes, options = {} )
  @original_selection_groups = selection_groups
  @original_fortunes = fortunes
  
  @ui = options[:ui] || CliUi.new(:game => self)

  @chooser_class = options[:choose_class] || Chooser
  @reveal_class = options[:reveal_class] || Reveal
  @panel_class = options[:panel_class] || Panel
  @chooser_options = options[:chooser_options] || {}
  @chooser_options[:ui] ||= ui

  reset
end

Instance Attribute Details

#chooser_classObject (readonly)

Returns the value of attribute chooser_class.



26
27
28
# File 'lib/fortune_teller/game.rb', line 26

def chooser_class
  @chooser_class
end

#chooser_optionsObject (readonly)

Returns the value of attribute chooser_options.



27
28
29
# File 'lib/fortune_teller/game.rb', line 27

def chooser_options
  @chooser_options
end

#fortunesObject (readonly)

Returns the value of attribute fortunes.



25
26
27
# File 'lib/fortune_teller/game.rb', line 25

def fortunes
  @fortunes
end

#panel_classObject (readonly)

Returns the value of attribute panel_class.



26
27
28
# File 'lib/fortune_teller/game.rb', line 26

def panel_class
  @panel_class
end

#reveal_classObject (readonly)

Returns the value of attribute reveal_class.



26
27
28
# File 'lib/fortune_teller/game.rb', line 26

def reveal_class
  @reveal_class
end

#selection_groupsObject (readonly)

Returns the value of attribute selection_groups.



25
26
27
# File 'lib/fortune_teller/game.rb', line 25

def selection_groups
  @selection_groups
end

#uiObject (readonly)

Returns the value of attribute ui.



28
29
30
# File 'lib/fortune_teller/game.rb', line 28

def ui
  @ui
end

Class Method Details

.run(options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/fortune_teller/game.rb', line 19

def self.run( options = {} )
  options[:selection_groups] ||= DEFAULT_SELECTION_GROUPS.map(&:call)
  options[:fortunes] ||= DEFAULT_FORTUNES
  new( options.delete(:selection_groups), options.delete(:fortunes), options ).run
end

Instance Method Details

#resetObject



49
50
51
52
# File 'lib/fortune_teller/game.rb', line 49

def reset
  @fortunes = @original_fortunes.dup
  @selection_groups = @original_selection_groups.dup
end

#restartObject



44
45
46
47
# File 'lib/fortune_teller/game.rb', line 44

def restart
  reset
  run
end

#runObject



54
55
56
# File 'lib/fortune_teller/game.rb', line 54

def run
  chooser_tree.choose
end