class Codebreaker::Console

Attributes

difficulty_name[R]
game[R]

Public Instance Methods

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

Private Instance Methods

ask(question) click to toggle source
# File lib/codebreaker/console.rb, line 64
def ask(question)
  output(question) if question
  gets.chomp
end
dichotomy_question?(question) click to toggle source
# File lib/codebreaker/console.rb, line 69
def dichotomy_question?(question)
  answer = ask(question).downcase
  %w[yes y yeap].include?(answer)
end
difficulty_rules() click to toggle source
# File lib/codebreaker/console.rb, line 99
def difficulty_rules
  Game::DIFFICULTIES.each do |diff_name, params|
    output("#{diff_name}: #{params[:attempts]} attempts and #{params[:hints]} hints")
  end
end
game_round() click to toggle source
# File lib/codebreaker/console.rb, line 33
def game_round
  while game.attempts.positive?
    user_answer = round_question(game.attempts)
    next hint if user_answer == 'hint'
    return win if game.equal_codes?(user_answer)
    round_message(user_answer)
  end
  lose
end
handle_difficulty() click to toggle source
# File lib/codebreaker/console.rb, line 43
def handle_difficulty
  @difficulty_name = ask(:choose_difficulty).to_sym
  return if Game::DIFFICULTIES.key? @difficulty_name
  output(:spelling_error)
  handle_difficulty
end
handle_statistics_for_output() click to toggle source
# File lib/codebreaker/console.rb, line 113
def handle_statistics_for_output
  statistics.map do |record|
    "#{record[:name]} won the game on #{record[:difficulty]} level and still had #{record[:attempts]} attempts and #{record[:hints]} hints"
  end
end
hint() click to toggle source
# File lib/codebreaker/console.rb, line 50
def hint
  msg = game.hints.empty? ? :no_hint : game.take_a_hint!
  output(msg)
end
lose() click to toggle source
# File lib/codebreaker/console.rb, line 94
def lose
  output(phrases[:lose] << game.secret_code.join)
  dichotomy_question?(:new_game) ? new_game : output(:goodbye)
end
new_game() click to toggle source
# File lib/codebreaker/console.rb, line 26
def new_game
  rules
  handle_difficulty
  @game = Codebreaker::Game.new(difficulty_name)
  game_round
end
output(message) click to toggle source
# File lib/codebreaker/console.rb, line 60
def output(message)
  puts message.is_a?(Symbol) ? phrases[message] : message
end
output_statistics() click to toggle source
# File lib/codebreaker/console.rb, line 79
def output_statistics
  handle_statistics_for_output.each { |record| output(record) }
  welcome_instructions
end
phrases() click to toggle source
# File lib/codebreaker/console.rb, line 105
def phrases
  @phrases ||= Codebreaker::Storage.load_file('phrases')
end
round_message(user_answer) click to toggle source
# File lib/codebreaker/console.rb, line 55
def round_message(user_answer)
  message = game.valid_answer?(user_answer) ? game.handle_guess(user_answer) : :invalid_number
  output(message)
end
round_question(attempts) click to toggle source
# File lib/codebreaker/console.rb, line 84
def round_question(attempts)
  ask("#{phrases[:round_question]}#{attempts}")
end
rules() click to toggle source
# File lib/codebreaker/console.rb, line 74
def rules
  output(:rules)
  difficulty_rules
end
score_data() click to toggle source
# File lib/codebreaker/console.rb, line 119
def score_data
  name = ask(:username)
  { name: name, difficulty: @difficulty_name, attempts: game.attempts, hints: game.hints.size }
end
statistics() click to toggle source
# File lib/codebreaker/console.rb, line 109
def statistics
  @statistics ||= Codebreaker::Storage.load_file('statistics')
end
welcome_instructions() click to toggle source
# File lib/codebreaker/console.rb, line 15
def welcome_instructions
  case ask(:welcome_instruction).downcase
  when 'game' then new_game
  when 'stats' then output_statistics
  when 'exit' then exit
  else
    output(:spelling_error)
    welcome_instructions
  end
end
win() click to toggle source
# File lib/codebreaker/console.rb, line 88
def win
  output(:win)
  Codebreaker::Storage.save_score(score_data) if dichotomy_question?(:save_score)
  dichotomy_question?(:new_game) ? new_game : output(:goodbye)
end