class Codebreaker::Console

Attributes

game[RW]

Public Class Methods

new() click to toggle source
# File lib/codebreaker/console.rb, line 5
def initialize
  @game = Game.new
end

Public Instance Methods

play() click to toggle source
# File lib/codebreaker/console.rb, line 9
def play
  intro
  until game.completed?
    case answ = get_answer
      when /^[1-6]{4}/ then game.mark_user_code(answ)
      when /^[Hh]{1}/ then game.show_hint
      else puts 'You entered wrong value, pal. Try again'
    end
  end
  puts game.won? ? 'Great job! You guessed secret code!' : 'Oh, you exceed the number of available attempts'
  save_result_request
  play_again_request
end

Private Instance Methods

get_answer() click to toggle source
# File lib/codebreaker/console.rb, line 44
def get_answer
  gets.chomp
end
intro() click to toggle source
# File lib/codebreaker/console.rb, line 24
def intro
  puts 'Welcome to Codebreaker!'
  puts 'You need to break randomly generated secret code'
  puts 'You can input code or request hint(with H or h)'
  puts 'So, lets go!'
end
play_again_request() click to toggle source
# File lib/codebreaker/console.rb, line 36
def play_again_request
  puts 'Do you wanna play again?'
  return unless get_answer =~ /[Yy]/
  @game = Game.new
  system('clear')
  play
end
save_result_request() click to toggle source
# File lib/codebreaker/console.rb, line 31
def save_result_request
  puts 'Do you wanna save result?'
  game.save_result if get_answer =~ /[Yy]/
end