Guest User

Prisoner's Dilema

a guest
Feb 23rd, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.46 KB | None | 0 0
  1. # Solution to prisoner's dilema
  2. # http://www.cartalk.com/content/prison-switcharoo-0?question
  3. # https://news.ycombinator.com/item?id=7286279
  4.  
  5.  
  6. $TOTAL_NUM_PRISONERS = 23
  7.  
  8. $switch_a = rand() > 0.5
  9. $switch_b = rand() > 0.5
  10.  
  11. $prisoners_who_have_visited = [false] * $TOTAL_NUM_PRISONERS
  12. $cycles = 0
  13.  
  14. def we_have_all_visited()
  15.     if $prisoners_who_have_visited.any?{|pr| pr==false}
  16.         puts "\nYou will all be fed to the aligators now... (after #{$cycles} visits)"
  17.     else
  18.         puts "\nCongratualtions, you may all go free (took #{$cycles} visits)"
  19.     end
  20.  
  21.     exit
  22. end
  23.  
  24.  
  25. class Prisoner
  26.  
  27.     def initialize(name)
  28.         @name = name
  29.         @i_have_previously_touched_a = false
  30.     end
  31.  
  32.     def name
  33.         return @name
  34.     end
  35.  
  36.     def i_have_previously_touched_a
  37.         return @i_have_previously_touched_a
  38.     end
  39.  
  40.     def inspect
  41.         return "Prisoner<#{@name}, #{@i_have_previously_touched_a}>"
  42.     end
  43.  
  44.     def to_s
  45.         return inspect
  46.     end
  47.  
  48.     def switch_time(a_state, b_state)
  49.         if a_state == false and !@i_have_previously_touched_a then
  50.             @i_have_previously_touched_a = true
  51.             return 'A'
  52.         else
  53.             return 'B'
  54.         end
  55.     end
  56.  
  57. end
  58.  
  59.  
  60. class Watchkeeper < Prisoner
  61.  
  62.     def initialize
  63.         @name = "Watchkeeper"
  64.         @i_have_previously_touched_a = false
  65.         @num_prisoners_touched_a = 0
  66.     end
  67.  
  68.     def switch_time(a_state, b_state)
  69.         if a_state == true
  70.             @num_prisoners_touched_a += 1
  71.             @i_have_previously_touched_a = true
  72.  
  73.             puts "Number of people who have switched 'A' is #{@num_prisoners_touched_a}"
  74.  
  75.             if @num_prisoners_touched_a == ($TOTAL_NUM_PRISONERS-1)
  76.                 puts "I declare we have all visited this room!"
  77.                 we_have_all_visited()
  78.             end
  79.  
  80.             return 'A'
  81.         else
  82.             return 'B'
  83.         end
  84.     end
  85. end
  86.  
  87. # Throw prisoners in jail
  88. prisoners = Array.new
  89. for i in 1 .. $TOTAL_NUM_PRISONERS-1
  90.     prisoners.push(Prisoner.new("Prisoner #{i}"))
  91. end
  92. prisoners.push(Watchkeeper.new)
  93.  
  94.  
  95. puts "Welcome to the prisoner's dilema"
  96. puts "================================\n\n"
  97.  
  98. while true
  99.     next_prisoner = rand(0 .. $TOTAL_NUM_PRISONERS-1)
  100.     pr = prisoners[next_prisoner]
  101.  
  102.     puts "Current state of the switches: A=#{$switch_a}, B=#{$switch_b}"
  103.     puts "#{pr.name}, you're up!"
  104.     puts "#{pr.name}, have you previously touched A? #{pr.i_have_previously_touched_a}"
  105.  
  106.     choice = pr.switch_time($switch_a, $switch_b)
  107.     puts "#{pr.name} chose #{choice}\n\n"
  108.  
  109.     if choice == 'A'
  110.         $switch_a = !$switch_a
  111.     else
  112.         $switch_b = !$switch_b
  113.     end
  114.  
  115.     $prisoners_who_have_visited[next_prisoner] = true
  116.  
  117.     $cycles += 1
  118.  
  119.     #gets
  120. end
Advertisement
Add Comment
Please, Sign In to add comment