Refactored and randomized picking order.

This commit is contained in:
James 2024-11-07 14:15:18 -06:00
parent c89cb15393
commit ca8ffc44b5

View file

@ -1,22 +1,40 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
names = [ 'Allen', 'Vickie', 'Matt', 'Lisa', 'James', 'Pich', 'Kristen', 'Jason', 'Markie' ] adults = [ 'Allen', 'Vickie', 'Matt', 'Lisa', 'James', 'Pich', 'Kristen', 'Jason', 'Markie' ]
kids = [ 'Malachi', 'Isaiah', 'Samara', 'Ethan', 'Kennedi', 'Kasen', 'Griffin', 'Millie', 'Otto' ]
names = adults
hat = [] class Participant
attr_accessor :name
attr_accessor :buys_for
def initialize name
@name = name
end
names.each do |name|
hat.push name
end end
buys_for = {} participants = Array.new
names.each { |name| participants.push Participant.new(name) }
names.each do |name| # put everyone's name in the hat
while buys_for[name] == nil hat = Array.new
pick = hat[rand(hat.length)] participants.each { |participant| hat.push participant }
puts "#{name} picks #{pick}"
unless pick == name # randomize the order names are picked
buys_for[name] = pick pick_order = participants.shuffle
# pick names from hat, making sure they don't pick themself
pick_order.each do |participant|
while participant.buys_for == nil
pick = hat[rand(hat.length)]
puts "#{participant.name} picks #{pick.name}"
unless pick == participant
participant.buys_for = pick
hat.delete(pick) hat.delete(pick)
end end
end end
end end