class Codebreaker::ConsoleApp

Public Class Methods

new() click to toggle source
# File lib/ep-codebreaker/console_app.rb, line 7
def initialize
  @game = Game.new
end

Public Instance Methods

run() click to toggle source
# File lib/ep-codebreaker/console_app.rb, line 11
def run
  Messages.logo
  Messages.welcome

  loop do
    Messages.run
    print '> '

    case gets.chomp
    when '1' then play
    when '2' then puts high_scores
    when '0'
      Messages.bye
      break
    else Messages.wrong_option
    end
  end
end

Private Instance Methods

check_guess(input) click to toggle source
# File lib/ep-codebreaker/console_app.rb, line 66
def check_guess(input)
  puts @game.check_guess(input)
  Messages.tries_left(@game.tries_left)
rescue ArgumentError => ex
  puts ex.message, "\n"
end
high_scores() click to toggle source
# File lib/ep-codebreaker/console_app.rb, line 55
def high_scores
  scores = @game.high_scores

  return 'No scores' if scores.empty?

  scores
    .map
    .with_index { |player, i| format("%2i #{player.formatted}", i + 1) }
    .unshift(format('%2s %10s %6s', '', 'Name', 'Points'))
end
play() click to toggle source
# File lib/ep-codebreaker/console_app.rb, line 32
def play
  @game.start
  Messages.play

  until @game.finished?
    print '>>> '
    input = gets.chomp

    case input
    when '1' then Messages.hint(@game.hint, @game.hints_left, @game.tries_left)
    when '0' then return
    else check_guess(input)
    end
  end

  if @game.won?
    Messages.win(@game.answer)
    save_score
  else
    Messages.lose(@game.answer)
  end
end
save_score() click to toggle source
# File lib/ep-codebreaker/console_app.rb, line 73
def save_score
  print 'Do you want to save your score? y/[n]: '

  return unless gets.chomp.casecmp('y').zero?

  begin
    print 'Type your name: '
    @game.save_score(gets.chomp)
    puts 'Your score was saved.'
  rescue ArgumentError => ex
    puts ex.message, "\n"
    retry
  end
end