class Codebreaker::Entities::Menu

Constants

COMMANDS_LIST
CONFIRM_COMMAND
HINT_COMMAND
MAX_SIZE
MIN_SIZE

Attributes

game[R]
guess[R]
storage[R]
viewer[R]

Public Class Methods

new() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 23
def initialize
  @storage = Storage.new
  @viewer = Viewer.new
  @game = Game.new
  @statistics = Statistics.new
end

Public Instance Methods

ask(phrase_key = nil, options = {}) click to toggle source
# File lib/codebreaker/entities/menu.rb, line 53
def ask(phrase_key = nil, options = {})
  viewer.message(phrase_key, options) if phrase_key
  gets.chomp
end
choice_code_process() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 94
def choice_code_process
  case guess
  when HINT_COMMAND then hint_process
  when COMMANDS_LIST[:exit] then game_menu
  else handle_command
  end
end
choice_menu_process(command) click to toggle source
# File lib/codebreaker/entities/menu.rb, line 132
def choice_menu_process(command)
  case command
  when COMMANDS_LIST[:start] then start
  when COMMANDS_LIST[:exit] then exit_from_game
  when COMMANDS_LIST[:rules] then rules
  when COMMANDS_LIST[:stats] then stats
  else
    viewer.command_error
    game_menu
  end
end
exit_from_game() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 127
def exit_from_game
  viewer.goodbye_message
  exit
end
game_menu() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 30
def game_menu
  viewer.start_message

  choice_menu_process(ask(:choice_options, commands: COMMANDS_LIST.keys.join(' | ')))
end
game_process() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 84
def game_process
  while game.attempts.positive?
    @guess = ask
    return handle_win if game.check_win?(guess)

    choice_code_process
  end
  handle_lose
end
generate_game(difficulty) click to toggle source
# File lib/codebreaker/entities/menu.rb, line 77
def generate_game(difficulty)
  game.init_game(difficulty)
  viewer.message(:difficulty,
                 hints: difficulty[:hints],
                 attempts: difficulty[:attempts])
end
handle_command() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 102
def handle_command
  return viewer.command_error unless format_check?(guess)

  p game.start(guess)
  viewer.round_message
  game.attempt_wasted
end
handle_lose() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 116
def handle_lose
  viewer.lost_game_message(game.code)
  game_menu
end
handle_win() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 110
def handle_win
  viewer.win_game_message
  save_result
  game_menu
end
hint_process() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 121
def hint_process
  return viewer.no_hints_message if game.hints_not_remain?

  viewer.print_hint(game.hint_take)
end
level_choice() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 66
def level_choice
  loop do
    level = ask(:difficulty_level, levels: Game::DIFFICULTIES.keys.join(' | '))

    return generate_game(Game::DIFFICULTIES[level.to_sym]) if Game::DIFFICULTIES[level.to_sym]
    return game_menu if level == COMMANDS_LIST[:exit]

    viewer.command_error
  end
end
name_valid?(name) click to toggle source
# File lib/codebreaker/entities/menu.rb, line 62
def name_valid?(name)
  !empty_check?(name) && size_check(name, MIN_SIZE, MAX_SIZE)
end
render_stats(list) click to toggle source
# File lib/codebreaker/entities/menu.rb, line 154
def render_stats(list)
  list.each_with_index do |key, index|
    puts "#{index + 1}: "
    key.each { |param, value| puts "#{param}:#{value}" }
  end
end
rules() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 36
def rules
  viewer.rules
  game_menu
end
save_result() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 58
def save_result
  storage.save_game_results(game.to_h(@name)) if ask(:save_results_message) == CONFIRM_COMMAND[:yes]
end
start() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 41
def start
  @name = user_registration
  level_choice
  game_process
end
stats() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 47
def stats
  scores = storage.load
  render_stats(@statistics.stats(scores)) if scores
  game_menu
end
user_registration() click to toggle source
# File lib/codebreaker/entities/menu.rb, line 144
def user_registration
  loop do
    name = ask(:registration)

    return name if name_valid?(name)

    viewer.registration_name_length_error
  end
end