Class: Console

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

Instance Method Summary collapse

Instance Method Details

#confirmationObject



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

def confirmation
  gets.to_s.strip.casecmp('y').zero?
end

#guess(input) ⇒ Object



22
23
24
# File 'lib/codebreaker/console.rb', line 22

def guess(input)
  puts @game.guess(input) || 'Incorrect input!'
end

#roundObject



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

def round
  until @game.ended?
    print 'Your guess: '
    input = gets.to_s.strip
    input == 'hint' ? use_hint : guess(input)
  end
  puts "The round is over, you #{@game.winner? ? 'won' : 'loose'}!"
end

#save_game_resultObject



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

def save_game_result
  print 'Do you want to save result? (y/n): '
  @game.save if confirmation
end

#startObject



2
3
4
5
6
7
8
9
10
11
# File 'lib/codebreaker/console.rb', line 2

def start
  catch :finish do
    loop do
      @game = Codebreaker::Game.new
      round
      save_game_result
      try_again
    end
  end
end

#try_againObject



35
36
37
38
# File 'lib/codebreaker/console.rb', line 35

def try_again
  print 'Do you want to try again? (y/n): '
  throw :finish unless confirmation
end

#use_hintObject



26
27
28
# File 'lib/codebreaker/console.rb', line 26

def use_hint
  puts @game.hint || 'All hints are used!'
end