Class: Codebreaker::Entities::Menu

Inherits:
Object
  • Object
show all
Includes:
Helpers::Validator
Defined in:
lib/codebreaker/entities/menu.rb

Constant Summary collapse

COMMANDS_LIST =
{
  start: 'start',
  exit: 'exit',
  rules: 'rules',
  stats: 'stats'
}.freeze
CONFIRM_COMMAND =
{
  yes: 'yes'
}.freeze
HINT_COMMAND =
'hint'.freeze
MIN_SIZE =
3
MAX_SIZE =
25

Constants included from Helpers::Validator

Helpers::Validator::FORMAT_VALUES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Validator

#empty_check?, #format_check?, #size_check

Constructor Details

#initializeMenu

Returns a new instance of Menu.



23
24
25
26
27
28
# File 'lib/codebreaker/entities/menu.rb', line 23

def initialize
  @storage = Storage.new
  @viewer = Viewer.new
  @game = Game.new
  @statistics = Statistics.new
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



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

def game
  @game
end

#guessObject (readonly)

Returns the value of attribute guess.



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

def guess
  @guess
end

#storageObject (readonly)

Returns the value of attribute storage.



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

def storage
  @storage
end

#viewerObject (readonly)

Returns the value of attribute viewer.



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

def viewer
  @viewer
end

Instance Method Details

#ask(phrase_key = nil, options = {}) ⇒ Object



53
54
55
56
# File 'lib/codebreaker/entities/menu.rb', line 53

def ask(phrase_key = nil, options = {})
  viewer.message(phrase_key, options) if phrase_key
  gets.chomp
end

#choice_code_processObject



94
95
96
97
98
99
100
# File 'lib/codebreaker/entities/menu.rb', line 94

def choice_code_process
  case guess
  when HINT_COMMAND then hint_process
  when COMMANDS_LIST[:exit] then game_menu
  else handle_command
  end
end

#choice_menu_process(command) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/codebreaker/entities/menu.rb', line 132

def choice_menu_process(command)
  case command
  when COMMANDS_LIST[:start] then start
  when COMMANDS_LIST[:exit] then exit_from_game
  when COMMANDS_LIST[:rules] then rules
  when COMMANDS_LIST[:stats] then stats
  else
    viewer.command_error
    game_menu
  end
end

#exit_from_gameObject



127
128
129
130
# File 'lib/codebreaker/entities/menu.rb', line 127

def exit_from_game
  viewer.goodbye_message
  exit
end

#game_menuObject



30
31
32
33
34
# File 'lib/codebreaker/entities/menu.rb', line 30

def game_menu
  viewer.start_message

  choice_menu_process(ask(:choice_options, commands: COMMANDS_LIST.keys.join(' | ')))
end

#game_processObject



84
85
86
87
88
89
90
91
92
# File 'lib/codebreaker/entities/menu.rb', line 84

def game_process
  while game.attempts.positive?
    @guess = ask
    return handle_win if game.check_win?(guess)

    choice_code_process
  end
  handle_lose
end

#generate_game(difficulty) ⇒ Object



77
78
79
80
81
82
# File 'lib/codebreaker/entities/menu.rb', line 77

def generate_game(difficulty)
  game.init_game(difficulty)
  viewer.message(:difficulty,
                 hints: difficulty[:hints],
                 attempts: difficulty[:attempts])
end

#handle_commandObject



102
103
104
105
106
107
108
# File 'lib/codebreaker/entities/menu.rb', line 102

def handle_command
  return viewer.command_error unless format_check?(guess)

  p game.start(guess)
  viewer.round_message
  game.attempt_wasted
end

#handle_loseObject



116
117
118
119
# File 'lib/codebreaker/entities/menu.rb', line 116

def handle_lose
  viewer.lost_game_message(game.code)
  game_menu
end

#handle_winObject



110
111
112
113
114
# File 'lib/codebreaker/entities/menu.rb', line 110

def handle_win
  viewer.win_game_message
  save_result
  game_menu
end

#hint_processObject



121
122
123
124
125
# File 'lib/codebreaker/entities/menu.rb', line 121

def hint_process
  return viewer.no_hints_message if game.hints_not_remain?

  viewer.print_hint(game.hint_take)
end

#level_choiceObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/codebreaker/entities/menu.rb', line 66

def level_choice
  loop do
    level = ask(:difficulty_level, levels: Game::DIFFICULTIES.keys.join(' | '))

    return generate_game(Game::DIFFICULTIES[level.to_sym]) if Game::DIFFICULTIES[level.to_sym]
    return game_menu if level == COMMANDS_LIST[:exit]

    viewer.command_error
  end
end

#name_valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/codebreaker/entities/menu.rb', line 62

def name_valid?(name)
  !empty_check?(name) && size_check(name, MIN_SIZE, MAX_SIZE)
end

#render_stats(list) ⇒ Object



154
155
156
157
158
159
# File 'lib/codebreaker/entities/menu.rb', line 154

def render_stats(list)
  list.each_with_index do |key, index|
    puts "#{index + 1}: "
    key.each { |param, value| puts "#{param}:#{value}" }
  end
end

#rulesObject



36
37
38
39
# File 'lib/codebreaker/entities/menu.rb', line 36

def rules
  viewer.rules
  game_menu
end

#save_resultObject



58
59
60
# File 'lib/codebreaker/entities/menu.rb', line 58

def save_result
  storage.save_game_results(game.to_h(@name)) if ask(:save_results_message) == CONFIRM_COMMAND[:yes]
end

#startObject



41
42
43
44
45
# File 'lib/codebreaker/entities/menu.rb', line 41

def start
  @name = user_registration
  level_choice
  game_process
end

#statsObject



47
48
49
50
51
# File 'lib/codebreaker/entities/menu.rb', line 47

def stats
  scores = storage.load
  render_stats(@statistics.stats(scores)) if scores
  game_menu
end

#user_registrationObject



144
145
146
147
148
149
150
151
152
# File 'lib/codebreaker/entities/menu.rb', line 144

def user_registration
  loop do
    name = ask(:registration)

    return name if name_valid?(name)

    viewer.registration_name_length_error
  end
end