class Codebreaker::Console

Attributes

game[R]
guess[R]
player[R]

Public Instance Methods

welcome_message() click to toggle source
# File lib/codebreaker/console.rb, line 8
def welcome_message
  output(:welcome_message)
  run
end

Private Instance Methods

difficulty_setup() click to toggle source
# File lib/codebreaker/console.rb, line 59
def difficulty_setup
  loop do
    output(:output_difficulty)
    return true if game.set(user_input)
  end
end
game_cycle() click to toggle source
# File lib/codebreaker/console.rb, line 45
def game_cycle
  output(:mode_info, game.difficulty)
  game_cycle_logic until game.lost?
  lost
end
game_cycle_logic() click to toggle source
# File lib/codebreaker/console.rb, line 51
def game_cycle_logic
  round_menu
  @guess = user_input
  hint_menu if guess == I18n.t(:hint)
  mark if game.guess_valid?(guess)
  won if game.won?(guess)
end
hint_menu() click to toggle source
# File lib/codebreaker/console.rb, line 72
def hint_menu
  output(:no_hints_left) if game.hint_keeper.empty?
  output(:take_hint, take_hint: game.take_hint!)
end
lost() click to toggle source
# File lib/codebreaker/console.rb, line 87
def lost
  output(:game_lost, secret_code: game.secret_code.join(''))
end
mark() click to toggle source
# File lib/codebreaker/console.rb, line 77
def mark
  game.use_attempt! && (puts play(guess))
end
output(*message) click to toggle source
# File lib/codebreaker/console.rb, line 123
def output(*message)
  puts I18n.t(*message)
end
play(guess) click to toggle source
# File lib/codebreaker/console.rb, line 101
def play(guess)
  guess = guess.chars.map(&:to_i)
  game.guess(guess)
end
play_again() click to toggle source
# File lib/codebreaker/console.rb, line 96
def play_again
  output(:play_again)
  user_input == 'yes' ? run : exit
end
registration() click to toggle source
# File lib/codebreaker/console.rb, line 34
def registration
  @player = Player.new
  loop do
    output(:output_name)
    player.name = user_input
    return true if player.valid?

    puts player.errors.first
  end
end
round_menu() click to toggle source
# File lib/codebreaker/console.rb, line 66
def round_menu
  output(:remained_attempts, attempts: game.attempts)
  output(:hints_left, hints: game.hint_keeper.size)
  output(:input_guess)
end
run() click to toggle source
# File lib/codebreaker/console.rb, line 15
def run
  loop do
    @game = Game.new
    output(:game_navigation)
    case user_input
    when I18n.t(:start) then start_game_preparations
    when I18n.t(:rule) then output(:rules)
    when I18n.t(:stats) then statistic(sort_player)
    else output(:wrong_command)
    end
  end
end
save_result() click to toggle source
# File lib/codebreaker/console.rb, line 91
def save_result
  output(:save)
  game.save(game.to_yaml(player.name)) if user_input == 'yes'
end
start_game_preparations() click to toggle source
# File lib/codebreaker/console.rb, line 28
def start_game_preparations
  return unless registration && difficulty_setup

  game_cycle
end
statistic(array) click to toggle source
# File lib/codebreaker/console.rb, line 106
def statistic(array)
  puts I18n.t(:statistic)
  rating = 1

  array.each do |e|
    header_part2 = "|#{e[:try]}\t\t|#{e[:hints_total]}\t\t|#{e[:hints_used]}\t\t|"
    header_part1 = "|#{rating}\t|#{e[:name]}\t|#{e[:difficulty]}\t\t|#{e[:attempts_total]}\t\t|#{e[:attempts_used]}"
    puts header_part1 + header_part2
    rating += 1
  end
end
user_input() click to toggle source
# File lib/codebreaker/console.rb, line 118
def user_input
  input = gets.chomp
  input == I18n.t(:exit) ? exit : input
end
won() click to toggle source
# File lib/codebreaker/console.rb, line 81
def won
  output(:game_won, secret_code: game.secret_code.join(''))
  save_result
  play_again
end