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
14
# File 'lib/codebreaker/game.rb', line 8

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

Instance Attribute Details

#made_guessesObject (readonly)

Returns the value of attribute made_guesses.



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

def made_guesses
  @made_guesses
end

#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



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

def available_attempts
  ATTEMPTS - @used_attempts
end

#available_hintsObject



35
36
37
# File 'lib/codebreaker/game.rb', line 35

def available_hints
  HINTS - @used_hints
end

#hintObject



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

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

#make_guess(user_code) ⇒ Object



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

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
  result = exact_matches + number_matches
  @made_guesses[user_code] = result
  result
end