lib/codebreaker.rb in codebreaker_vv-0.2.0 vs lib/codebreaker.rb in codebreaker_vv-1.0.0

- old
+ new

@@ -1,77 +1,70 @@ -# require_relative 'codebreaker/version' require_relative 'src/autoloader' -module Codebreaker - class Error < StandardError; end +class Codebreaker + attr_reader :secret_code, :attempts, :hints - class << self - attr_reader :secret_code, :attempts, :hints + def initialize + @data_base_manager = DataBaseManager.new + end - def start_game(difficulty, player_name) - @total_attempts = 0 - @total_hints = 1 - @difficulty = difficulty - @player_name = player_name - init_difficulty_stats(difficulty) - @hints = @total_hints - @attempts = @total_attempts - generate_secret_code - end + def start_game(difficulty, player_name) + @difficulty = difficulty + @player_name = player_name + init_difficulty_stats(difficulty) + @hints = @total_hints + @attempts = @total_attempts + generate_secret_code + end - def check_code_and_give_result(user_code) - answer = @verifier.verify_user_code(user_code) - @attempts -= 1 + def check_code_and_give_result(user_code) + answer = @verifier.verify_user_code(user_code) + @attempts -= 1 - if answer == '++++' - "#{answer} #{Locale::PLAYER_WIN}" - elsif @attempts <= 0 - "#{answer} #{Locale::PLAYER_LOSE}" - else - answer - end + if answer == '++++' + "#{answer} #{Locale::PLAYER_WIN}" + elsif @attempts <= 0 + "#{answer} #{Locale::PLAYER_LOSE}" + else + answer end + end - def give_hint - if @hints <= 0 - Locale::NO_MORE_HINTS_AVAILABLE - else - @hints -= 1 - hint = @code_for_hints.sample - @code_for_hints.delete_at(@code_for_hints.index(hint)) - hint - end + def give_hint + if @hints <= 0 + Locale::NO_MORE_HINTS_AVAILABLE + else + @hints -= 1 + hint = @code_for_hints.sample + @code_for_hints.delete_at(@code_for_hints.index(hint)) + hint end + end - def save_game_result - attempts_used = @total_attempts - @attempts - hints_used = @total_hints - @hints - DataBaseManager.instance.save_game_results([@player_name, @difficulty, - @total_attempts, attempts_used, - @total_hints, hints_used]) - end + def save_game_result + attempts_used = @total_attempts - @attempts + hints_used = @total_hints - @hints + @data_base_manager.save_game_results([@player_name, @difficulty, + @total_attempts, attempts_used, + @total_hints, hints_used]) + end - def load_game_results - DataBaseManager.instance.load_game_results - end + def load_game_results + @data_base_manager.load_game_results + end - private + private - def init_difficulty_stats(difficulty) - case difficulty - when 0 - @total_attempts = 15 - @total_hints = 2 - when 1 - @total_attempts = 10 - else - @total_attempts = 5 - end - end + def init_difficulty_stats(difficulty) + @total_attempts, @total_hints = case difficulty + when 0 then [15, 2] + when 1 then [10, 1] + else [5, 1] + end + end - def generate_secret_code - @secret_code = Factory.new.new_secret_code - @verifier = CodeVerifier.new(@secret_code) - @code_for_hints = @secret_code.clone - end + def generate_secret_code + @secret_code = Factory.new_secret_code + @verifier = CodeVerifier.new(@secret_code) + @code_for_hints = @secret_code.clone end end