Advertisement
TTTPPP

EucherHands.py

Nov 6th, 2012
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. from random import *
  2.  
  3. def rank(x):
  4.     return x%6
  5. def colour(x):
  6.     return (x/12)
  7. def suit(x):
  8.     return (x/6)
  9.  
  10. def jack(x):
  11.     return (rank(x) == 2)
  12. def sameSuit(x,y):
  13.     return suit(x) == suit(y)
  14. def sameColour(x,y):
  15.     return colour(x) == colour(y)
  16.  
  17. def s1(H):
  18.     jacks = [0,0]
  19.     non = [0,0,0,0]
  20.     colours = [0,0]
  21.     for h in H:
  22.         if jack(h):
  23.             jacks[colour(h)]+=1
  24.         else:
  25.             non[suit(h)]+=1
  26.         colours[colour(h)]+=1
  27.     if set(jacks) == set([0,2]) and set(non) == set([0,3]) and set(colours) == set([0,5]):
  28.         return True
  29.     return False
  30.  
  31. def s2(H,D):
  32.     if s1(H):
  33.         return False
  34.     for h in H:
  35.         # Can we replace h with a card from D and get s1?
  36.         partialHand = H.difference(set([h]))
  37.         for d in D:
  38.             hand = partialHand.union(set([d]))
  39.             if s1(hand):
  40.                 return True
  41.     return False
  42.  
  43. def s3(H):
  44.     jacks = 0
  45.     for h in H:
  46.         if jack(h):
  47.             jacks += 1
  48.     if jacks == 4:
  49.         return True
  50.     return False
  51.  
  52. repeats = 1000000
  53. for z in range(10):
  54.     s1Count = 0
  55.     s2Count = 0
  56.     s3Count = 0
  57.     for j in range(repeats):
  58.         D = set(range(24))
  59.  
  60.         h = []
  61.         for i in range(4):
  62.             h.append(set(sample(D, 5)))
  63.             D.difference_update(h[-1])
  64.  
  65.         got1 = False
  66.         got2 = False
  67.         got3 = False
  68.         #All hands tested per deal
  69.         #for hand in h:
  70.         #One hand tested per deal
  71.         for hand in h[:1]:
  72.             got1 = got1 or s1(hand)
  73.             got2 = got2 or s2(hand, D)
  74.             got3 = got3 or s3(hand)
  75.         if got1:
  76.             s1Count += 1
  77.         if got2:
  78.             s2Count += 1
  79.         if got3:
  80.             s3Count += 1
  81.  
  82.     print s1Count*100.0/repeats, s2Count*100.0/repeats, s3Count*100.0/repeats
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement