Class: Codebreaker::Game

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

Constant Summary collapse

GAME_DIFFICULTIES =
{ simple: { attempts: 15, hints: 2 }, medium: { attempts: 10, hints: 1 },
hell: { attempts: 5, hints: 1 } }.freeze
WIN =
:win
LOSS =
:loss
PLUS =
'+'
MINUS =
'-'
NUMBER_OF_ENCRYPTED_DIGITS =
4
VALUES_FOR_ENCRYPTED_DIGITS =
(1..6).to_a

Constants included from Storage

Storage::STORAGE_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Storage

#show_statistics, #statistics_with_raiting, #store_game

Constructor Details

#initialize(user_name) ⇒ Game

Returns a new instance of Game.



25
26
27
28
29
# File 'lib/game.rb', line 25

def initialize(user_name)
  @user = User.new(user_name)
  @secret_code = SecretCode.new(generate_secret_code).value
  @status = nil
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



13
14
15
# File 'lib/game.rb', line 13

def attempts
  @attempts
end

#hintsObject (readonly)

Returns the value of attribute hints.



13
14
15
# File 'lib/game.rb', line 13

def hints
  @hints
end

#levelObject (readonly)

Returns the value of attribute level.



13
14
15
# File 'lib/game.rb', line 13

def level
  @level
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'lib/game.rb', line 14

def status
  @status
end

#total_attemptsObject (readonly)

Returns the value of attribute total_attempts.



13
14
15
# File 'lib/game.rb', line 13

def total_attempts
  @total_attempts
end

#total_hintsObject (readonly)

Returns the value of attribute total_hints.



13
14
15
# File 'lib/game.rb', line 13

def total_hints
  @total_hints
end

#userObject (readonly)

Returns the value of attribute user.



13
14
15
# File 'lib/game.rb', line 13

def user
  @user
end

Instance Method Details

#add_hints_attempts(level) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/game.rb', line 31

def add_hints_attempts(level)
  @level = level.to_sym
  @total_attempts = Game::GAME_DIFFICULTIES[@level][:attempts]
  @total_hints = Game::GAME_DIFFICULTIES[@level][:hints]
  @attempts = @total_attempts
  @hints = @total_hints
end

#guess(user_variant) ⇒ Object



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

def guess(user_variant)
  return @status = Game::LOSS if @attempts.zero?

  answer = SecretCode.new(user_variant.to_i)
  @attempts -= 1 if answer.valid?

  if answer.value == @secret_code
    store_game
    return @status = Game::WIN
  end

  check_user_answer(answer.value)
end

#take_hintObject



39
40
41
42
43
44
45
# File 'lib/game.rb', line 39

def take_hint
  raise I18n.t(:out_of_hints) if @hints.zero?

  @hints -= 1

  @secret_code.digits.shuffle.pop
end