Class: Codebreaker::Game

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

Constant Summary collapse

DIFFICULTIES =
{
  easy: { attempts: 15, hints: 2, hardness: 1 },
  medium: { attempts: 10, hints: 1, hardness: 2 },
  hell: { attempts: 5, hints: 1, hardness: 3 }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player, difficulty_type) ⇒ Game

Returns a new instance of Game.



11
12
13
14
15
16
# File 'lib/codebreaker/game.rb', line 11

def initialize(player, difficulty_type)
  @player = player
  @difficulty_type = difficulty_type
  @attempts_left = difficulty[:attempts]
  @is_win = false
end

Instance Attribute Details

#attempts_leftObject (readonly)

Returns the value of attribute attempts_left.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def attempts_left
  @attempts_left
end

#difficulty_typeObject (readonly)

Returns the value of attribute difficulty_type.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def difficulty_type
  @difficulty_type
end

#playerObject (readonly)

Returns the value of attribute player.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def player
  @player
end

Instance Method Details

#attempts_totalObject



49
50
51
# File 'lib/codebreaker/game.rb', line 49

def attempts_total
  difficulty[:attempts]
end

#guess_secret_code(guess) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/codebreaker/game.rb', line 29

def guess_secret_code(guess)
  ensure_game_is_not_over
  @attempts_left -= 1
  matching = SecretCodeMatcher.new(secret_code, guess).call
  check_win(guess)
  matching
end

#hints_leftObject



57
58
59
# File 'lib/codebreaker/game.rb', line 57

def hints_left
  hints.count
end

#hints_totalObject



53
54
55
# File 'lib/codebreaker/game.rb', line 53

def hints_total
  difficulty[:hints]
end

#lose?Boolean

Returns:

  • (Boolean)


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

def lose?
  attempts_left.zero? && !win?
end

#over?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/codebreaker/game.rb', line 45

def over?
  win? || lose?
end

#save_resultObject



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

def save_result
  raise Errors::GameSaveError unless win?

  CreateStatService.new(self).call
end

#secret_codeObject



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

def secret_code
  @secret_code ||= SecretCodeGenerator.new.call
end

#take_hintObject



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

def take_hint
  ensure_game_is_not_over
  raise Errors::NoHintsError if hints.empty?

  hints.pop
end

#win?Boolean

Returns:

  • (Boolean)


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

def win?
  @is_win
end