Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker_gapdn/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.



10
11
12
13
14
15
# File 'lib/codebreaker_gapdn/game.rb', line 10

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

Instance Attribute Details

#attempts_counterObject (readonly)

Returns the value of attribute attempts_counter.



8
9
10
# File 'lib/codebreaker_gapdn/game.rb', line 8

def attempts_counter
  @attempts_counter
end

#current_difficultyObject (readonly)

Returns the value of attribute current_difficulty.



8
9
10
# File 'lib/codebreaker_gapdn/game.rb', line 8

def current_difficulty
  @current_difficulty
end

#hinted_numbersObject (readonly)

Returns the value of attribute hinted_numbers.



8
9
10
# File 'lib/codebreaker_gapdn/game.rb', line 8

def hinted_numbers
  @hinted_numbers
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



8
9
10
# File 'lib/codebreaker_gapdn/game.rb', line 8

def secret_code
  @secret_code
end

Instance Method Details

#check_number_match(guess) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/codebreaker_gapdn/game.rb', line 17

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

  number_match(exact_match)
end

#lose?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/codebreaker_gapdn/game.rb', line 33

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

#take_hintObject



25
26
27
# File 'lib/codebreaker_gapdn/game.rb', line 25

def take_hint
  @hinted_numbers.pop
end

#win?(string) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/codebreaker_gapdn/game.rb', line 29

def win?(string)
  string == win_string
end