class MasterMind::Tobi::GameHelper

Public Instance Methods

ask_level() click to toggle source
# File lib/mastermind/tobi/gamehelper.rb, line 68
def ask_level
  print UI::LEVEL_MESSAGE
  option_chosen = false
  
  while !option_chosen
    option_chosen = true                              # assume user selects valid level so as to quit loop
    
    input = gets.chomp.downcase
    case input                                        
    when "b", "beginner" then return GameLogic::BEGINNER
    when "i", "intermediate" then return GameLogic::INTERMEDIATE
    when "a", "advanced" then return GameLogic::ADVANCED
    else                                               # user selects an invalid level
      print UI::INVALID_MESSAGE
      option_chosen = false
    end  
  end
end
ask_mode(sequence, game_logic) click to toggle source
# File lib/mastermind/tobi/gamehelper.rb, line 41
def ask_mode(sequence, game_logic)
  print UI::MODE_SELECT
  option_chosen = false
  
  while !option_chosen
    option_chosen = true                              # assume user selects valid option so as to quit loop
    
    input = gets.chomp.downcase
    case input
    when "s", "single" then SinglePlayer.new(sequence, game_logic).start_game
    when "m", "multi"
      print UI::PASSWORD_MESSAGE
      hide_guess = MasterMind::Tobi::GameMethods.new.yes_or_no?
      MultiPlayer.new(sequence, game_logic, hide_guess).start_game
    else 
      print UI::INVALID_MESSAGE
      option_chosen = false
    end
  end
  
end
play_game() click to toggle source
# File lib/mastermind/tobi/gamehelper.rb, line 33
def play_game
  game_logic = GameLogic.new(ask_level); sequence = game_logic.generate_sequence
  ask_mode(sequence, game_logic)
  puts ""
  print UI::OPTIONS_MESSAGE + UI::INPUT_PROMPT
  user_choice
end
print_help() click to toggle source
user_choice() click to toggle source
# File lib/mastermind/tobi/gamehelper.rb, line 9
def user_choice
  option_chosen = false
  
  while !option_chosen
    option_chosen = true                              # assume user selects valid option so as to quit loop
    
    input = gets.chomp.downcase
    option_chosen = validate_input?(input)
  end
end
validate_input?(input) click to toggle source
# File lib/mastermind/tobi/gamehelper.rb, line 20
def validate_input?(input)
  case input
    when "p", "play" then play_game
    when "r", "read"
      print_help
      return false                                     # continue loop after displaying help message
    when "q", "quit" then exit
    else                                               # user selects an invalid option
      print UI::INVALID_MESSAGE
      return false
  end
end