Class: Game

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

Constant Summary collapse

GUESS_PRESENCE =
'-'
GUESS_PLACE =
'+'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(difficulty) ⇒ Game

Returns a new instance of Game.



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

def initialize(difficulty)
  @secret_code = create_secret_code
  @guess = []
  @attempts_counter = 0
  difficulty(difficulty)
end

Instance Attribute Details

#attempts_counterObject (readonly)

Returns the value of attribute attempts_counter.



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

def attempts_counter
  @attempts_counter
end

#current_difficultyObject (readonly)

Returns the value of attribute current_difficulty.



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

def current_difficulty
  @current_difficulty
end

#hinted_numbersObject (readonly)

Returns the value of attribute hinted_numbers.



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

def hinted_numbers
  @hinted_numbers
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



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

def secret_code
  @secret_code
end

#winObject (readonly)

Returns the value of attribute win.



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

def win
  @win
end

Instance Method Details

#check_number_match(guess) ⇒ Object



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

def check_number_match(guess)
  @guess = guess
  @attempts_counter += 1
  if @guess == @secret_code
    @win = true
    return win_string
  end

  number_match(exact_match)
end

#difficulty(difficulty) ⇒ Object



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

def difficulty(difficulty)
  @current_difficulty = difficulty.level
  @hinted_numbers = @secret_code.sample(@current_difficulty[:hints])
end

#lose?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/codebreaker/game.rb', line 40

def lose?
  @attempts_counter >= @current_difficulty[:attempts]
end

#take_hintObject



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

def take_hint
  @hinted_numbers.pop
end

#win?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

def win?(string)
  string == win_string
end