Class: Codebreaker::Game

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

Constant Summary collapse

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

Instance Attribute 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
# File 'lib/codebraker_ov/entities/game.rb', line 20

def initialize(level)
  @secret = Array.new(4) { rand(1..6).to_s }
  @level_name = level.to_s
  @attempts_total = DIFFICULTIES[level][:attempts]
  @attempts = DIFFICULTIES[level][:attempts]
  @hints_available = @secret.sample(DIFFICULTIES[level][:hints])
  @hints_given = []
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#attempts_totalObject (readonly)

Returns the value of attribute attempts_total.



3
4
5
# File 'lib/codebraker_ov/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/codebraker_ov/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/codebraker_ov/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/codebraker_ov/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/codebraker_ov/entities/game.rb', line 3

def level_name
  @level_name
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

Instance Method Details

#compare_with(guess) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/codebraker_ov/entities/game.rb', line 52

def compare_with(guess)
  @guess = guess.chars
  @result = ''

  rest = check_full_matches
  check_partial_matches(rest)
  @attempts -= 1
  @result
end

#lose?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/codebraker_ov/entities/game.rb', line 74

def lose?
  !@attempts.positive?
end

#receive_hintObject



62
63
64
65
66
67
68
# File 'lib/codebraker_ov/entities/game.rb', line 62

def receive_hint
  return unless @hints_available.length.positive?

  @hints_given << @hints_available.pop

  @hints_given.last
end

#win?(guess) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/codebraker_ov/entities/game.rb', line 70

def win?(guess)
  @secret == guess.chars
end