Class: Codebreaker::Game

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

Constant Summary collapse

ATTEMPTS =
10
HINTS =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



8
9
10
11
12
13
# File 'lib/codebreaker/game.rb', line 8

def initialize
  @secret_code = generate
  @shuffled_secret_code = @secret_code.shuffle
  @used_attempts = 0
  @used_hints = 0
end

Instance Attribute Details

#used_attemptsObject (readonly)

Returns the value of attribute used_attempts.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def used_attempts
  @used_attempts
end

#used_hintsObject (readonly)

Returns the value of attribute used_hints.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def used_hints
  @used_hints
end

Instance Method Details

#available_attemptsObject



28
29
30
# File 'lib/codebreaker/game.rb', line 28

def available_attempts
  ATTEMPTS - @game.used_attempts
end

#available_hintsObject



32
33
34
# File 'lib/codebreaker/game.rb', line 32

def available_hints
  HINTS - @game.used_hints
end

#hintObject



15
16
17
18
# File 'lib/codebreaker/game.rb', line 15

def hint
  @used_hints += 1
  @shuffled_secret_code.pop
end

#make_guess(user_code) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/codebreaker/game.rb', line 20

def make_guess(user_code)
  return unless code_valid?(user_code)
  @used_attempts += 1
  @user_code = user_code.chars.map(&:to_i)
  return '++++' if @user_code == @secret_code
  exact_matches + number_matches
end