Class: Codebreaker::Game

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

Constant Summary collapse

RANGE =
(1..6).freeze
GAME_LEVELS =
{
  easy: { attempts: 30, hints: 3, level_num: 0 },
  medium: { attempts: 15, hints: 2, level_num: 1 },
  hard: { attempts: 10, hints: 1, level_num: 2 }
}.freeze
DEFAULT_PATH_TO_FILE =
"./lib/codebreaker/data/stat.yml".freeze
COUNTS_OF_HINTS =
(0..3).freeze
DIGITS_COUNT =
4
USER_ANSWER_REX =
/^[1-6]{4}$/.freeze
NAME_SIZE_RANGE =
(3..20).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#validate_code, #validate_length

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def attempts
  @attempts
end

#hintsObject (readonly)

Returns the value of attribute hints.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def hints
  @hints
end

#hints_indexObject (readonly)

Returns the value of attribute hints_index.



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

def hints_index
  @hints_index
end

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def level
  @level
end

#level_numObject (readonly)

Returns the value of attribute level_num.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def level_num
  @level_num
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def name
  @name
end

#phrasesObject (readonly)

Returns the value of attribute phrases.



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

def phrases
  @phrases
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



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

def secret_code
  @secret_code
end

#user_codeObject (readonly)

Returns the value of attribute user_code.



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

def user_code
  @user_code
end

Class Method Details

.message(msg, params = {}) ⇒ Object



95
96
97
# File 'lib/codebreaker/game.rb', line 95

def self.message(msg, params = {})
  I18n.t(msg, params)
end

Instance Method Details

#enter_level(level) ⇒ Object



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

def enter_level(level)
  return unless GAME_LEVELS.key?(level.to_sym)

  @level = level

  @attempts = GAME_LEVELS.dig(level.to_sym, :attempts)
  @hints = GAME_LEVELS.dig(level.to_sym, :hints)
  @level_num = GAME_LEVELS.dig(level.to_sym, :level_num)
  @hints_index = COUNTS_OF_HINTS.to_a.sample @hints
end

#enter_name(name) ⇒ Object



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

def enter_name(name)
  return unless validate_length(name, NAME_SIZE_RANGE)

  @name = name
end

#equal_codes?(user_answer) ⇒ Boolean

Returns:

  • (Boolean)


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

def equal_codes?(user_answer)
  secret_code.join == user_answer
end

#game_resultObject



69
70
71
72
# File 'lib/codebreaker/game.rb', line 69

def game_result
  secret_code_clone = @secret_code.clone
  count_plus(secret_code_clone) + count_minus(secret_code_clone)
end

#handle_guess(user_answer) ⇒ Object



64
65
66
67
# File 'lib/codebreaker/game.rb', line 64

def handle_guess(user_answer)
  take_attempts
  set_user_code(user_answer)
end

#new_gameObject



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

def new_game
  @secret_code = Array.new(DIGITS_COUNT) { rand(RANGE) }
end

#save(path_to_file = DEFAULT_PATH_TO_FILE) ⇒ Object



89
90
91
92
93
# File 'lib/codebreaker/game.rb', line 89

def save(path_to_file = DEFAULT_PATH_TO_FILE)
  storage = StorageInterceptor.new(path_to_file)
  codebreaker_data = storage.read_database || []
  storage.write_database(storage_data(codebreaker_data))
end

#set_user_code(enter_code) ⇒ Object



60
61
62
# File 'lib/codebreaker/game.rb', line 60

def set_user_code(enter_code)
  @user_code = enter_code.each_char.map(&:to_i)
end

#show_hintsObject



56
57
58
# File 'lib/codebreaker/game.rb', line 56

def show_hints
  secret_code[hints_index.shift]
end

#storage_data(codebreaker_data) ⇒ Object



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

def storage_data(codebreaker_data)
  attempts_used = GAME_LEVELS.dig(level.to_sym, :attempts) - attempts
  hints_used = GAME_LEVELS.dig(level.to_sym, :hints) - hints
  game_date = Time.new.strftime '%Y/%m/%d %H:%M:%S'
  hash_stat = { name: @name,
                level: @level,
                level_num: @level_num,
                attempts: @attempts,
                attempts_used: attempts_used,
                hints: @hints,
                hints_used: hints_used,
                game_date: game_date }
  codebreaker_data << hash_stat
end

#take_attemptsObject



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

def take_attempts
  @attempts -= 1
end

#take_hintsObject



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

def take_hints
  @hints -= 1
end