Class: Codebreaker::Entities::Game

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeGame

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

#attemptsObject (readonly)

Returns the value of attribute attempts.



25
26
27
# File 'lib/codebreaker/entities/game.rb', line 25

def attempts
  @attempts
end

#codeObject (readonly)

Returns the value of attribute code.



25
26
27
# File 'lib/codebreaker/entities/game.rb', line 25

def code
  @code
end

#hintsObject (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_wastedObject



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

def attempt_wasted
  @attempts -= 1
end

#check_win?(answer) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/codebreaker/entities/game.rb', line 42

def check_win?(answer)
  code.join == answer
end

#hint_takeObject



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

def hint_take
  hints.pop
end

#hints_not_remain?Boolean

Returns:

  • (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