class CodebreakerRuban::Console

Constants

EASY_STRING
HELL_STRING
HINT
MEDIUM_STRING
OPTION
WIN
YES

Attributes

difficulty[RW]
game[RW]
state[RW]
user[RW]

Public Class Methods

new() click to toggle source
# File lib/app/entities/console.rb, line 20
def initialize
  set_state(:choice_option)
end

Public Instance Methods

run() click to toggle source
# File lib/app/entities/console.rb, line 24
def run
  Message.greeting
  loop do
    send(state)
    break if state == :game_over
  end
  Message.goodbye
end

Private Instance Methods

check_guess() click to toggle source
# File lib/app/entities/console.rb, line 76
def check_guess
  Message.enter_code
  process_user_input do |input|
    if input == HINT
      Message.display(@game.hint_use)
    else
      @game.validate_guess_input(input)
      @game.errors.empty? ? check_win(input) : Message.display(@game.errors)
    end
  end
end
check_win(input) click to toggle source
# File lib/app/entities/console.rb, line 88
def check_win(input)
  if @game.check_attemps
    check_secret_and_guess = @game.check_user_input(input)
    Message.display(check_secret_and_guess)
    Message.win if game_result(check_secret_and_guess)
  else
    Message.lose
    Message.show_secret_code(@game.secret_code)
    set_state(:restart_game)
  end
end
choice_difficulty() click to toggle source
# File lib/app/entities/console.rb, line 57
def choice_difficulty
  Message.choice_difficulty(EASY_STRING, MEDIUM_STRING, HELL_STRING)
  process_user_input do |input|
    @difficulty = case input
    when EASY_STRING then Game::DIFFICULTY[:easy]
    when MEDIUM_STRING then Game::DIFFICULTY[:medium]
    when HELL_STRING then Game::DIFFICULTY[:hell]
    end
    return Message.error_difficulty unless @difficulty

    set_state(:start_game)
  end
end
choice_name() click to toggle source
# File lib/app/entities/console.rb, line 48
def choice_name
  Message.enter_name
  process_user_input do |input|
    @user = User.new(input)
    @user.validate(input)
    @user.errors.empty? ? set_state(:choice_difficulty) : Message.display(@user.errors)
  end
end
choice_option() click to toggle source
# File lib/app/entities/console.rb, line 35
def choice_option
  Message.choice_option(*OPTION.values)
  process_user_input do |input|
    case input
    when OPTION[:start] then set_state(:choice_name)
    when OPTION[:rules] then Message.rules
    when OPTION[:stats] then Statistic.show_stats
    else
      Message.wrong_command
    end
  end
end
game_over() click to toggle source
# File lib/app/entities/console.rb, line 128
def game_over
  set_state(:game_over)
end
game_result(check_secret_and_guess) click to toggle source
# File lib/app/entities/console.rb, line 100
def game_result(check_secret_and_guess)
  return unless check_secret_and_guess == WIN

  set_state(:save_result)
end
process_user_input() { |input| ... } click to toggle source
# File lib/app/entities/console.rb, line 132
def process_user_input
  input = gets.chomp.downcase
  if input == OPTION[:exit]
    set_state(:game_over)
  else
    yield(input)
  end
end
restart_game() click to toggle source
# File lib/app/entities/console.rb, line 115
def restart_game
  Message.restart
  if YES.include? user_input
    set_state(:choice_option)
  else
    set_state(:game_over)
  end
end
save_result() click to toggle source
# File lib/app/entities/console.rb, line 106
def save_result
  Message.save
  if YES.include? user_input
    Storage.save_in_store(@game.to_h)
    Message.result_save
  end
  set_state(:restart_game)
end
set_state(console_state) click to toggle source
# File lib/app/entities/console.rb, line 141
def set_state(console_state)
  @state = console_state
end
start_game() click to toggle source
# File lib/app/entities/console.rb, line 71
def start_game
  @game = Game.new(@difficulty, @user)
  set_state(:check_guess)
end
user_input() click to toggle source
# File lib/app/entities/console.rb, line 124
def user_input
  gets.chomp.downcase
end