lib/codebreaker/game.rb in codebreaker_kub-0.2.4 vs lib/codebreaker/game.rb in codebreaker_kub-0.2.5
- old
+ new
@@ -1,27 +1,27 @@
# frozen_string_literal: true
module Codebreaker
class Game
- include Codebreaker::GuessChecker
-
- attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
+ attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
+ :all_attempts
attr_reader :hints_code
CODE_LENGTH = 4
CODE_RANGE = (1..6).freeze
def initialize
@difficulties = Codebreaker::Loader.load('difficulties')
@code = generate_code
@hints_code = @code.chars.shuffle
- symbols
end
def game_option(name, difficulty)
@name = name
@difficulty = difficulty
+ @all_attempts = difficulty_option[:attempts]
+ @all_hints = difficulty_option[:hints]
@attempts_left = difficulty_option[:attempts]
@hints_left = difficulty_option[:hints]
end
def hint
@@ -38,23 +38,23 @@
def input_operation(input_code)
@input_code = input_code
return unless attempts_left?
@attempts_left -= 1
- check_input(@code, input_code)
+ check_input(@code, @input_code)
end
def attempts_left?
attempts_left.positive?
end
def win?
input_code == code
end
- def save
- Codebreaker::Loader.save(to_h, 'stats')
+ def save(filename = 'stats')
+ Codebreaker::Loader.save(to_h, filename)
end
private
def generate_code
@@ -67,18 +67,25 @@
def difficulty_option
@difficulties[@difficulty]
end
+ def check_input(code, input)
+ guess_checker = Codebreaker::GuessChecker.new(code, input)
+ guess_checker.symbols
+ guess_checker.check_input
+ end
+
def to_h
{
name: @name,
difficulty: @difficulty,
- attempts: difficulty_option[:attempts],
- hints: difficulty_option[:hints],
+ attempts: @all_attempts,
+ hints: @all_hints,
code: @code,
- used_attempts: difficulty_option[:attempts] - @attempts_left,
- used_hints: difficulty_option[:hints] - @hints_left
+ used_attempts: @all_attempts - @attempts_left,
+ used_hints: @all_hints - @hints_left,
+ date: Time.now
}
end
end
end