Sha256: ff012644d46c230ed53af745106d9744bd331af04d25d7c91bf53af29f23a176

Contents?: true

Size: 1.55 KB

Versions: 9

Compression:

Stored size: 1.55 KB

Contents

module Codebreaker
  module Entities
    class Game
      COUNT_NUMBERS = 4
      CODE_RANGE = (1..6).freeze
      DIFFICULTIES =
        {
          easy:
            {
              attempts: 15,
              hints: 2
            },
          medium:
            {
              attempts: 10,
              hints: 1
            },
          hard:
            {
              attempts: 5,
              hints: 1
            }
        }.freeze

      attr_reader :attempts, :hints, :code

      def initialize
        @controller = Controller.new
      end

      def init_game(difficulty)
        @difficulty = difficulty
        @code = generate_code
        @hints = @code.sample(difficulty[:hints])
        @attempts = difficulty[:attempts]
      end

      def start(guess)
        @controller.secret_code_handler(code.join, guess)
      end

      def check_win?(answer)
        code.join == answer
      end

      def hints_not_remain?
        hints.empty?
      end

      def attempt_wasted
        @attempts -= 1
      end

      def hint_take
        hints.pop
      end

      def to_h(name)
        {
          name: name,
          difficulty: DIFFICULTIES.key(@difficulty),
          all_attempts: @difficulty[:attempts],
          all_hints: @difficulty[:hints],
          attempts_wasted: @difficulty[:attempts] - @attempts,
          hints_wasted: @difficulty[:hints] - @hints.length,
          date: Time.now
        }
      end

      private

      def generate_code
        Array.new(COUNT_NUMBERS) do
          rand CODE_RANGE
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
codebreaker_artuomka-0.1.8 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.7 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.6 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.5 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.4 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.3 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.2 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.1 lib/codebreaker/entities/game.rb
codebreaker_artuomka-0.1.0 lib/codebreaker/entities/game.rb