Class: Codebreaker::Entities::Game
- Inherits:
-
Object
- Object
- Codebreaker::Entities::Game
- Defined in:
- lib/codebreaker/entities/game.rb
Constant Summary collapse
- COUNT_NUMBERS =
4
- CODE_RANGE =
(1..6).freeze
- DIFFICULTIES =
{ easy: { attempts: 15, hints: 2 }, medium: { attempts: 10, hints: 1 }, hard: { attempts: 5, hints: 1 } }.freeze
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
Instance Method Summary collapse
- #attempt_wasted ⇒ Object
- #check_win?(answer) ⇒ Boolean
- #hint_take ⇒ Object
- #hints_not_remain? ⇒ Boolean
- #init_game(difficulty) ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #start(guess) ⇒ Object
- #to_h(name) ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
27 28 29 |
# File 'lib/codebreaker/entities/game.rb', line 27 def initialize @controller = Controller.new end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
25 26 27 |
# File 'lib/codebreaker/entities/game.rb', line 25 def attempts @attempts end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
25 26 27 |
# File 'lib/codebreaker/entities/game.rb', line 25 def code @code end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
25 26 27 |
# File 'lib/codebreaker/entities/game.rb', line 25 def hints @hints end |
Instance Method Details
#attempt_wasted ⇒ Object
50 51 52 |
# File 'lib/codebreaker/entities/game.rb', line 50 def attempt_wasted @attempts -= 1 end |
#check_win?(answer) ⇒ Boolean
42 43 44 |
# File 'lib/codebreaker/entities/game.rb', line 42 def check_win?(answer) code.join == answer end |
#hint_take ⇒ Object
54 55 56 |
# File 'lib/codebreaker/entities/game.rb', line 54 def hint_take hints.pop end |
#hints_not_remain? ⇒ Boolean
46 47 48 |
# File 'lib/codebreaker/entities/game.rb', line 46 def hints_not_remain? hints.empty? end |
#init_game(difficulty) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/codebreaker/entities/game.rb', line 31 def init_game(difficulty) @difficulty = difficulty @code = generate_code @hints = @code.sample(difficulty[:hints]) @attempts = difficulty[:attempts] end |
#start(guess) ⇒ Object
38 39 40 |
# File 'lib/codebreaker/entities/game.rb', line 38 def start(guess) @controller.secret_code_handler(code.join, guess) end |
#to_h(name) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/codebreaker/entities/game.rb', line 58 def to_h(name) { name: name, difficulty: DIFFICULTIES.key(@difficulty), all_attempts: @difficulty[:attempts], all_hints: @difficulty[:hints], attempts_wasted: @difficulty[:attempts] - @attempts, hints_wasted: @difficulty[:hints] - @hints.length, date: Time.now } end |