Class: IcodebreakerGem::GameCore

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/game_core.rb

Constant Summary

Constants included from Validation

Validation::CODE, Validation::USERNAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#validate_code, #validate_difficulty, #validate_name

Constructor Details

#initialize(secret) ⇒ GameCore

Returns a new instance of GameCore.



8
9
10
11
12
13
# File 'lib/game_core.rb', line 8

def initialize(secret)
  validate_code secret

  @secret = secret.to_s
  @shuffle = @secret.chars.shuffle
end

Instance Attribute Details

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/game_core.rb', line 6

def secret
  @secret
end

Class Method Details

.randomObject



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

def self.random
  (0..3).map { rand(1..6) }.join
end

Instance Method Details

#compare(guess) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/game_core.rb', line 15

def compare(guess)
  return '++++' if @secret == guess
  return '' if (@secret.chars & guess.chars).empty?

  pluses1, minuses1 = pluses_and_minuses(@secret, guess)

  pluses2, minuses2 = pluses_and_minuses(guess, @secret)

  '+' * [pluses1, pluses2].min + '-' * [minuses1, minuses2].min
end

#hintObject



26
27
28
# File 'lib/game_core.rb', line 26

def hint
  @shuffle.pop
end