class CodebreakerRostik::Console
Constants
- ANSWERS
- COUNT_ATTEMPTS
- COUNT_HINTS
- EXIT
- GAME_RESULTS
- HINT
- NAME_DIFFICULTIES
- NAME_LENGTH_RANGE
- OPTIONS
Attributes
info_difficult[R]
user[R]
user_name[R]
Public Instance Methods
check_option()
click to toggle source
# File lib/console.rb, line 28 def check_option show_info(:welcome) loop do case show_message_with_input(:choose_option) when OPTIONS[:start] then return registration when OPTIONS[:rules] then show_rules when OPTIONS[:stats] then show_stats else show_info(:wrong_input_option) end end end
show_message_with_input(message, key = nil)
click to toggle source
# File lib/console.rb, line 41 def show_message_with_input(message, key = nil) show_info(message, key) value = gets.chomp.capitalize value == OPTIONS[:exit] ? bye : value end
Private Instance Methods
ask_difficulty()
click to toggle source
# File lib/console.rb, line 69 def ask_difficulty loop do difficulty = show_message_with_input(:choose_difficulty, difficulties: Difficulty::DIFFICULTIES.keys.join(', ')) break Difficulty.new(difficulty).choose_difficulty if NAME_DIFFICULTIES.include?(difficulty) show_info(:unexpected_command) end end
ask_for_save()
click to toggle source
# File lib/console.rb, line 114 def ask_for_save Storage.new.add_data_to_db(user) if show_message_with_input(:ask_save_to_db) == ANSWERS[:yes] end
ask_name()
click to toggle source
# File lib/console.rb, line 56 def ask_name loop do name = show_message_with_input(:write_name) return name if valid_name?(name) show_info(:unexpected_command) end end
ask_restart()
click to toggle source
# File lib/console.rb, line 108 def ask_restart return check_option if show_message_with_input(:restart) == ANSWERS[:yes] bye end
attempts_left?()
click to toggle source
# File lib/console.rb, line 126 def attempts_left? user[:attempts_left] != user[:attempts_total] end
check_hint()
click to toggle source
# File lib/console.rb, line 130 def check_hint user[:hints_left] == user[:hints_total] ? show_info(:used_all_hints) : show_hint end
check_lose()
click to toggle source
# File lib/console.rb, line 118 def check_lose @game.attempts_left_increase(user) return if attempts_left? show_info(:lose) ask_restart end
check_win_with_lose(guess_code)
click to toggle source
# File lib/console.rb, line 93 def check_win_with_lose(guess_code) guess_code == @game.secret_code.join ? win : check_lose end
game_proccess(guess_code)
click to toggle source
# File lib/console.rb, line 88 def game_proccess(guess_code) puts @game.compare_guess_and_secret_codes(guess_code) check_win_with_lose(guess_code) end
new_storage()
click to toggle source
# File lib/console.rb, line 134 def new_storage @db = Storage.new end
play_game()
click to toggle source
# File lib/console.rb, line 79 def play_game @game ||= Game.new loop do guess_code = show_message_with_input(:message_guess_code) check_hint if guess_code == HINT @game.valid_guess_code?(guess_code) ? game_proccess(guess_code) : show_info(:unexpected_command) end end
registration()
click to toggle source
# File lib/console.rb, line 49 def registration @user_name = ask_name @info_difficulty = ask_difficulty @user = { name: user_name, **@info_difficulty, attempts_left: COUNT_ATTEMPTS, hints_left: COUNT_HINTS } play_game end
show_db_info(data)
click to toggle source
# File lib/console.rb, line 147 def show_db_info(data) @db.sort_db_info(data).each do |user| user.each do |key, value| puts "#{key}: #{value} #{"\n" if key == :hints_left} " end end end
show_hint()
click to toggle source
# File lib/console.rb, line 103 def show_hint @game.hints_left_increase(user) puts @game.give_digit_hint end
show_rules()
click to toggle source
# File lib/console.rb, line 138 def show_rules show_info(:rules) end
show_stats()
click to toggle source
# File lib/console.rb, line 142 def show_stats new_storage @db.file_exist? ? show_db_info(@db.load) : show_info(:clear_stats) end
valid_name?(name)
click to toggle source
# File lib/console.rb, line 65 def valid_name?(name) validate_length_range?(name, NAME_LENGTH_RANGE) && validate_class?(name, String) end
win()
click to toggle source
# File lib/console.rb, line 97 def win show_info(:win) ask_for_save ask_restart end