Class: Codebreaker::Console

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

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



5
6
7
8
9
# File 'lib/codebreaker_gem/codebreaker.rb', line 5

def initialize
  @game = CodebreakerGem::Game.new
  @game.generate_code
  @guess = ''
end

Instance Method Details

#get_guessObject



51
52
53
54
55
# 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_gameObject



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

def new_game
  Codebreaker::Console.new.run
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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_gameObject



74
75
76
77
78
79
# 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_achievementsObject



67
68
69
70
71
72
# 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_hintObject



46
47
48
49
# 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_responseObject



62
63
64
65
# File 'lib/codebreaker_gem/codebreaker.rb', line 62

def show_response
  puts "You have #{@game.attempts} attempts"
  puts @game.response
end

#want_more?Boolean

Returns:

  • (Boolean)


57
58
59
60
# 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_looseObject



41
42
43
44
# File 'lib/codebreaker_gem/codebreaker.rb', line 41

def you_loose
  puts 'You loose :('
  new_game if want_more?
end

#you_wonObject



34
35
36
37
38
39
# 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