Class: CodebreakerVolkova::Game
- Inherits:
-
Object
- Object
- CodebreakerVolkova::Game
- Includes:
- GameBusinessLogic, Validator
- Defined in:
- lib/codebreaker_volkova/game_progress.rb
Constant Summary collapse
- FILENAME =
'store.yml'
- SIGN_CONSTANT =
4
- NUMBER_CONSTANT =
6
Constants included from Validator
Validator::LEVEL_OF_GAME, Validator::VALID_GUESS_LENGTH, Validator::VALID_NAME_LENGTH, Validator::VALID_NUMBERS
Constants included from GameBusinessLogic
CodebreakerVolkova::GameBusinessLogic::DIFFICULTIES, CodebreakerVolkova::GameBusinessLogic::RESULT_MINUS, CodebreakerVolkova::GameBusinessLogic::RESULT_PLUS
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#attempts_total ⇒ Object
readonly
Returns the value of attribute attempts_total.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#game_over ⇒ Object
readonly
Returns the value of attribute game_over.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#hints_total ⇒ Object
readonly
Returns the value of attribute hints_total.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #add_difficulty(difficulty) ⇒ Object
- #add_name(name) ⇒ Object
- #attempts? ⇒ Boolean
- #check_result(guess) ⇒ Object
- #convert_to_hash ⇒ Object
- #generate_secret_number(sign = SIGN_CONSTANT, number = NUMBER_CONSTANT) ⇒ Object
- #hints? ⇒ Boolean
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #load_stats(filename_path = FILENAME) ⇒ Object
- #save_stats(filename_path = FILENAME) ⇒ Object
- #start ⇒ Object
Methods included from Validator
#check_constituents_difficulty?, #check_constituents_guess?, #check_length_guess?, #check_length_name?, #instance?, #not_empty?
Methods included from GameBusinessLogic
#take_attempt, #take_hint, #take_level_of_difficulty, #take_pluses, #take_result
Constructor Details
#initialize ⇒ Game
15 16 17 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 15 def initialize @game_over = false end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def attempts @attempts end |
#attempts_total ⇒ Object (readonly)
Returns the value of attribute attempts_total.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def attempts_total @attempts_total end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def difficulty @difficulty end |
#game_over ⇒ Object (readonly)
Returns the value of attribute game_over.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def game_over @game_over end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def hints @hints end |
#hints_total ⇒ Object (readonly)
Returns the value of attribute hints_total.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def hints_total @hints_total end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def secret_code @secret_code end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
8 9 10 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 8 def username @username end |
Instance Method Details
#add_difficulty(difficulty) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 29 def add_difficulty(difficulty) validate_difficulty(difficulty) @difficulty = difficulty @attempts_total = take_attempt(difficulty) @attempts = @attempts_total @hints_total = take_hint(difficulty) @hints = @hints_total end |
#add_name(name) ⇒ Object
24 25 26 27 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 24 def add_name(name) validate_name(name) @username = name end |
#attempts? ⇒ Boolean
47 48 49 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 47 def attempts? @attempts.positive? end |
#check_result(guess) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 38 def check_result(guess) validate_guess(guess) guess = guess.each_char.map(&:to_i) result = take_result(@secret_code, guess) @game_over = true if result == GameBusinessLogic::RESULT_PLUS * 4 @attempts -= 1 result end |
#convert_to_hash ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 68 def convert_to_hash { name: @username, difficulty: @difficulty, attempts_total: @attempts_total, attempts_used: @attempts_total - @attempts, hints_total: @hints_total, hints_used: @hints_total - @hints } end |
#generate_secret_number(sign = SIGN_CONSTANT, number = NUMBER_CONSTANT) ⇒ Object
58 59 60 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 58 def generate_secret_number(sign = SIGN_CONSTANT, number = NUMBER_CONSTANT) Array.new(sign) { rand(1..number) } end |
#hints? ⇒ Boolean
51 52 53 54 55 56 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 51 def hints? return false unless @hints.positive? @hints -= 1 @hints_code.pop end |
#load_stats(filename_path = FILENAME) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 79 def load_stats(filename_path = FILENAME) return false unless File.exist?(filename_path) return false if File.zero?(filename_path) documents = YAML.load_stream(File.open(filename_path)) documents.map.sort_by do |item| [take_level_of_difficulty(item[:difficulty]), item[:attempts_used], item[:hints_used]] end end |
#save_stats(filename_path = FILENAME) ⇒ Object
62 63 64 65 66 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 62 def save_stats(filename_path = FILENAME) File.open(filename_path, 'a') do |file| file.write(convert_to_hash.to_yaml) end end |
#start ⇒ Object
19 20 21 22 |
# File 'lib/codebreaker_volkova/game_progress.rb', line 19 def start @secret_code = generate_secret_number @hints_code = @secret_code.dup.shuffle end |