class Codebreaker::Console

Public Class Methods

new() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 5
def initialize
  @game = CodebreakerGem::Game.new
  @game.generate_code
  @guess = ''
end

Public Instance Methods

get_guess() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 51
def get_guess
  puts "Enter your guess"
  @guess = gets.chomp
  raise 'You must enter 4 numbers' if @guess.size < 4
end
new_game() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 30
def new_game
  Codebreaker::Console.new.run
end
run() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 11
def run
  while @game.attempts > 0  do
    get_guess
    case @guess
      when @game.secret_code
        you_won
        break
      when 'hint'
        @game.get_hint
        show_hint
      else
        @game.guess = @guess
        @game.check
        show_response
      end
  end
  you_loose if @game.attempts <= 0
end
save_game() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 74
def save_game
  puts 'Enter your name'
  scores = @game.get_scores
  scores[:user_name] = gets.chomp
  @game.save_achievement(scores)
end
show_all_achievements() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 67
def show_all_achievements
  puts 'Achievements table'
  @game.read_achievements.each do |line|
    puts line
  end
end
show_hint() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 46
def show_hint
  puts "You have #{@game.hints} hints"
  puts @game.hint ? "HINT: #{@game.hint}" : 'Sorry, but you have used all the hints :('
end
show_response() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 62
def show_response
  puts "You have #{@game.attempts} attempts"
  puts @game.response
end
want_more?() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 57
def want_more?
  puts 'Want more? y/n'
  return ['y','Y','д','Д'].include?(gets.chomp) ? true : false
end
you_loose() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 41
def you_loose
  puts 'You loose :('
  new_game if want_more?
end
you_won() click to toggle source
# File lib/codebreaker_gem/codebreaker.rb, line 34
def you_won
  puts 'You won!'
  save_game
  show_all_achievements
  new_game if want_more?
end