Class: IcodebreakerGem::Game

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

Constant Summary collapse

STATUS =
%i[go_on over].freeze
RESULT =
%i[win lose].freeze
DIFFICULTIES =
{
  easy: {
    attempts_total: 15,
    hints_total: 2
  },
  medium: {
    attempts_total: 10,
    hints_total: 1
  },
  hell: {
    attempts_total: 5,
    hints_total: 1
  }
}.freeze

Constants included from Storage

Storage::DATA_FILE, Storage::STORAGE_PATH

Constants included from Validation

Validation::CODE, Validation::USERNAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Storage

load_storage, save_storage, sort_codebreakers

Methods included from Validation

#validate_code, #validate_difficulty, #validate_name

Constructor Details

#initialize(name = 'User1', difficulty = :easy, secret = GameCore.random) ⇒ Game

Returns a new instance of Game.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/game.rb', line 27

def initialize(name = 'User1', difficulty = :easy, secret = GameCore.random)
  validate_name name
  validate_difficulty difficulty

  @name = name.to_s
  @difficulty = difficulty.to_sym

  @attempts_used = 0
  @hints_used = 0

  @status = :go_on
  @result = nil

  @code = GameCore.new(secret)
end

Instance Attribute Details

#attempts_usedObject

Returns the value of attribute attempts_used.



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

def attempts_used
  @attempts_used
end

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#difficultyObject

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#hints_usedObject

Returns the value of attribute hints_used.



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

def hints_used
  @hints_used
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#attempt(attempt_code) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/game.rb', line 51

def attempt(attempt_code)
  case @status
  when :go_on
    @attempts_used += 1
    answer = @code.compare(attempt_code)
    check_win(answer)
    check_lose
    answer
  when :over
    I18n.t('message.lose')
  end
end

#attempts_totalObject



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

def attempts_total
  DIFFICULTIES[difficulty][:attempts_total]
end

#game_dataObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/game.rb', line 76

def game_data
  {

    name: name,
    difficulty: difficulty.to_s,
    attempts_total: attempts_total,
    attempts_used: attempts_used,
    hints_total: hints_total,
    hints_used: hints_used
  }
end

#hintObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/game.rb', line 64

def hint
  case @status
  when :go_on
    return I18n.t('message.no_hints') if @hints_used >= hints_total

    @hints_used += 1
    @code.hint
  when :over
    I18n.t('message.lose')
  end
end

#hints_totalObject



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

def hints_total
  DIFFICULTIES[difficulty][:hints_total]
end