Class: Codebreaker::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker/console.rb

Constant Summary collapse

COMMANDS =
{
  start: 'start',
  rules: 'rules',
  stats: 'stats',
  exit: 'exit'
}.freeze
HINT =
'hint'
ANSWERS =
{ yes: 'yes' }.freeze

Instance Method Summary collapse

Instance Method Details

#hint_showObject



37
38
39
40
41
42
# File 'lib/codebreaker/console.rb', line 37

def hint_show
  return message(:over_hint) if @game.hints.zero?

  puts @game.show_hints
  @game.take_hints
end

#launchObject



16
17
18
19
20
21
# File 'lib/codebreaker/console.rb', line 16

def launch
  message(:start_game)
  message(:wel_instruct, COMMANDS)
  @answer = read_from_console
  check_answer
end

#looseObject



51
52
53
54
# File 'lib/codebreaker/console.rb', line 51

def loose
  message(:lose)
  continue_game? ? start_game : exit
end

#round_gameObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/codebreaker/console.rb', line 23

def round_game
  while @game.attempts.positive?
    user_answer = message_game_read_console
    next hint_show if user_answer == HINT
    next message(:invalid_number) unless @game.validate_code(user_answer, Codebreaker::Game::USER_ANSWER_REX)

    @game.handle_guess(user_answer)
    message_game_result
    return win if @game.equal_codes?(user_answer)

  end
  loose
end

#stats_showObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/codebreaker/console.rb', line 56

def stats_show
  @stat = Codebreaker::Statistics.new
  return message(:empty_stat) unless data = @stat.stats

  message(:stats)
  message(:col_table)
  data.each_with_index do |row, index|
    print "#{index}\t"
    row.each do |_key, cell|
      print "#{cell}\t"
    end
    print "\n"
  end
end

#winObject



44
45
46
47
48
49
# File 'lib/codebreaker/console.rb', line 44

def win
  message(:win)
  message(:progress)
  @game.save if read_from_console.eql? ANSWERS[:yes]
  continue_game? ? start_game : exit
end