Advertisement
Guest User

Forge/Hoard Probability

a guest
Aug 5th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.85 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. class Card
  4.     attr_reader :name
  5.    
  6.     def initialize(name)
  7.         @name = name
  8.     end
  9.    
  10.     def to_s()
  11.         "<Card #{@name}>"
  12.     end
  13. end
  14.  
  15. class Deck
  16.     def initialize()
  17.         @cards = Array.new
  18.     end
  19.    
  20.     def add(card)
  21.         @cards << card
  22.     end
  23.    
  24.     def draw_random_five()
  25.         return @cards.shuffle()[0..4]
  26.     end
  27. end
  28.  
  29. myDeck = Deck.new()
  30. 3.times { myDeck.add(Card.new("Forge")) }
  31. 3.times { myDeck.add(Card.new("Duchy")) }
  32. 3.times { myDeck.add(Card.new("Gold")) }
  33. 6.times { myDeck.add(Card.new("Other")) }
  34.  
  35. matches = 0
  36. iterations = 1000000
  37. iterations.times do
  38.     hand = myDeck.draw_random_five()
  39.     num_forge = hand.count { |c| c.name == "Forge" }
  40.     num_duchy = hand.count { |c| c.name == "Duchy" }
  41.     num_gold = hand.count { |c| c.name == "Gold" }
  42.     matches += 1 if num_forge>0 and num_duchy>0 and num_gold>0
  43. end
  44. puts "#{100*matches.to_f/iterations.to_f}%"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement