Sha256: 1c71a33770b6b1d829d32ac545f7c7471c988021930201f9c8a030f8fca23e7d

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Bridge::Points
  class Duplicate
    def initialize(*scores)
      @scores = Array(scores).flatten.sort.reverse
    end

    def theoretical_maximum
      (@scores.size - 1) * 2
    end

    def skipped_scores(number = 1)
      @scores[number..(-1 - number)]
    end

    def average_score(number_to_skip = 1)
      scores = skipped_scores(number_to_skip)
      (scores.inject(0.0) { |result, score| result += score} / scores.size).round(-1)
    end

    def maximum
      {}.tap do |result|
        @scores.each_with_index do |score, i|
          result[score] ||= @scores[(i + 1)..-1].inject(0) { |points, s| points += (score <=> s) + 1 }
        end
      end
    end

    def maximum_in_percents(precision = 2)
      maximum.tap do |result|
        result.each do |score, points|
          result[score] = (points * 100.0 / theoretical_maximum).round(precision)
        end
      end
    end

    def butler(number_to_skip = 1)
      {}.tap do |result|
        @scores.each do |score|
          result[score] = Bridge::Points.imps(score - average_score(number_to_skip))
        end
      end
    end

    def cavendish
      {}.tap do |result|
        @scores.each do |score|
          result[score] ||= (@scores.inject(0.0) { |points, s| points += Bridge::Points.imps(score - s) } / (@scores.size - 1)).round(1)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bridge-0.1.3 lib/bridge/points/duplicate.rb