Sha256: 71af18d76e3a2fa245dc8e677e5015f2e175393cad60f94c1f1d410d679111a1

Contents?: true

Size: 863 Bytes

Versions: 1

Compression:

Stored size: 863 Bytes

Contents

module Codebreaker
  module Marker
    def check_win
      return 'Congratulation! You win!' if win?
      if @attempts.zero?
        @hint = 0
        @game_start = false
        "Game over! Secret code is #{@secret_code}."
      else
        marking
      end
    end

    def win?
      @secret_code == @player_code
    end

    private

    def marking
      @player_code = convert_to_a(@player_code)
      @attempts -= 1
      pluses + minuses
    end

    def pluses
      @player_code.zip(@secret_code.chars).map {|array| '+' if array[0] == array[1]}.compact.join
    end

    def minuses
      secret = convert_to_a(@secret_code)
      array = @player_code.map { |num| secret[secret.find_index(num)] = '-' if secret.include?(num) }
      '-' * (array.count('-') - pluses.length)
    end

    def convert_to_a(code)
      code.chars.to_a
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
Codebreaker_RG2016-0.2.3 lib/codebreaker/modules/marker.rb