Class: MontyHallProblem::Playground
- Inherits:
-
Object
- Object
- MontyHallProblem::Playground
- Defined in:
- lib/monty_hall_problem.rb
Instance Attribute Summary collapse
-
#times ⇒ Object
Returns the value of attribute times.
Instance Method Summary collapse
-
#initialize(times, door_size: 3, car_size: 1, open_size: 1) ⇒ Playground
constructor
A new instance of Playground.
- #play(mode) ⇒ Object
- #play_change ⇒ Object
- #play_no_change ⇒ Object
Constructor Details
#initialize(times, door_size: 3, car_size: 1, open_size: 1) ⇒ Playground
Returns a new instance of Playground.
78 79 80 81 82 83 |
# File 'lib/monty_hall_problem.rb', line 78 def initialize(times, door_size: 3, car_size: 1, open_size: 1) self.times = times @door_size = door_size @car_size = car_size @open_size = open_size end |
Instance Attribute Details
#times ⇒ Object
Returns the value of attribute times.
76 77 78 |
# File 'lib/monty_hall_problem.rb', line 76 def times @times end |
Instance Method Details
#play(mode) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/monty_hall_problem.rb', line 108 def play(mode) result = self.times.times.map do mode == :change ? play_change : play_no_change end win_count = result.count { |e| e == :win } lose_count = result.count { |e| e == :lose } puts "#{mode} - all:#{self.times}; win:#{win_count}; lose:#{lose_count}; P(win)=#{(win_count.to_f/self.times.to_f * 100).round(2)}%" self end |
#play_change ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/monty_hall_problem.rb', line 85 def play_change doors = Doors.new(@door_size) host = Host.new(doors, @car_size) player = Player.new(doors) host.put_cars idx = player.choose_door host.open_wronth_door(idx, @open_size) idx = player.change_door(idx) host.make_known(idx) ? :win : :lose end |
#play_no_change ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/monty_hall_problem.rb', line 97 def play_no_change doors = Doors.new(@door_size) host = Host.new(doors, @car_size) player = Player.new(doors) host.put_cars idx = player.choose_door host.open_wronth_door(idx, @open_size) host.make_known(idx) ? :win : :lose end |