Sha256: 0ce775d4093a98f69ad27f024c63a43e561e1a3cc71519808262f71842e97cbc

Contents?: true

Size: 844 Bytes

Versions: 1

Compression:

Stored size: 844 Bytes

Contents

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

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

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

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

    def win?
      @secret_code == @player_code
    end

    private

    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.2 lib/codebreaker/modules/marker.rb