Class: Codebreaker::Console

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

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



6
7
8
# File 'lib/console.rb', line 6

def initialize
  @game = Game.new
end

Instance Method Details

#playObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/console.rb', line 10

def play
  puts "You have #{Game::ATTEMPS} attemps and #{Game::HINT} hint.
  Enter 'hint' for get hint. And 'exit' for exit from a game."

  until @game.loose?
    case code = gets.chomp
      when 'hint'
      puts "The hint is #{ @game.get_hint }"
      when 'exit'
      exit
      when /^[1-6]{4}$/
      puts @game.match_guess(code)
    end
    break if @game.win?
  end
  
  save
  @game = Game.new if try_again?
  play
end

#saveObject



36
37
38
39
40
# File 'lib/console.rb', line 36

def save
  puts 'Enter your name:'
  user_name = gets.chomp
  @game.save_stat( user_name )
end

#try_again?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/console.rb', line 31

def try_again?
  puts 'Do you want try again?(y/n)'
  gets.chomp == 'y' ? true : exit
end