Created
August 26, 2011 20:37
-
-
Save mseymour/1174377 to your computer and use it in GitHub Desktop.
Acrobot acro game class (incomplete 2011-08-25 @ 5:37 PM)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Games | |
module Acro | |
class Game | |
attr_reader :acronym | |
attr_reader :answers | |
attr_reader :voters | |
def initialize acronym = nil | |
@acronym = acronym ||= Random.new.rand(3..10).times.map { [*?A...?Z].sample }.join | |
@answers = [] | |
@voters = [] | |
Answer = Struct.new(:answer, :nick, :time, :votes) | |
end | |
def add_answer answer, nick | |
@answers << Answer.new(answer, nick, time.now, 0) | |
end | |
def vote! id, nick | |
raise "You already voted!" if @voters.include?[nick] | |
@answers[id].votes += 1 | |
end | |
def decide_winner | |
@answers.sort {|x,y| y.votes <=> x.votes}.first | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment