class Menu

Constants

CHOOSE_COMMANDS
COMMANDS
HINT_COMMAND
MAX_SIZE_VALUE
MIN_SIZE_VALUE

Attributes

game[R]
guess[R]
renderer[R]
storage[R]

Public Class Methods

new() click to toggle source
# File lib/app/entities/menu.rb, line 20
def initialize
  @storage = DataStorage.new
  @renderer = Renderer.new
  @game = Game.new
  @statistics = Statistics.new
end

Public Instance Methods

game_menu() click to toggle source
# File lib/app/entities/menu.rb, line 27
def game_menu
  renderer.start_message
  choice_menu_process(ask(:choice_options, commands: COMMANDS.keys.join(' | ')))
end

Private Instance Methods

ask(phrase_key = nil, options = {}) click to toggle source
# File lib/app/entities/menu.rb, line 51
def ask(phrase_key = nil, options = {})
  renderer.message(phrase_key, options) if phrase_key
  gets.chomp
end
choice_code_process() click to toggle source
# File lib/app/entities/menu.rb, line 102
def choice_code_process
  case guess
  when HINT_COMMAND then hint_process
  when COMMANDS[:exit] then game_menu
  else handle_command
  end
end
choice_menu_process(command_name) click to toggle source
# File lib/app/entities/menu.rb, line 140
def choice_menu_process(command_name)
  case command_name
  when COMMANDS[:start] then start
  when COMMANDS[:exit] then exit_from_game
  when COMMANDS[:rules] then rules
  when COMMANDS[:stats] then stats
  else
    renderer.command_error
    game_menu
  end
end
exit_from_game() click to toggle source
# File lib/app/entities/menu.rb, line 135
def exit_from_game
  renderer.goodbye_message
  exit
end
game_process() click to toggle source
# File lib/app/entities/menu.rb, line 92
def game_process
  while game.attempts.positive?
    @guess = ask
    return handle_win if game.win?(guess)

    choice_code_process
  end
  handle_lose
end
generate_game(difficulty) click to toggle source
# File lib/app/entities/menu.rb, line 85
def generate_game(difficulty)
  game.generate(difficulty)
  renderer.message(:difficulty,
                   hints: difficulty[:hints],
                   attempts: difficulty[:attempts])
end
handle_command() click to toggle source
# File lib/app/entities/menu.rb, line 110
def handle_command
  return renderer.command_error unless check_command_range(guess)

  p game.start_process(guess)
  renderer.round_message
  game.decrease_attempts!
end
handle_lose() click to toggle source
# File lib/app/entities/menu.rb, line 124
def handle_lose
  renderer.lost_game_message(game.code)
  game_menu
end
handle_win() click to toggle source
# File lib/app/entities/menu.rb, line 118
def handle_win
  renderer.win_game_message
  save_result
  game_menu
end
hint_process() click to toggle source
# File lib/app/entities/menu.rb, line 129
def hint_process
  return renderer.no_hints_message? if game.hints_spent?

  renderer.print_hint_number(game.take_a_hint!)
end
level_choice() click to toggle source
# File lib/app/entities/menu.rb, line 74
def level_choice
  loop do
    level = ask(:hard_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[:exit]

    renderer.command_error
  end
end
name_valid?(name) click to toggle source
# File lib/app/entities/menu.rb, line 70
def name_valid?(name)
  !check_emptyness(name) && check_length(name, MIN_SIZE_VALUE, MAX_SIZE_VALUE)
end
registrate_user() click to toggle source
# File lib/app/entities/menu.rb, line 60
def registrate_user
  loop do
    name = ask(:registration)

    return name if name_valid?(name)

    renderer.registration_name_length_error
  end
end
render_stats(list) click to toggle source
# File lib/app/entities/menu.rb, line 152
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/app/entities/menu.rb, line 34
def rules
  renderer.rules
  game_menu
end
save_result() click to toggle source
# File lib/app/entities/menu.rb, line 56
def save_result
  storage.save_game_result(game.to_h(@name)) if ask(:save_results_message) == CHOOSE_COMMANDS[:yes]
end
start() click to toggle source
# File lib/app/entities/menu.rb, line 39
def start
  @name = registrate_user
  level_choice
  game_process
end
stats() click to toggle source
# File lib/app/entities/menu.rb, line 45
def stats
  scores = storage.load
  render_stats(@statistics.stats(scores)) if scores
  game_menu
end