class Codebreaker::Game

Constants

CODE_RANGE_MAX
CODE_RANGE_MIN
COUNT_OF_NUMBER

Attributes

attempts_left[RW]
difficulty[RW]
hints[R]
hints_left[RW]
matcher[R]
number_of_attempts[RW]
number_of_hints[RW]
secret_code[RW]
status[R]
user[RW]

Public Class Methods

new(matcher: Matcher.new) click to toggle source
# File lib/codebreaker_mats/game.rb, line 10
def initialize(matcher: Matcher.new)
  @matcher = matcher
end

Public Instance Methods

attempts_available?() click to toggle source
# File lib/codebreaker_mats/game.rb, line 24
def attempts_available?
  return false if won? || lost?

  attempts_left.positive? || lost
end
guess(guess) click to toggle source
# File lib/codebreaker_mats/game.rb, line 30
def guess(guess)
  @attempts_left -= 1
  matcher.match?(guess) && won
end
hint!() click to toggle source
# File lib/codebreaker_mats/game.rb, line 39
def hint!
  if hints_left.positive?
    @hints_left -= 1
    hints.shift
  end
end
lost?() click to toggle source
# File lib/codebreaker_mats/game.rb, line 50
def lost?
  status == :lost
end
marks() click to toggle source
# File lib/codebreaker_mats/game.rb, line 35
def marks
  matcher.marks
end
save_stat(stat_file) click to toggle source
# File lib/codebreaker_mats/game.rb, line 54
def save_stat(stat_file)
  save_stat_in_file(stat_file, stats_hash)
end
set_difficulty(diff, attempts, hints) click to toggle source
# File lib/codebreaker_mats/game.rb, line 67
def set_difficulty(diff, attempts, hints)
  @number_of_attempts = @attempts_left = attempts
  @number_of_hints = @hints_left = hints
  @difficulty = diff
end
start() click to toggle source
# File lib/codebreaker_mats/game.rb, line 14
def start
  generate_secret_code
  generate_hints
  @attempts_left = @number_of_attempts = 0
  @hints_left = @number_of_hints = 0
  matcher.secret_code = @secret_code
  @status = :in_progress
  @difficulty
end
stats_hash() click to toggle source
# File lib/codebreaker_mats/game.rb, line 58
def stats_hash
  { user: @user,
    difficulty: @difficulty,
    attempts: @number_of_attempts,
    attempts_used: @number_of_attempts - @attempts_left,
    hints: @number_of_hints,
    hints_used: @number_of_hints - @hints_left }
end
won?() click to toggle source
# File lib/codebreaker_mats/game.rb, line 46
def won?
  status == :won
end

Private Instance Methods

generate_hints() click to toggle source
# File lib/codebreaker_mats/game.rb, line 83
def generate_hints
  @hints = @secret_code.split('').shuffle
end
generate_secret_code() click to toggle source
# File lib/codebreaker_mats/game.rb, line 75
def generate_secret_code
  @secret_code = Array.new(COUNT_OF_NUMBER) { roll_dice }.join
end
lost() click to toggle source
# File lib/codebreaker_mats/game.rb, line 92
def lost
  @status = :lost
  false
end
roll_dice() click to toggle source
# File lib/codebreaker_mats/game.rb, line 79
def roll_dice
  rand(CODE_RANGE_MIN..CODE_RANGE_MAX)
end
won() click to toggle source
# File lib/codebreaker_mats/game.rb, line 87
def won
  @status = :won
  true
end