Skip to content

Instantly share code, notes, and snippets.

@cvonkleist
Created October 12, 2009 02:51
Show Gist options
  • Save cvonkleist/208084 to your computer and use it in GitHub Desktop.
Save cvonkleist/208084 to your computer and use it in GitHub Desktop.
goats
#!/usr/bin/env ruby
class Array
def random; self[rand(self.length)]; end
end
# extended by actual player classes
class Player
attr_reader :name
def initialize(name)
@name = name
end
def happy_with_prize?
@prize != :goat
end
def say(text)
STDOUT.puts "Plyr: " + text
end
def receive(prize)
@prize = prize
end
def pick_first_door(doors)
@first_pick = doors.random
end
end
class SwitchPlayer < Player
def pick_first_door(door_count)
door = super
say "Gee, Bob... I guess mah first pick's gotta be door %d" % door
door
end
# always switch
def pick_final_door(doors_remaining)
say "Welp, Bob, mah paw told me I oughtta what with the switchin'."
(doors_remaining - [@first_pick]).first
end
end
class StayPlayer < Player
def pick_first_door(door_count)
door = super
say "AHM A GONNA PICK DOOR %s" % (%w(ZERUH WUN TEW)[door] || door)
door
end
# always stay
def pick_final_door(doors_remaining)
say "AH AIN'T GONNA SWITCH, BOBBY"
@first_pick
end
end
class Host
def initialize(*args)
@door_with_prize, @player, @door_count = *args
end
def run_show!
say "Welcome to THE PRICE ARE GOAT!"
say "Pick a door, %s." % @player.name
first_pick = @player.pick_first_door(doors)
say "Great choice, %s. You picked door #%d" % [@player.name, first_pick]
say "Time for the reveal!"
doors_to_open = (doors - [first_pick] - [@door_with_prize])
doors_to_open -= [doors_to_open.random] if first_pick == @door_with_prize
say "Vanna, please open: " + (doors_to_open * ', ')
say "It's %s!" % [doors_to_open.length == 1 ? 'a goat' : '%d goats' % doors_to_open.length]
say "Now, do you want to keep your choice, or will you switch?"
final_pick = @player.pick_final_door(doors - doors_to_open)
say "Okay, %s! Let's see if you're a thousandthousandaire!" % @player.name
say "Vanna, please open door #%d" % final_pick
if final_pick == @door_with_prize
@player.receive(:million_male_deer)
say "DAS GELD! A WINRAR IS U!"
else
@player.receive(:goat)
say "FAIL! Here's your goat, %s! Enjoy." % @player.name
end
end
def doors
(0...@door_count).to_a
end
def say(text)
STDOUT.puts "Host: " + text
end
end
def win_ratio(won_games, lost_games)
total_games = won_games + lost_games
'%d%% games won' % (won_games.to_f / total_games * 100)
end
DOOR_COUNT = 3
GAME_COUNT = 1000
win_counts = {}
{'Cletus' => SwitchPlayer, 'Mildred' => StayPlayer}.each do |name, klass|
win_counts[name] = 0
GAME_COUNT.times do
player = klass.new(name)
Host.new(rand(DOOR_COUNT), player, DOOR_COUNT).run_show!
win_counts[name] += 1 if player.happy_with_prize?
puts
end
end
puts "Summary:\n"
win_counts.each do |name, count|
puts " " + name
puts " %s%% of games won\n" % (100.0 * count / GAME_COUNT)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment