end
end
+ def result
+ top_result = resultFactory( self )
+ until @candidates.empty?
+ aResult = resultFactory( self )
+ top_result.full_results << aResult
+ filter_out(aResult)
+ end
+ top_result
+ end
+
protected
def verify_vote(vote=nil)
vote.instance_of?( Array ) and
vote == vote.uniq
end
-
+
end
class PureCondorcetVote < CondorcetVote
- def result
- PureCondorcetResult.new( self )
+ def resultFactory(init)
+ PureCondorcetResult.new(init)
end
end
class CloneproofSSDVote < CondorcetVote
- def result
- CloneproofSSDResult.new( self )
+ def resultFactory(init)
+ CloneproofSSDResult.new(init)
end
end
def tally_vote
self.verify_vote(vote)
end
+
+ def filter_out(winner)
+ if winner.winners[0].class == Array
+ to_filter = winner.winners[0]
+ else
+ to_filter = [winner.winners[0]]
+ end
+ @candidates.delete_if {|x| to_filter.include?(x)}
+ end
+
end
class PluralityVote < ElectionVote
class ElectionResult
attr_reader :winners
+ attr_accessor :full_results
def initialize(voteobj=nil)
unless voteobj and voteobj.kind_of?( ElectionVote )
@election = voteobj
@winners = Array.new
+ @full_results = Array.new
end
def winner
@winners.length > 0
end
+ def get_full_results
+ @full_results.collect {|x| x.winners}
+ end
+
end
class PluralityResult < ElectionResult
8.times {vote_array << "EBADC".split("")}
assert_equal "E", CloneproofSSDVote.new(vote_array).result.winners[0]
+ assert_equal [['E'], ['A'], ['C'], ['B'], ['D']],
+ CloneproofSSDVote.new(vote_array).result.get_full_results
end
def test_ssd2
4.times {vote_array << "DCBA".split("")}
assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0]
+ assert_equal [['D'], ['A'], ['C'], ['B']],
+ CloneproofSSDVote.new(vote_array).result.get_full_results
end
def test_ssd3
2.times {vote_array << "CBDA".split("")}
assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
+ assert_equal [['B'], ['C'], ['D'], ['A']],
+ CloneproofSSDVote.new(vote_array).result.get_full_results
end
end