Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/app/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.



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

def initialize
  @process = Processor.new
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



21
22
23
# File 'lib/app/entities/game.rb', line 21

def attempts
  @attempts
end

#codeObject (readonly)

Returns the value of attribute code.



21
22
23
# File 'lib/app/entities/game.rb', line 21

def code
  @code
end

#hintsObject (readonly)

Returns the value of attribute hints.



21
22
23
# File 'lib/app/entities/game.rb', line 21

def hints
  @hints
end

Instance Method Details

#decrease_attempts!Object



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

def decrease_attempts!
  @attempts -= 1
end

#generate(difficulty) ⇒ Object



27
28
29
30
31
32
# File 'lib/app/entities/game.rb', line 27

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

#hints_spent?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/app/entities/game.rb', line 57

def hints_spent?
  hints.empty?
end

#start_process(command) ⇒ Object



34
35
36
# File 'lib/app/entities/game.rb', line 34

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

#take_a_hint!Object



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

def take_a_hint!
  hints.pop
end

#to_h(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/app/entities/game.rb', line 46

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)


38
39
40
# File 'lib/app/entities/game.rb', line 38

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