Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Includes:
Storage, Validation
Defined in:
lib/codebreaker/game.rb

Constant Summary collapse

DIFFICULTY =
{
  hell: { attempts: 5, hint: 1, mode: 'hell' },
  medium: { attempts: 10, hint: 1, mode: 'medium' },
  easy: { attempts: 15, hint: 2, mode: 'easy' }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Storage

load, save, sort_player

Methods included from Validation

#class_valid?, #guess_valid?, #size_correct?

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @secret_code = 4.times.map { Random.rand(1..6) }
  @errors = []
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#hint_keeperObject (readonly)

Returns the value of attribute hint_keeper.



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

def hint_keeper
  @hint_keeper
end

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#playObject (readonly)

Returns the value of attribute play.



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

def play
  @play
end

#playerObject (readonly)

Returns the value of attribute player.



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

def player
  @player
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
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

#storageObject (readonly)

Returns the value of attribute storage.



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

def storage
  @storage
end

Instance Method Details

#attempt_usedObject



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

def attempt_used
  @attempts -= 1
end

#guess(guess) ⇒ Object



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

def guess(guess)
  @result = '+' * exact_match_count(guess) + '-' * number_match_count(guess)
end

#lost?Boolean

Returns:

  • (Boolean)


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

def lost?
  attempts.zero?
end

#set(user_input) ⇒ Object



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

def set(user_input)
  return unless DIFFICULTY.key?(user_input.to_sym)

  @difficulty = DIFFICULTY.dig(user_input.to_sym)
  @hints = @difficulty.dig(:hint)
  @attempts = @difficulty.dig(:attempts)
  @hint_keeper = @secret_code.sample(hints)
end

#take_hint!Object



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

def take_hint!
  hint_keeper.pop
end

#to_yaml(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/codebreaker/game.rb', line 42

def to_yaml(name)
  {
    name: name,
    difficulty: difficulty[:mode],
    attempts_total: difficulty[:attempts],
    attempts_used: difficulty[:attempts] - attempts,
    hints_total: difficulty[:hint],
    hints_used: difficulty[:hint] - hint_keeper.size
  }.to_yaml
end

#validate_guess(guess) ⇒ Object



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

def validate_guess(guess)
  guess_valid?(guess)
end

#won?(guess) ⇒ Boolean

Returns:

  • (Boolean)


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

def won?(guess)
  secret_code.join == guess
end