Sha256: a30f971d629e4f82a412ac6388b0d9575d001700ccba92041ea077c29b7fd8ee

Contents?: true

Size: 902 Bytes

Versions: 3

Compression:

Stored size: 902 Bytes

Contents

# frozen_string_literal: true

module CodebreakerKirill
  class Game
    include CodeGenerator

    attr_reader :user

    def initialize(user)
      @secret_code = CodeGenerator.call
      @user = user
    end

    def give_a_hint
      return nil if @user.hints_used >= @user.level[:hints]

      @user.hints_used += 1
      @secret_code.shuffle[@user.hints_used]
    end

    def respond_to_guess(input)
      @user.attempts_used += 1
      status = status(input, @secret_code)
      resolved_code = GuessHandler.new(input, @secret_code).call
      return { status: status, secret_code: @secret_code } if status == 'loss'

      { status: status, response: resolved_code, secret_code: @secret_code }
    end

    def status(input, code)
      return 'loss' if @user.attempts_used >= @user.level[:attempts]
      return 'win' if input.each_char.map(&:to_i) == code

      'in_game'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
codebreaker_kirill-2.0.0 lib/codebreaker_kirill/game.rb
codebreaker_kirill-1.3.7 lib/codebreaker_kirill/game.rb
codebreaker_kirill-1.3.6 lib/codebreaker_kirill/game.rb