Class: CodebrackerSimb::Game

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

Constant Summary collapse

HARD =
'hard'
MEDIUM =
'medium'
EASY =
'easy'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player, complexity) ⇒ Game

Returns a new instance of Game.



14
15
16
17
18
19
20
21
# File 'lib/codebracker_simb/game.rb', line 14

def initialize(player, complexity)
  @attempts = game_options(complexity)[:attempts]
  @hints = game_options(complexity)[:hints]
  @player = player
  @hint_positions = []
  @complexity = complexity
  define_code
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



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

def answer
  @answer
end

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#complexityObject (readonly)

Returns the value of attribute complexity.



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

def complexity
  @complexity
end

#hint_positionsObject

Returns the value of attribute hint_positions.



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

def hint_positions
  @hint_positions
end

#hintsObject

Returns the value of attribute hints.



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

def hints
  @hints
end

#playerObject (readonly)

Returns the value of attribute player.



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

def player
  @player
end

Instance Method Details

#check_answerObject



27
28
29
30
# File 'lib/codebracker_simb/game.rb', line 27

def check_answer
  refresh_attempts_quantity
  Checker.new(code, answer).compare
end

#define_codeObject



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

def define_code
  @code = Array.new(4) { rand(1..6) }
end

#end_with_win?Boolean

Returns:

  • (Boolean)


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

def end_with_win?
  code == answer
end

#hint!Object

Raises:



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

def hint!
  position = nil
  loop do
    position = rand(0..3)
    break unless hint_positions.include? position
  end
  raise NoHintsError unless hints.positive?

  hint_positions << position
  @hints -= 1
  code[position]
end

#input_answer(input) ⇒ Object



23
24
25
# File 'lib/codebracker_simb/game.rb', line 23

def input_answer(input)
  @answer = input.chars.map(&:to_i)
end

#paramsObject



57
58
59
60
61
62
63
64
# File 'lib/codebracker_simb/game.rb', line 57

def params
  total_attempts = game_options(complexity)[:attempts]
  total_hints = game_options(complexity)[:hints]
  used_hints = total_hints - hints
  used_attempts = total_attempts - attempts
  { player: player, complexity: complexity, total_attempts: total_attempts,
    used_attempts: used_attempts, total_hints: total_hints, used_hints: used_hints }
end

#refresh_attempts_quantityObject



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

def refresh_attempts_quantity
  @attempts -= 1 if attempts.positive?
end