Class: Codebreaker::Game

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

Constant Summary

Constants included from FileStore

FileStore::FILE_DIRECTORY, FileStore::FILE_NAME

Constants included from Validation

Validation::NUMBERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FileStore

#load_file, #save_file, statistics

Methods included from Validation

#validate_guess, #validate_name, #validate_state

Constructor Details

#initialize(user) ⇒ Game

Returns a new instance of Game.



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

def initialize(user)
  @user = user
  @state = Constants::START_GAME
  @secret_code = SecretCodeGenerator.call
end

Instance Attribute Details

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



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

def secret_code
  @secret_code
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.show_difficultiesObject



51
52
53
# File 'lib/codebreaker/game.rb', line 51

def self.show_difficulties
  Constants::DIFFICULTIES
end

Instance Method Details

#attempts?Boolean

Returns:

  • (Boolean)


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

def attempts?
  (user.attempts <= Constants::DIFFICULTIES[@user.difficulty][:attempts]) && user.attempts.positive?
end

#generate_matrix(input) ⇒ Object



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

def generate_matrix(input)
  validate_state(@state, Constants::IN_GAME)
  validate_guess(input)
  user.decrement_attemts
  MatrixGenerator.new(input, @secret_code).call
end

#hints?Boolean

Returns:

  • (Boolean)


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

def hints?
  (user.hints <= Constants::DIFFICULTIES[@user.difficulty][:hints]) && user.hints.positive?
end

#lose?Boolean

Returns:

  • (Boolean)


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

def lose?
  return unless user.attempts.zero?

  @state = Constants::LOSE
  true
end

#startObject



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

def start
  validate_state(@state, Constants::START_GAME)
  @copied_code = @secret_code.dup
  @state = Constants::IN_GAME
end

#use_hintObject



55
56
57
58
59
60
61
62
# File 'lib/codebreaker/game.rb', line 55

def use_hint
  return if @copied_code.empty?

  hint = @copied_code.sample
  @copied_code.join.sub!(hint.to_s, "")
  user.decrement_hints
  hint
end

#win?(result) ⇒ Boolean

Returns:

  • (Boolean)


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

def win?(result)
  return false unless result == @secret_code

  @state = Constants::WIN
  true
end