Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/rk/codebreaker/game.rb
Constant Summary collapse
- ATTEMPT_NUMBER =
10
Instance Attribute Summary collapse
-
#available_attempts ⇒ Object
readonly
Returns the value of attribute available_attempts.
-
#hint ⇒ Object
readonly
Returns the value of attribute hint.
Instance Method Summary collapse
- #check_input(code) ⇒ Object
- #check_matches(user_code) ⇒ Object
- #hint_answer ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #save_to_file(filename, username) ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 |
# File 'lib/rk/codebreaker/game.rb', line 8 def initialize @hint = true @available_attempts = ATTEMPT_NUMBER @result = "" @secret_code = generate_code end |
Instance Attribute Details
#available_attempts ⇒ Object (readonly)
Returns the value of attribute available_attempts.
6 7 8 |
# File 'lib/rk/codebreaker/game.rb', line 6 def available_attempts @available_attempts end |
#hint ⇒ Object (readonly)
Returns the value of attribute hint.
6 7 8 |
# File 'lib/rk/codebreaker/game.rb', line 6 def hint @hint end |
Instance Method Details
#check_input(code) ⇒ Object
15 16 17 18 19 |
# File 'lib/rk/codebreaker/game.rb', line 15 def check_input(code) return 'Incorrect format' unless code_valid?(code) @available_attempts -= 1 return check_matches(code) end |
#check_matches(user_code) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/rk/codebreaker/game.rb', line 26 def check_matches(user_code) @result = "" return @result = '++++' if user_code == @secret_code sum_array = @secret_code.chars.zip(user_code.chars) exact_match_calculation(sum_array) just_match_calculation(sum_array) @result end |
#hint_answer ⇒ Object
21 22 23 24 |
# File 'lib/rk/codebreaker/game.rb', line 21 def hint_answer @hint = false @secret_code.split('').sample end |
#save_to_file(filename, username) ⇒ Object
35 36 37 38 39 |
# File 'lib/rk/codebreaker/game.rb', line 35 def save_to_file(filename, username) File.open(filename, 'a') do |file| file.puts "#{username}|used attempts #{ATTEMPT_NUMBER - @available_attempts};" end end |