Class: CodebrackerSimb::Game
- Inherits:
-
Object
- Object
- CodebrackerSimb::Game
- Defined in:
- lib/codebracker_simb/game.rb
Constant Summary collapse
- HARD =
'hard'
- MEDIUM =
'medium'
- EASY =
'easy'
Instance Attribute Summary collapse
-
#answer ⇒ Object
Returns the value of attribute answer.
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#complexity ⇒ Object
readonly
Returns the value of attribute complexity.
-
#hint_positions ⇒ Object
Returns the value of attribute hint_positions.
-
#hints ⇒ Object
Returns the value of attribute hints.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
Instance Method Summary collapse
- #check_answer ⇒ Object
- #define_code ⇒ Object
- #end_with_win? ⇒ Boolean
- #hint! ⇒ Object
-
#initialize(player, complexity) ⇒ Game
constructor
A new instance of Game.
- #input_answer(input) ⇒ Object
- #params ⇒ Object
- #refresh_attempts_quantity ⇒ Object
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 = (complexity)[:attempts] @hints = (complexity)[:hints] @player = player @hint_positions = [] @complexity = complexity define_code end |
Instance Attribute Details
#answer ⇒ Object
Returns the value of attribute answer.
8 9 10 |
# File 'lib/codebracker_simb/game.rb', line 8 def answer @answer end |
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
7 8 9 |
# File 'lib/codebracker_simb/game.rb', line 7 def attempts @attempts end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/codebracker_simb/game.rb', line 7 def code @code end |
#complexity ⇒ Object (readonly)
Returns the value of attribute complexity.
7 8 9 |
# File 'lib/codebracker_simb/game.rb', line 7 def complexity @complexity end |
#hint_positions ⇒ Object
Returns the value of attribute hint_positions.
8 9 10 |
# File 'lib/codebracker_simb/game.rb', line 8 def hint_positions @hint_positions end |
#hints ⇒ Object
Returns the value of attribute hints.
8 9 10 |
# File 'lib/codebracker_simb/game.rb', line 8 def hints @hints end |
#player ⇒ Object (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_answer ⇒ Object
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_code ⇒ Object
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
36 37 38 |
# File 'lib/codebracker_simb/game.rb', line 36 def end_with_win? code == answer end |
#hint! ⇒ Object
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 |
#params ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/codebracker_simb/game.rb', line 57 def params total_attempts = (complexity)[:attempts] total_hints = (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_quantity ⇒ Object
40 41 42 |
# File 'lib/codebracker_simb/game.rb', line 40 def refresh_attempts_quantity @attempts -= 1 if attempts.positive? end |