Class: Codebreaker::Game

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

Constant Summary collapse

WIN =
:win
LOSE =
:lose
LEVELS =
{ easy: 'easy', medium: 'medium', hard: 'hard' }.freeze
COMPLEXITIES =
{ easy: { hints: 2, attempts: 15, level: LEVELS[:easy] },
medium: { hints: 1, attempts: 10, level: LEVELS[:medium] },
hard: { hints: 1, attempts: 5, level: LEVELS[:hard] } }.freeze

Constants included from Storage

Storage::FILE_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Storage

#restore_storage, #save

Constructor Details

#initialize(name, complexity) ⇒ Game

Returns a new instance of Game.



14
15
16
17
18
19
20
# File 'lib/entities/game.rb', line 14

def initialize(name, complexity)
  @name = name
  @level = complexity.to_sym
  generate_hints_attempts(@level)
  @cb_numbers = Core.new.generate_secret_code
  @cb_numbers_copy = @cb_numbers.clone
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



4
5
6
# File 'lib/entities/game.rb', line 4

def attempts
  @attempts
end

#cb_numbersObject (readonly)

Returns the value of attribute cb_numbers.



4
5
6
# File 'lib/entities/game.rb', line 4

def cb_numbers
  @cb_numbers
end

#cb_numbers_copyObject (readonly)

Returns the value of attribute cb_numbers_copy.



4
5
6
# File 'lib/entities/game.rb', line 4

def cb_numbers_copy
  @cb_numbers_copy
end

#hintsObject (readonly)

Returns the value of attribute hints.



4
5
6
# File 'lib/entities/game.rb', line 4

def hints
  @hints
end

#levelObject (readonly)

Returns the value of attribute level.



4
5
6
# File 'lib/entities/game.rb', line 4

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/entities/game.rb', line 4

def name
  @name
end

Instance Method Details

#calculate_attemptsObject



47
48
49
# File 'lib/entities/game.rb', line 47

def calculate_attempts
  take_level_hash[:attempts] - @attempts
end

#calculate_hintsObject



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

def calculate_hints
  take_level_hash[:hints] - @hints
end

#generate_hints_attempts(level) ⇒ Object



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

def generate_hints_attempts(level)
  level_hash = take_level_hash
  @hints = level_hash[:hints]
  @attempts = level_hash[:attempts]
end

#guess(user_guess) ⇒ Object



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

def guess(user_guess)
  @attempts -= 1
  user_guess_array = user_guess.split('').map(&:to_i)
  return WIN if win? user_guess_array
  return LOSE if lose?
  Core.new.check(@cb_numbers, user_guess_array)
end

#hintObject



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

def hint
  return nil if @hints.zero?

  @hints -= 1
  @cb_numbers_copy.shuffle.pop
end

#take_level_hashObject



52
53
54
# File 'lib/entities/game.rb', line 52

def take_level_hash
  COMPLEXITIES[@level]
end