Sha256: 0f8b8145f9226d84b4da8b3a42d61094a0e0f2ecb7b4c22770eec35a1465fcc9

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Codebreaker
  module Engine
    def check_guess
      @player_code = @player.guess
      if @player_code == 'hint' && @hint.nonzero?
        puts 'Hint: Secret code contains: ' + @secret_code[rand(0..3)]
        @hint -= 1
        @attempts += 1
      end
      check_win
    end

    private

    def check_win
      if win?
        puts 'Congratulation! You win!'
      elsif @attempts.zero?
        puts "Game over! Secret code is #{@secret_code}."
        @hint = 0
      else
        puts((pluses + minuses).join)
        @attempts -= 1
        start
      end
    end

    def array_player_code
      @player_code.chars.to_a
    end

    def pluses
      array_player_code.map.with_index { |num, index| '+' if num == @secret_code[index] }.compact
    end

    def minuses
      secret_code = @secret_code.chars.to_a
      array_player_code.map{ |num| secret_code[secret_code.find_index(num)] = "-" if secret_code.include?(num)}
      secret_code.select{ |num| num == "-" }.drop(pluses.size)
    end

    def win?
      @secret_code == @player_code
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
Codebreaker_RG2016-0.1.6 lib/codebreaker/modules/engine.rb