Class: CodebrackerSimb::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(complexity, player) ⇒ Game

Returns a new instance of Game.



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

def initialize(complexity, player)
  @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



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

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

#define_codeObject



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

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

#end_with_win?Boolean

Returns:

  • (Boolean)


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

def end_with_win?
  code == answer
end

#hintObject



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

def hint
  position = nil
  loop do
    position = rand(0..3)
    break unless hint_positions.include? position
  end
  if @hints.positive?
    hint_positions << position
    @hints -= 1
    code[position]
  else
    raise NoHintsError
  end
end

#input_answer(input) ⇒ Object



19
20
21
# File 'lib/codebracker_simb/game.rb', line 19

def input_answer(input)
  @answer = input.split('').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



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

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