Class: Codebreaker::Game

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

Constant Summary collapse

CODE_LENGTH =
4
CODE_RANGE =
(1..6).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, difficulty) ⇒ Game

Returns a new instance of Game.



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

def initialize(name, difficulty)
  @difficulties = Loader.load('difficulties')
  @code = generate_code
  @hints_code = @code.chars.shuffle
  game_option(name, difficulty)
end

Instance Attribute Details

#all_attemptsObject

Returns the value of attribute all_attempts.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def all_attempts
  @all_attempts
end

#all_hintsObject

Returns the value of attribute all_hints.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def all_hints
  @all_hints
end

#attempts_leftObject

Returns the value of attribute attempts_left.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def attempts_left
  @attempts_left
end

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def code
  @code
end

#difficultiesObject

Returns the value of attribute difficulties.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def difficulties
  @difficulties
end

#difficultyObject

Returns the value of attribute difficulty.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def difficulty
  @difficulty
end

#hints_codeObject (readonly)

Returns the value of attribute hints_code.



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

def hints_code
  @hints_code
end

#hints_leftObject

Returns the value of attribute hints_left.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def hints_left
  @hints_left
end

#input_codeObject

Returns the value of attribute input_code.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def input_code
  @input_code
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/codebreaker/game.rb', line 5

def name
  @name
end

Instance Method Details

#attempts_left?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/codebreaker/game.rb', line 47

def attempts_left?
  attempts_left.positive?
end

#game_option(name, difficulty) ⇒ Object



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

def game_option(name, difficulty)
  @name = name
  @difficulty = difficulty.to_sym
  @all_attempts = difficulty_option[:attempts]
  @all_hints = difficulty_option[:hints]
  @attempts_left = difficulty_option[:attempts]
  @hints_left = difficulty_option[:hints]
end

#hintObject



28
29
30
31
32
33
# File 'lib/codebreaker/game.rb', line 28

def hint
  return unless hints_left?

  @hints_left -= 1
  generate_hint
end

#hints_left?Boolean

Returns:

  • (Boolean)


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

def hints_left?
  hints_left.positive?
end

#input_operation(input_code) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/codebreaker/game.rb', line 39

def input_operation(input_code)
  @input_code = input_code
  return unless attempts_left?

  @attempts_left -= 1
  check_input(@code, @input_code)
end

#save(filename = 'stats') ⇒ Object



55
56
57
# File 'lib/codebreaker/game.rb', line 55

def save(filename = 'stats')
  Codebreaker::Loader.save(to_h, filename)
end

#win?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/codebreaker/game.rb', line 51

def win?
  input_code == code
end