class Codebreaker::GameConsole

Public Class Methods

new() click to toggle source
# File lib/console.rb, line 5
def initialize
  @game = Game.new
end

Public Instance Methods

play() click to toggle source
# File lib/console.rb, line 9
def play
  loop do
    choice = menu
    break if choice == '0'
    case choice
    when '1'
      play_game
      save
    when '2'
      show_table
    end
  end
end

Private Instance Methods

menu() click to toggle source
play_game() click to toggle source
# File lib/console.rb, line 28
def play_game      
  @game.start
  while (@game.num_of_try <= @game.count_of_try && !@game.game_status)
    puts 'Input 4 digits between 1 and 6 (q for quit or h for hint):'
    user_input = gets.chomp
    if user_input == 's'
      @game.instance_variable_set(:@secret_code, gets.chomp.chars.map(&:to_i))
      next
    end
    break if user_input == 'q'
    puts (user_input == 'h' ? @game.hint : @game.match_secret_code(user_input))
  end
end
save() click to toggle source
# File lib/console.rb, line 42
def save
  puts 'Do you want to save results(y/n)?'
  if (gets.chomp == 'y')
    puts 'Input your name:'
    @game.save_result(gets.chomp)
  end
end
show_table() click to toggle source
# File lib/console.rb, line 24
def show_table
  puts Game.load_results
end