Class: CodebreakerFirst::Game
- Inherits:
-
Object
- Object
- CodebreakerFirst::Game
- Defined in:
- lib/codebreaker_first.rb
Overview
Rubocop
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#game_result ⇒ Object
Returns the value of attribute game_result.
-
#user_tries ⇒ Object
Returns the value of attribute user_tries.
Instance Method Summary collapse
- #check_code(input_code) ⇒ Object
- #from_hash(data) ⇒ Object
-
#initialize(code = '', user_tries = 0, game_result = nil) ⇒ Game
constructor
A new instance of Game.
- #lose ⇒ Object
- #results ⇒ Object
- #save_result(name) ⇒ Object
- #start ⇒ Object
- #win ⇒ Object
Constructor Details
#initialize(code = '', user_tries = 0, game_result = nil) ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 |
# File 'lib/codebreaker_first.rb', line 11 def initialize(code = '', user_tries = 0, game_result = nil) @code = code @user_tries = user_tries @game_result = game_result end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
9 10 11 |
# File 'lib/codebreaker_first.rb', line 9 def code @code end |
#game_result ⇒ Object
Returns the value of attribute game_result.
9 10 11 |
# File 'lib/codebreaker_first.rb', line 9 def game_result @game_result end |
#user_tries ⇒ Object
Returns the value of attribute user_tries.
9 10 11 |
# File 'lib/codebreaker_first.rb', line 9 def user_tries @user_tries end |
Instance Method Details
#check_code(input_code) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/codebreaker_first.rb', line 29 def check_code(input_code) @user_tries += 1 return lose if @user_tries >= MAX_TRIES return win if input_code == @code validate_code input_code result = '' @code.chars.each.with_index do |char, index| case char == input_code[index] when true result << '+' else result << '-' if input_code.include? char end end result end |
#from_hash(data) ⇒ Object
17 18 19 |
# File 'lib/codebreaker_first.rb', line 17 def from_hash(data) Game.new data['code'], data['user_tries'], data['game_result'] end |
#lose ⇒ Object
54 55 56 |
# File 'lib/codebreaker_first.rb', line 54 def lose @game_result = false end |
#results ⇒ Object
64 65 66 |
# File 'lib/codebreaker_first.rb', line 64 def results Result.results end |
#save_result(name) ⇒ Object
58 59 60 61 62 |
# File 'lib/codebreaker_first.rb', line 58 def save_result(name) raise Exception, 'To save, you must finish the game!' if @game_result.nil? Result.save name, @user_tries, @game_result end |
#start ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/codebreaker_first.rb', line 21 def start return if code.length == 4 4.times do @code << rand(1...6).to_s end end |
#win ⇒ Object
50 51 52 |
# File 'lib/codebreaker_first.rb', line 50 def win @game_result = true end |