-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty_hall_2012-08-22.rb
executable file
·65 lines (51 loc) · 1.25 KB
/
monty_hall_2012-08-22.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
class Player
attr_accessor :chosen_door
def initialize(doors)
@chosen_door = choose_door(doors)
end
def choose_door(doors)
doors[ rand(0..doors.length-1) ]
end
def win?
@chosen_door == :car
end
end
class Steadfast < Player
def make_deal(door)
# do nothing: this player always sticks with his/her initial choice
end
end
class Fickle < Player
def make_deal(door)
# this player always chooses the remaining door
@chosen_door = door
end
end
class Game
attr_accessor :doors, :player
def initialize(player_type)
@doors = [:goat, :goat, :car].shuffle
@player = player_type.new(@doors)
end
def lets_make_a_deal
@doors.delete_at( @doors.index(@player.chosen_door) )
@doors.delete_at( @doors.index(:goat) )
@player.make_deal(@doors[0])
@player.win?
end
end
if __FILE__ == $0
simulations = (ARGV[0] ||= 1000).to_i
[Steadfast, Fickle].each do |player|
wins = 0
(1..simulations).each do |sim|
game = Game.new(player)
result = game.lets_make_a_deal
wins += 1 if result
end
pct = "%4.2f" % ((Float(wins) / Float(simulations)) * 100)
print "The #{player} player won #{wins} out of #{simulations} times "
puts "(#{pct}%)."
end
end