Class: PlayerOption

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

Instance Method Summary collapse

Constructor Details

#initializePlayerOption

Returns a new instance of PlayerOption.



7
8
9
10
# File 'lib/player_option.rb', line 7

def initialize
  # Create instance of TTY Prompt
  @prompt = TTY::Prompt.new
end

Instance Method Details

#get_selectionObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/player_option.rb', line 38

def get_selection
  # Get ingredient names (array of strings)
  ingredient_names = []
  Recipe.ingredient_lists[0].each do |list|
    ingredient_names << list.keys.join
  end

  # Remove duplicate last value
  ingredient_names.pop

  # Variable to collect user input of item and quntity
  player_recipe = []

  # Loop prompt until player done selecting ingredients
  loop do
    # Loop through ingredients for player to choose
    puts
    item = @prompt.select("What would you like to add? (stack from bottom up)") do |item|
      ingredient_names.each do |ingredient|
        item.choice ingredient
      end

      # Option to finish selecting
      item.choice "Done"
    end

    # Exit loop if Done
    break if item === "Done"

    # Ask for quantity
    puts "How many \"#{item}\"? (Enter 0 to 5)"
    # Quantity input validation loop
    while quantity = $stdin.gets.strip do
      # Must be a whole number 0 to 5
      if (quantity =~ /^[0-5]$/)
        break
      else
        puts "Please enter a number from 0 to 5:"
      end
    end

    # Collect player's selections
    player_recipe << { item => quantity.to_i }
  end

  # Array of ingredient-quantity hashes
  player_recipe
end

#launch_gameObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/player_option.rb', line 12

def launch_game
  player_response = @prompt.select("Do you want to launch the game?") do |menu|
    menu.choice "Launch Game"
    menu.choice "Exit"
  end

  if player_response == "Launch Game"
    true
  else
    false
  end
end

#start_gameObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/player_option.rb', line 25

def start_game
  player_response = @prompt.select("What would you like to do?") do |menu|
    menu.choice "View 'How to Play'"
    menu.choice "Start Game"
  end

  if player_response == "Start Game"
    true
  else
    false
  end
end