Class: Codebraker::Game
- Inherits:
-
Object
- Object
- Codebraker::Game
- Defined in:
- lib/codebracker/game.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#result_of_comparing ⇒ Object
readonly
Returns the value of attribute result_of_comparing.
Instance Method Summary collapse
- #compare_codes ⇒ Object
- #count_score ⇒ Object
- #decrease_attempts ⇒ Object
- #generate_code ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #loose? ⇒ Boolean
- #show_hint ⇒ Object
- #show_result_of_comparing ⇒ Object
- #start(user_suggested_code) ⇒ Object
- #use_hint ⇒ Object
- #validate_input(input) ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 14 15 16 |
# File 'lib/codebracker/game.rb', line 9 def initialize @secret_code = '' @user_suggested_code = '' @attempts = 5 @hints = 2 @score = 0 generate_code end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
7 8 9 |
# File 'lib/codebracker/game.rb', line 7 def attempts @attempts end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
7 8 9 |
# File 'lib/codebracker/game.rb', line 7 def hints @hints end |
#result_of_comparing ⇒ Object (readonly)
Returns the value of attribute result_of_comparing.
7 8 9 |
# File 'lib/codebracker/game.rb', line 7 def result_of_comparing @result_of_comparing end |
Instance Method Details
#compare_codes ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/codebracker/game.rb', line 43 def compare_codes @secret_code_for_comparing = @secret_code.dup 4.times { |i| if @secret_code_for_comparing[i] == @user_suggested_code[i] @secret_code_for_comparing[i] = '$' @user_suggested_code[i] = '+' end } 4.times { |i| if @secret_code_for_comparing.include?(@user_suggested_code[i]) @secret_code_for_comparing.sub!(@user_suggested_code[i], '$') @user_suggested_code[i] = '-' end } 4.times { |i| if /[\d]/ =~ @user_suggested_code[i] @user_suggested_code[i] = ' ' end } @result_of_comparing = @user_suggested_code.dup end |
#count_score ⇒ Object
65 66 67 |
# File 'lib/codebracker/game.rb', line 65 def count_score @score = @attempts * 10 + @hints * 15 end |
#decrease_attempts ⇒ Object
69 70 71 |
# File 'lib/codebracker/game.rb', line 69 def decrease_attempts @attempts -= 1 end |
#generate_code ⇒ Object
34 35 36 |
# File 'lib/codebracker/game.rb', line 34 def generate_code @secret_code = (1..4).map { rand(1..6) }.join end |
#loose? ⇒ Boolean
30 31 32 |
# File 'lib/codebracker/game.rb', line 30 def loose? @attempts.zero? end |
#show_hint ⇒ Object
79 80 81 |
# File 'lib/codebracker/game.rb', line 79 def show_hint puts @secret_code[rand(0..3)] end |
#show_result_of_comparing ⇒ Object
83 84 85 |
# File 'lib/codebracker/game.rb', line 83 def show_result_of_comparing puts @user_suggested_code end |
#start(user_suggested_code) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/codebracker/game.rb', line 18 def start(user_suggested_code) @user_suggested_code = user_suggested_code decrease_attempts validate_input(user_suggested_code) compare_codes show_result_of_comparing end |
#use_hint ⇒ Object
73 74 75 76 77 |
# File 'lib/codebracker/game.rb', line 73 def use_hint return ABSENT_HITNS_MESSAGE if @hints.zero? @hints -= 1 show_hint end |
#validate_input(input) ⇒ Object
38 39 40 41 |
# File 'lib/codebracker/game.rb', line 38 def validate_input(input) raise ArgumentError, 'Type exactly 4 integer' unless input.length == 4 raise ArgumentError, 'Type only integers from 1 to 6' if input =~ /[^1-6]/ end |
#win? ⇒ Boolean
26 27 28 |
# File 'lib/codebracker/game.rb', line 26 def win? @result_of_comparing == '++++' end |