class Codebreaker::Game

Module Game - logic

Attributes

hints[R]
hints_used[R]
match_result[R]
total_attempts[R]
turn[R]
turn_statistic[R]

Public Class Methods

new() click to toggle source
# File lib/codebreaker/game.rb, line 6
def initialize
  @secret_code = code_generator
  @total_attempts = ATTEMPTS
  @hints = HINTS
  @hints_used = 0
  @match_result = ''
  @turn = 1
  @turn_statistic = {}
end

Public Instance Methods

code_view_with_hint() click to toggle source
# File lib/codebreaker/game.rb, line 41
def code_view_with_hint
  @secret_code.slice(0, @hints_used) <<
  @secret_code.slice(@hints_used, @secret_code.size).tr(@secret_code, '*')
end
counter_attepmpts_and_turn() click to toggle source
# File lib/codebreaker/game.rb, line 22
def counter_attepmpts_and_turn
  @total_attempts -= 1
  GAME_TURN + "#{@turn += 1}"
end
get_a_hint() click to toggle source
# File lib/codebreaker/game.rb, line 36
def get_a_hint
  @hints -= 1
  @hints_used += 1
end
inbound_processing(input_code) click to toggle source
# File lib/codebreaker/game.rb, line 27
def inbound_processing(input_code)
  data_checking input_code
  @attempts -= 1
end
validate_turn(input_code) click to toggle source
# File lib/codebreaker/game.rb, line 16
def validate_turn(input_code)
  data_checking input_code
  find_cows_and_bulls input_code
  counter_attepmpts_and_turn
end
winner?() click to toggle source
# File lib/codebreaker/game.rb, line 32
def winner?
  @match_result == '++++'
end

Private Instance Methods

code_generator() click to toggle source
# File lib/codebreaker/game.rb, line 48
def code_generator
  (1..4).map { rand(1..6) }.join
end
data_checking(input_code) click to toggle source
# File lib/codebreaker/game.rb, line 67
def data_checking(input_code)
  @turn_statistic[@turn] = input_code
  raise ArgumentError, 'Type only integers from 1 to 6' if input_code =~ /[^0-6]/
  raise ArgumentError, 'Type exactly 4 integer' unless input_code.length == 4
end
find_cows_and_bulls(input_code) click to toggle source
# File lib/codebreaker/game.rb, line 52
def find_cows_and_bulls(input_code)
  @match_result = ''
  input_code.each_char.with_index do |elem, i|
    @match_result << if @secret_code.include?(elem)
      if input_code[i] == @secret_code[i]
        '+'
      else
        '-'
      end
    else
      ' '
    end
  end
end