lib/codebreaker_first.rb in codebreaker_first-0.4.0 vs lib/codebreaker_first.rb in codebreaker_first-0.5.0
- old
+ new
@@ -1,9 +1,9 @@
require 'json'
require_relative 'codebreaker_first/version'
-module Codebreaker
+module CodebreakerFirst
MAX_TRIES = 6
# Rubocop
class Game
attr_accessor :code, :user_tries, :game_result
@@ -17,37 +17,42 @@
def from_hash(data)
Game.new data['code'], data['user_tries'], data['game_result']
end
def start
+ return if code.length
+
4.times do
@code << rand(1...6).to_s
end
end
def check_code(input_code)
@user_tries += 1
- return loose if @user_tries >= MAX_TRIES
+ 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|
- result << '+' if char == input_code[index]
-
- result << '-' unless char == input_code[index]
+ case char == input_code[index]
+ when true
+ result << '+'
+ else
+ result << '-' if input_code.include? char
+ end
end
result
end
def win
@game_result = true
end
- def loose
+ def lose
@game_result = false
end
def save_result(name)
raise Exception, 'To save, you must finish the game!' if @game_result.nil?