Class: Codebreaker::Entities::Game

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

Constant Summary collapse

DIGITS_COUNT =
4
DIFFICULTIES =
{
  easy: {
    attempts: 15,
    hints: 2
  },
  medium: {
    attempts: 10,
    hints: 1
  },
  hell: {
    attempts: 5,
    hints: 1
  }
}.freeze
RANGE =
(1..6).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @process = Processor.new
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



23
24
25
# File 'lib/codebreaker_marian/entities/game.rb', line 23

def attempts
  @attempts
end

#codeObject (readonly)

Returns the value of attribute code.



23
24
25
# File 'lib/codebreaker_marian/entities/game.rb', line 23

def code
  @code
end

#hintsObject (readonly)

Returns the value of attribute hints.



23
24
25
# File 'lib/codebreaker_marian/entities/game.rb', line 23

def hints
  @hints
end

Instance Method Details

#decrease_attempts!Object



45
46
47
# File 'lib/codebreaker_marian/entities/game.rb', line 45

def decrease_attempts!
  @attempts -= 1 if @attempts != 0
end

#generate(difficulty) ⇒ Object



29
30
31
32
33
34
# File 'lib/codebreaker_marian/entities/game.rb', line 29

def generate(difficulty)
  @difficulty = difficulty
  @code = generate_secret_code
  @hints = @code.sample(difficulty[:hints])
  @attempts = difficulty[:attempts]
end

#hints_spent?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/codebreaker_marian/entities/game.rb', line 61

def hints_spent?
  hints.empty?
end

#start_process(command) ⇒ Object



36
37
38
39
# File 'lib/codebreaker_marian/entities/game.rb', line 36

def start_process(command)
  decrease_attempts!
  @process.secret_code_proc(code.join, command)
end

#take_a_hint!Object



65
66
67
# File 'lib/codebreaker_marian/entities/game.rb', line 65

def take_a_hint!
  hints.pop
end

#to_h(name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/codebreaker_marian/entities/game.rb', line 49

def to_h(name)
  {
    name: name,
    difficulty: DIFFICULTIES.key(@difficulty),
    all_attempts: @difficulty[:attempts],
    all_hints: @difficulty[:hints],
    attempts_used: @difficulty[:attempts] - @attempts,
    hints_used: @difficulty[:hints] - @hints.length,
    date: Time.now
  }
end

#win?(guess) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/codebreaker_marian/entities/game.rb', line 41

def win?(guess)
  code.join == guess
end