Class: 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.



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

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)

private



8
9
10
# File 'lib/game.rb', line 8

def attempts
  @attempts
end

#hintsObject (readonly)

private



8
9
10
# File 'lib/game.rb', line 8

def hints
  @hints
end

#levelObject (readonly)

private



8
9
10
# File 'lib/game.rb', line 8

def level
  @level
end

#secret_codeObject (readonly)

private



8
9
10
# File 'lib/game.rb', line 8

def secret_code
  @secret_code
end

#statusObject

public



11
12
13
# File 'lib/game.rb', line 11

def status
  @status
end

#total_attemptsObject (readonly)

private



8
9
10
# File 'lib/game.rb', line 8

def total_attempts
  @total_attempts
end

#total_hintsObject (readonly)

private



8
9
10
# File 'lib/game.rb', line 8

def total_hints
  @total_hints
end

#userObject (readonly)

private



8
9
10
# File 'lib/game.rb', line 8

def user
  @user
end

Instance Method Details

#add_hints_attempts(level) ⇒ Object



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

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



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

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



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

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

  @hints -= 1

  @secret_code.digits.shuffle.pop
end