Class: Codebreaker::Game

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

Constant Summary collapse

DIFFICULTY =
{
  low: { attempts: 15, hints: 2 },
  medium: { attempts: 10, hints: 2 },
  hell: { attempts: 5, hints: 1 }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level) ⇒ Game

Returns a new instance of Game.



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

def initialize(level)
  @secret = ''
  @level_name = level.to_s
  @attempts_total = DIFFICULTY[level][:attempts]
  @attempts = DIFFICULTY[level][:attempts]
  @hints_available = DIFFICULTY[level][:hints]
  @hints_used = 0
  @hints_given = []
  @result = nil

  generate_number
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#attempts_totalObject (readonly)

Returns the value of attribute attempts_total.



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

def attempts_total
  @attempts_total
end

#hints_availableObject (readonly)

Returns the value of attribute hints_available.



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

def hints_available
  @hints_available
end

#hints_givenObject (readonly)

Returns the value of attribute hints_given.



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

def hints_given
  @hints_given
end

#hints_usedObject (readonly)

Returns the value of attribute hints_used.



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

def hints_used
  @hints_used
end

#level_nameObject (readonly)

Returns the value of attribute level_name.



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

def level_name
  @level_name
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

Class Method Details

.validate_answer(guess) ⇒ Object

rubocop:enable Naming/AccessorMethodName



96
97
98
# File 'lib/entities/game.rb', line 96

def self.validate_answer(guess)
  guess.instance_of?(String) && guess.length == 4
end

Instance Method Details

#compare_with(guess) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/entities/game.rb', line 68

def compare_with(guess)
  secret_copy = @secret.slice(0..-1)

  full_matches, secret_copy = check_full_matches(guess, secret_copy)
  partial_matches, _secret_copy = check_partial_matches(guess, secret_copy)
  result = full_matches + partial_matches
  @attempts -= 1

  @result = result.chars.sort.join
  @result
end

#get_hintObject

rubocop:disable Naming/AccessorMethodName



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/entities/game.rb', line 81

def get_hint
  return unless @hints_available.positive?

  number = @secret[Random.rand(0..3)]

  number = @secret[Random.rand(0..3)] until @hints_given.index(number).nil?

  @hints_available -= 1
  @hints_used += 1
  @hints_given.push(number)

  number
end