Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/codebreaker/game.rb

Constant Summary

Constants included from Constants

Constants::DIFFICULTS, Constants::MINUS, Constants::PLUS, Constants::WIN_RESULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, difficult) ⇒ Game

Returns a new instance of Game.



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

def initialize(name, difficult)
  @name = name
  @difficult = difficult
  @attempts = get_attempts(difficult)
  @hints = get_hints(difficult)

  generate_secret
  @secret_chars = @secret.chars.shuffle
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

#difficultObject (readonly)

Returns the value of attribute difficult.



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

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

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

Instance Method Details

#check_number(guess, secret_code = @secret) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/codebreaker/game.rb', line 44

def check_number(guess, secret_code = @secret)
  secret = secret_code.chars
  input = guess.chars

  grouped = group_arrays(secret, input)

  plus = grouped[true].size
  return result(plus) if plus == 4

  minus = get_minuses_count(grouped)
  result(plus, minus)
end

#decrement_attemptsObject



19
20
21
22
23
# File 'lib/codebreaker/game.rb', line 19

def decrement_attempts
  return false if @attempts.zero?

  @attempts -= 1
end

#use_hintObject



25
26
27
28
29
30
# File 'lib/codebreaker/game.rb', line 25

def use_hint
  return false if @hints.zero?

  @hints -= 1
  @secret_chars.pop
end

#used_attemptsObject



36
37
38
# File 'lib/codebreaker/game.rb', line 36

def used_attempts
  get_attempts(@difficult) - @attempts
end

#used_hintsObject



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

def used_hints
  get_hints(@difficult) - @hints
end

#win?(matches) ⇒ Boolean

Returns:

  • (Boolean)


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

def win?(matches)
  matches == WIN_RESULT
end