class Codebreaker::Console

Constants

COMMAND

Attributes

game[R]
user[R]

Public Instance Methods

menu() click to toggle source

Private Instance Methods

before_round() click to toggle source
# File lib/codebreaker/console.rb, line 72
def before_round
  show(:ENTER_GUESS, quit: COMMAND[:quit])
  show(:HINT_PROPOSAL, hint: COMMAND[:use_tip]) if @game.hints_left?
end
game_level_definer() click to toggle source

rubocop:disable Metrics/LineLength

# File lib/codebreaker/console.rb, line 43
def game_level_definer
  loop do
    show(:CHOOSE_DIFFICULTY, low: Game::LEVELS.keys[0].to_s, middle: Game::LEVELS.keys[1].to_s, high: Game::LEVELS.keys[2].to_s, quit: COMMAND[:quit])
    return if @game.set(input)

    show(:MENU_ERROR)
  end
end
game_over?() click to toggle source
# File lib/codebreaker/console.rb, line 82
def game_over?
  @game.win? || @game.lost?
end
input() click to toggle source
# File lib/codebreaker/console.rb, line 103
def input
  data_in = gets.chomp
  data_in == COMMAND[:quit] ? quit : data_in
end
play_game() click to toggle source

rubocop:enable Metrics/LineLength

# File lib/codebreaker/console.rb, line 54
def play_game
  loop do
    before_round
    in_round_input = input
    next provide_hint if in_round_input == COMMAND[:use_tip]

    round(in_round_input)
    next if @game.errors.any?

    puts @game.start
    return the_end if game_over?
  end
end
pre_game_process() click to toggle source
# File lib/codebreaker/console.rb, line 23
def pre_game_process
  registration
  @game = Game.new
  game_level_definer
  play_game
end
provide_hint() click to toggle source
# File lib/codebreaker/console.rb, line 68
def provide_hint
  @game.hints_left? ? (puts @game.hint) : show(:NO_HINTS_MESSAGE)
end
quit() click to toggle source
# File lib/codebreaker/console.rb, line 116
def quit
  show(:GOODBYE)
  exit
end
registration() click to toggle source
# File lib/codebreaker/console.rb, line 30
def registration
  loop do
    show(:NAME_PROPOSAL, quit: COMMAND[:quit])
    @user = User.new
    @user.name = input
    return if @user.errors.empty?

    puts @user.errors.first
  end
end
round(in_round_input) click to toggle source
# File lib/codebreaker/console.rb, line 77
def round(in_round_input)
  @game.guess(in_round_input)
  puts @game.errors.first
end
save_game() click to toggle source
# File lib/codebreaker/console.rb, line 94
def save_game
  show(:SAVE_MESSAGE, quit: COMMAND[:quit])
  return saving_process if input == COMMAND[:yes]
end
saving_process() click to toggle source
# File lib/codebreaker/console.rb, line 99
def saving_process
  Database.save(@game.to_h(@user.name))
end
show(*message) click to toggle source
# File lib/codebreaker/console.rb, line 108
def show(*message)
  puts I18n.t(*message)
end
show_stats() click to toggle source
# File lib/codebreaker/console.rb, line 112
def show_stats
  tp Database.load_stats, :difficulty, :name, :attempts_total, :attempts_used, :hints_total, :hints_used
end
the_end() click to toggle source
# File lib/codebreaker/console.rb, line 86
def the_end
  show(:CORRECT_CODE, code: @game.secret_code.join.to_s)
  return show(:LOSE_MESSAGE) if @game.lost?

  show(:WIN_MESSAGE)
  save_game
end