Class: CodebreakerAp::Game

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

Constant Summary collapse

LOCALES_FOLDER =
File.expand_path('storage_files', __dir__)
CODE_LENGTH =
4
CODE_LENGTH_RANGE =
(1..4).freeze
SECRET_CODE_NUMBERS_RANGE =
(1..6).freeze
IN_POSITION =
'+'.freeze
NOT_IN_POSITION =
'-'.freeze

Constants included from Statistic

Statistic::STATISTIC_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Statistic

#load_statistic, #save_stats, #show_stats

Methods included from Storage

#read_from_file, #save_to_file

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @difficulty = CodebreakerAp::Difficulty.new
  @secret_code = generate_secret_code
  @hints_code = secret_code.to_a.dup.shuffle
  @game_win = false
end

Instance Attribute Details

#difficultyObject (readonly)

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



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

def secret_code
  @secret_code
end

Instance Method Details

#check_answer(answer) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/codebreaker_ap/game.rb', line 22

def check_answer(answer)
  answer = answer.to_s

  @game_win = true if @secret_code.join == answer
  @difficulty.attempts -= 1

  compare_answer_with_secret(answer.split(''), secret_code.join.split(''))
end

#show_rulesObject



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

def show_rules
  rules = read_from_file("#{LOCALES_FOLDER}/#{Message.new.file_rules}")
  return unless rules

  rules
end

#take_hintObject



35
36
37
# File 'lib/codebreaker_ap/game.rb', line 35

def take_hint
  difficulty.hint(@hints_code)
end

#win?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/codebreaker_ap/game.rb', line 31

def win?
  @game_win
end