Class: CodebreakerSmn::Game
- Inherits:
-
Object
- Object
- CodebreakerSmn::Game
- Includes:
- ValidationHelper
- Defined in:
- lib/codebreaker_smn.rb
Constant Summary collapse
- DIFFICULTIES =
Codebreaker class Codemaker class DB Helper class/module Stats class Output class -> logic regarding returning messages on errors/hints/help/etc. Validation class Custom errors class
{ easy: {attempts: 15, hints: 2}, medium: {attempts: 10, hints: 1}, hell: {attempts: 5, hints: 1} }
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#hints ⇒ Object
Returns the value of attribute hints.
-
#results ⇒ Object
Returns the value of attribute results.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #game_over ⇒ Object
- #generate_code ⇒ Object
-
#get_hint ⇒ Object
TODO!.
- #guess_code(input) ⇒ Object
-
#high_scores ⇒ Object
TODO!!!!.
-
#initialize ⇒ Game
constructor
A new instance of Game.
-
#load_results ⇒ Object
TODO!!!! db path should be provided from Console.
- #new_game ⇒ Object
- #process_guess(input) ⇒ Object
-
#save_results ⇒ Object
TODO!!!!.
- #set_difficulty(level) ⇒ Object
- #start ⇒ Object
- #win ⇒ Object
Methods included from ValidationHelper
#not_empty_string, #positive_integer, #valid_digits, #valid_guess_length, #valid_name?, #valid_username_length, #validate_guess
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
26 27 28 29 |
# File 'lib/codebreaker_smn.rb', line 26 def initialize new_game @results = load_results end |
Instance Attribute Details
#attempts ⇒ Object
Returns the value of attribute attempts.
24 25 26 |
# File 'lib/codebreaker_smn.rb', line 24 def attempts @attempts end |
#code ⇒ Object
Returns the value of attribute code.
23 24 25 |
# File 'lib/codebreaker_smn.rb', line 23 def code @code end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
23 24 25 |
# File 'lib/codebreaker_smn.rb', line 23 def difficulty @difficulty end |
#hints ⇒ Object
Returns the value of attribute hints.
24 25 26 |
# File 'lib/codebreaker_smn.rb', line 24 def hints @hints end |
#results ⇒ Object
Returns the value of attribute results.
24 25 26 |
# File 'lib/codebreaker_smn.rb', line 24 def results @results end |
#state ⇒ Object
Returns the value of attribute state.
23 24 25 |
# File 'lib/codebreaker_smn.rb', line 23 def state @state end |
#username ⇒ Object
Returns the value of attribute username.
24 25 26 |
# File 'lib/codebreaker_smn.rb', line 24 def username @username end |
Instance Method Details
#game_over ⇒ Object
45 46 47 |
# File 'lib/codebreaker_smn.rb', line 45 def game_over @state = :game_over end |
#generate_code ⇒ Object
97 98 99 |
# File 'lib/codebreaker_smn.rb', line 97 def generate_code @code = Array.new(4) { rand(1..6) } end |
#get_hint ⇒ Object
TODO!
145 146 147 148 149 150 151 152 |
# File 'lib/codebreaker_smn.rb', line 145 def get_hint # TODO! if @hints > 0 && @state == :started @hints -= 1 code.sample else 'No hints left!' end end |
#guess_code(input) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/codebreaker_smn.rb', line 101 def guess_code(input) validate_guess(input) result = "" if @attempts > 1 && @state == :started @attempts -= 1 result = process_guess(input) win if result == "++++" elsif @attempts == 1 && @state == :started @attempts -= 1 result = process_guess(input) result == "++++" ? win : game_over else game_over end result end |
#high_scores ⇒ Object
TODO!!!!
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/codebreaker_smn.rb', line 75 def high_scores # TODO!!!! attempts_total = DIFFICULTIES[@difficulty.to_sym][:attempts] attempts_used = attempts_total - @attempts hints_total = DIFFICULTIES[@difficulty.to_sym][:hints] hints_used = hints_total - @hints { name: @username, difficulty: @difficulty, attempts_total: attempts_total, attempts_used: attempts_used, hints_total: hints_total, hints_used: hints_used, date: Date.today } # This method should combine and sort stat inside @results var and just return this @results var # We should return @results from just last game -> Console or Web app should form Stat on its own #puts 'Name, Difficulty, Attempts Total, Attempts Used, Hints Total, Hints Used' #@results.each do |result| # puts %Q(#{result[:name]} #{result[:difficulty]} #{result[:attempts_total].to_s} #{result[:attempts_used].to_s} #{result[:hints_total].to_s} #{result[:hints_used].to_s}) #end end |
#load_results ⇒ Object
TODO!!!! db path should be provided from Console
49 50 51 52 53 |
# File 'lib/codebreaker_smn.rb', line 49 def load_results # TODO!!!! db path should be provided from Console YAML.load_file('./db/results.yml') rescue SystemCallError [] end |
#new_game ⇒ Object
31 32 33 34 |
# File 'lib/codebreaker_smn.rb', line 31 def new_game reset_params @state = :new end |
#process_guess(input) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/codebreaker_smn.rb', line 118 def process_guess(input) temp_code = @code.clone input = input.split("") result = "" exclude_indexes = [] input.each_with_index do |char, index| if temp_code.include?(char.to_i) && (temp_code[index] == char.to_i) result << "+" exclude_indexes << index end end exclude_indexes.reverse_each do |index| input.delete_at(index) temp_code.delete_at(index) end temp_code.each_with_index do |char, index| if input.include?(char.to_s) # input.uniq.include? or temp_code.uniq.include? result << "-" temp_code[index] = nil # We have to check if char is one in the input or several -> count of matches in input end # code = 1455, input = 5133 end result end |
#save_results ⇒ Object
TODO!!!!
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/codebreaker_smn.rb', line 55 def save_results # TODO!!!! if @state == :win attempts_total = DIFFICULTIES[@difficulty.to_sym][:attempts] attempts_used = attempts_total - @attempts hints_total = DIFFICULTIES[@difficulty.to_sym][:hints] hints_used = hints_total - @hints @results << {name: @username, difficulty: @difficulty, attempts_total: attempts_total, attempts_used: attempts_used, hints_total: hints_total, hints_used: hints_used, date: Date.today} File.write('./db/results.yml', @results.to_yaml) end end |
#set_difficulty(level) ⇒ Object
154 155 156 157 158 |
# File 'lib/codebreaker_smn.rb', line 154 def set_difficulty(level) @difficulty = level @attempts = DIFFICULTIES[level.to_sym][:attempts] @hints = DIFFICULTIES[level.to_sym][:hints] end |
#start ⇒ Object
36 37 38 39 |
# File 'lib/codebreaker_smn.rb', line 36 def start generate_code @state = :started end |
#win ⇒ Object
41 42 43 |
# File 'lib/codebreaker_smn.rb', line 41 def win @state = :win end |