Sha256: 246fb1b170493dd249fdec69176d666d77052a00ed4fc01fadbb2473cfe01b8b

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'yaml'

module Codebreaker
  class Game
    
  attr_accessor :attempts
     
  HINT = 1
  ATTEMPS = 10
  
  PROPER = '+'
  WRONG_POSITION = '-'
    
  def initialize
    @secret_code = Array.new(4) { rand(1..6) }.join
    @hint = HINT
    @attempts = ATTEMPS
    @win_status = false
  end
    
  def match_guess(code)
    return "Your code is not valid" unless code.match(/(^([1-6]{4})$)/)
    
    @win_status = true if @secret_code == code
    @attempts -= 1
    secret = @secret_code.chars.map(&:to_i)
    
    res = []
    code.chars.map(&:to_i).each_with_index do |number, index|
      if number == secret[index]
        secret[index] = nil
        res << PROPER
      elsif secret.include? number
        secret[secret.find_index(number)] = nil
        res << WRONG_POSITION
      end  
    end 
   
    res.join
  end
    
  def loose?
    @attempts == 0  
  end  
  
  def win?
    @win_status
  end  

  def get_hint
    @hint -= 1
    @hint >= 0 ? @secret_code[rand(4)] : "Not Available"
  end 
  
  def save_stat( username )
    File.open('./stat.yml','a+') do |f|
      f.write({user: username, attempts: @attempts, date: Time.now}.to_yaml)
    end
  end
  
  def stat
    records = []
    records = Psych.load_stream(File.open('./stat.yml'))
    records.sort_by { |record| record[:attempts] }
  end  
  
  end
end    

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
edlvj_codebreaker-0.3.3 lib/edlvj_codebreaker.rb