class Codebreaker::Game
Constants
- EXACT_MATCH_SIGN
- NOT_EXACT_MATCH_SIGN
- SECRET_CODE_LENGTH
- SECRET_CODE_RANGE
Attributes
attempts_left[R]
attempts_used[R]
difficulty[R]
hints[R]
hints_left[R]
hints_used[R]
player[R]
secret_number[R]
Public Class Methods
new(player, difficulty)
click to toggle source
# File lib/codebreaker/game.rb, line 12 def initialize(player, difficulty) @player = player @difficulty = difficulty.level @secret_number = code_maker assign_hints @hints_used = 0 @attempts_used = 0 @hints_left = @difficulty[:hints] @attempts_left = @difficulty[:attempts] @hints = [] end
Public Instance Methods
attempt()
click to toggle source
# File lib/codebreaker/game.rb, line 32 def attempt @attempts_used += 1 @attempts_left -= 1 end
attempts_available?()
click to toggle source
# File lib/codebreaker/game.rb, line 46 def attempts_available? @attempts_used < @difficulty[:attempts] end
attempts_total()
click to toggle source
# File lib/codebreaker/game.rb, line 28 def attempts_total @difficulty[:attempts] end
compare(player_number)
click to toggle source
# File lib/codebreaker/game.rb, line 54 def compare(player_number) return EXACT_MATCH_SIGN * SECRET_CODE_LENGTH if win? || !attempts_available? attempt compare_numbers(player_number) end
hint()
click to toggle source
# File lib/codebreaker/game.rb, line 61 def hint increment_hint if hints_available? value = @assigned_hints.pop @hints << value value end
hints_available?()
click to toggle source
# File lib/codebreaker/game.rb, line 42 def hints_available? @hints_used < @difficulty[:hints] end
hints_total()
click to toggle source
# File lib/codebreaker/game.rb, line 24 def hints_total @difficulty[:hints] end
increment_hint()
click to toggle source
# File lib/codebreaker/game.rb, line 37 def increment_hint @hints_used += 1 @hints_left -= 1 end
win?()
click to toggle source
# File lib/codebreaker/game.rb, line 50 def win? @output == EXACT_MATCH_SIGN * SECRET_CODE_LENGTH end
Private Instance Methods
assign_hints()
click to toggle source
# File lib/codebreaker/game.rb, line 71 def assign_hints @assigned_hints = @secret_number.chars.shuffle.sample(difficulty[:hints]) end
code_maker()
click to toggle source
# File lib/codebreaker/game.rb, line 75 def code_maker Array.new(SECRET_CODE_LENGTH) { rand(SECRET_CODE_RANGE) }.join.to_s end
compare_numbers(player_number)
click to toggle source
# File lib/codebreaker/game.rb, line 79 def compare_numbers(player_number) secret_digits = @secret_number.chars player_digits = player_number.chars @output = strong_match(secret_digits, player_digits) @output += not_strong_match(secret_digits, player_digits) end
not_strong_match(secret_digits, player_digits)
click to toggle source
# File lib/codebreaker/game.rb, line 98 def not_strong_match(secret_digits, player_digits) output = '' secret_digits.each do |digit| next unless digit && player_digits.include?(digit) output += NOT_EXACT_MATCH_SIGN player_digits.delete_at(player_digits.index(digit)) end output end
strong_match(secret_digits, player_digits)
click to toggle source
# File lib/codebreaker/game.rb, line 86 def strong_match(secret_digits, player_digits) output = '' secret_digits.each_with_index do |digit, index| next unless digit == player_digits[index] output += EXACT_MATCH_SIGN secret_digits[index] = nil player_digits[index] = nil end output end