Thursday, June 25, 2015

Pair Ranking Algorithm

require 'highline/import'
require "pp"
require 'rasam'

include Rasam

options = ask("Enter your choices (or a blank line to quit):",
lambda { |ans| ans =~ /^-?\d+$/ ? Integer(ans) : ans} ) do |q|
  q.gather = ""
end

@pr = PairRank.new(options)
@saved_combinations = Array.new(@pr.combinations)
combinations = Array.new(@pr.combinations)

pair = combinations.shift

def get_user_choice_for(pair)
  choose do |menu|
    menu.prompt = "Please choose your favorite: "

    pair.each do |c|
      menu.choice(c) do
        say(c)

        rationale = ask("Why?  ")

        say(rationale)

        rc = RationalChoice.new(pair, c, rationale)
        @pr.make(rc)
      end
    end
  end
end

loop do
  p pair  
  get_user_choice_for(pair)
  break if combinations.empty?
  pair = combinations.shift
end


@pr.decisions.each do |d|
  p d.to_s
end


p @pr.score_for('A')
p @pr.score_for('B')
p @pr.score_for('C')

def handle_ties(pair)
  choose do |menu|
    menu.prompt = "Please choose your favorite: "

    pair.each do |c|
      menu.choice(c) do
        say(c)

        rationale = ask("Why?  ")

        say(rationale)

        rc = RationalChoice.new(pair, c, rationale)
        @pr.make(rc)
      end
    end
  end
end

loop do
  p 'Handling a tie'
  if @pr.tied_pair.empty?
    break
  else
    handle_ties(@pr.tied_pair)
   
    @pr.decisions.each do |d|
      p d.to_s
    end
   
    p @pr.score_for('A')
    p @pr.score_for('B')
    p @pr.score_for('C')
    p @pr.tied_pair
   
  end
end