Class: Codebreaker::Entities::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker/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/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/entities/game.rb', line 23

def attempts
  @attempts
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

Instance Method Details

#decrease_attempts!Object



44
45
46
# File 'lib/codebreaker/entities/game.rb', line 44

def decrease_attempts!
  @attempts -= 1
end

#generate(difficulty) ⇒ Object



29
30
31
32
33
34
# File 'lib/codebreaker/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)


59
60
61
# File 'lib/codebreaker/entities/game.rb', line 59

def hints_spent?
  hints.empty?
end

#start_process(command) ⇒ Object



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

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

#take_a_hint!Object



63
64
65
# File 'lib/codebreaker/entities/game.rb', line 63

def take_a_hint!
  hints.pop
end

#to_h(name) ⇒ Object



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

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
  }
end

#win?(guess) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/codebreaker/entities/game.rb', line 40

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