Class: Console
- Inherits:
-
Object
- Object
- Console
- Defined in:
- lib/codebreaker/console.rb
Instance Method Summary collapse
- #confirmation ⇒ Object
- #guess(input) ⇒ Object
- #round ⇒ Object
- #save_game_result ⇒ Object
- #start ⇒ Object
- #try_again ⇒ Object
- #use_hint ⇒ Object
Instance Method Details
#confirmation ⇒ Object
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 |
#round ⇒ Object
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_result ⇒ Object
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 |
#start ⇒ Object
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_again ⇒ Object
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_hint ⇒ Object
26 27 28 |
# File 'lib/codebreaker/console.rb', line 26 def use_hint puts @game.hint || 'All hints are used!' end |