SHARE
TWEET

Untitled

a guest Jun 5th, 2018 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. def gen_strategies(): #format of strategy is a 12 digit string, interpreted as each 4 digit string is for card 1,2,3.
  4.                       #First 2 digits are for first player, first and second decision, next 2 are for second player, decision in case of bet then in case of check by first player
  5.                       #1 is bet/call, 0 is fold. total of 1728 options.
  6.                       #remember that strings are 0-indexed
  7.                       #3 choices for player 1, 2 choices for player 2 for a total of 32 strategies
  8.                       #strategies are functions. They are called with 2 arguments, card, and state. State is a number from 1 to 4 corresponding to the 4 lines of the strings.
  9.                       #function should return 0 or 1 for the result.
  10.     cardstrats1a=['10','00'] #never call
  11.     cardstrats1b=['01','00'] #never call
  12.     cardstrats2a=['01','00'] #never bet
  13.     cardstrats2b=['10','00'] #never bet
  14.     #3cardstrats=['1010','1011','0110','0111'] #never fold
  15.     cardstrats3a=['10','01'] #never fold
  16.     cardstrats3b=['11'] #always bet as player 2 with 3
  17.     strategies=[]
  18.     for s1a in cardstrats1a:
  19.         for s1b in cardstrats1b:
  20.             for s2a in cardstrats2a:
  21.                 for s2b in cardstrats2b:
  22.                     for s3a in cardstrats3a:
  23.                         def s(card,state,a=s1a+s1b+s2a+s2b+s3a+cardstrats3b[0],explain=False):
  24.                             #print a
  25.                             if explain:
  26.                                 return a
  27.                             return (a)[card*4+state-5]=='1' #if card is 1, this ranges from 0-3, etc
  28.                         #strategies.append(s)
  29.     #randvalues=[0,.25,.5,.75,1]
  30.     randvalues=[0,.33,.5,.66,1]
  31.     def choose_rand(probability,choices,state):
  32.         x=random.uniform(0, 1)
  33.         if x<probability:
  34.             return choices[0][state]=='1'
  35.         return choices[1][state]=='1'
  36.    
  37.    
  38.        
  39.     for s1a in randvalues:
  40.         for s1b in randvalues:
  41.             for s2a in randvalues:
  42.                 for s2b in randvalues:
  43.                     for s3a in randvalues:
  44.                         def s(card,state,a=(s1a,s1b,s2a,s2b,s3a),explain=False):
  45.                             if explain:
  46.                                 return a
  47.                             if state<=2: #player 1
  48.                                 if card==1: #s1a
  49.                                     return choose_rand(a[0],cardstrats1a,state-1)
  50.                                 if card==2: #s2a
  51.                                     return choose_rand(a[2],cardstrats2a,state-1)
  52.                                 if card==3: #s3a
  53.                                     return choose_rand(a[4],cardstrats3a,state-1)
  54.                             else: #player 2
  55.                                 if card==1: #s1b
  56.                                     return choose_rand(a[1],cardstrats1b,state-3)
  57.                                 if card==2: #s2b
  58.                                     return choose_rand(a[3],cardstrats2b,state-3)
  59.                                 if card==3: #s3b
  60.                                     return True
  61.                         strategies.append(s)           
  62.     return strategies
  63.  
  64. debug=True
  65. debug=False
  66. def run_game(i,j):
  67.     a,b=0,0
  68.     #for game in range(0,3000):
  69.     for game in [(1,2),(1,3),(2,3),(3,1),(3,2),(2,1)]:
  70.         #acard=random.randint(1,3)
  71.         #bcard=random.randint(1,3)
  72.         #while (acard==bcard):
  73.         #   bcard=random.randint(1,3)
  74.         (acard,bcard)=game
  75.         if debug:
  76.             print 'strategies: first: ',i,' second: ',j
  77.             print 'cards ',acard,bcard
  78.         if i(acard,1): #first player bets
  79.             if j(bcard,3): #second player calls bet
  80.                 if acard>bcard: #a wins 2, b loses 2
  81.                     if debug:
  82.                         print "both bet, a wins 2, b loses 2"
  83.                     a+=2
  84.                     b-=2
  85.                     continue
  86.                 if bcard>acard: #b wins 2, a loses 2
  87.                     if debug:
  88.                         print "both bet, b wins 2, a loses 2"
  89.                     b+=2
  90.                     a-=2
  91.                     continue
  92.             else: #second player folds, a wins 1, b loses 1
  93.                 if debug:
  94.                     print "second player folds, a wins 1, b loses 1"
  95.                 a+=1
  96.                 b-=1
  97.                 continue
  98.         else: #first player checks
  99.             if j(bcard,4): #second player bets
  100.                 if i(acard,2): # first player calls
  101.                     if acard>bcard: #a wins 2, b loses 2
  102.                         if debug:
  103.                             print "both bet after a checks, a wins 2, b loses 2"
  104.                         a+=2
  105.                         b-=2
  106.                         continue
  107.                     if bcard>acard: #b wins 2, a loses 2
  108.                         if debug:
  109.                             print "both bet after a checks, b wins 2, a loses 2"
  110.                         b+=2
  111.                         a-=2
  112.                         continue
  113.                 else: #first player folds, b wins 1, a loses 1
  114.                     if debug:
  115.                         print "first player folds, b wins 1, a loses 1"
  116.                     b+=1
  117.                     a-=1
  118.             else: #second player checks
  119.                 if acard>bcard: #a wins 1, b loses 1
  120.                     if debug:
  121.                         print "both check, a wins 1, b loses 1"
  122.                     a+=1
  123.                     b-=1
  124.                     continue
  125.                 if bcard>acard: #b wins 1, a loses 1
  126.                     if debug:
  127.                         print "both check, a loses 1, b wins 1"
  128.                     b+=1
  129.                     a-=1
  130.                     continue
  131.                    
  132.     return (a,b)
  133. strategies=gen_strategies()
  134. #strategies=['000101100111', '000001101011','000101101011', '000001100111']
  135. results=[[s,0] for s in strategies]
  136. numstat=len(strategies)
  137. resultsarray=[[0 for i in range(0,numstat)]for j in range(0,numstat)]
  138. #print resultsarray
  139. #for i in results:
  140. #   print i
  141.  
  142.        
  143. for (num,i) in enumerate(results):
  144. #   print num
  145. #       continue
  146. #   print i
  147.     print i[0](1,1,explain=True)
  148.     #break
  149.     for (num2,j) in enumerate(results):
  150.         test=1459
  151.         test2=1724
  152.         #if num!=test and num2!=test: #for only testing certain strategies
  153.         #   continue
  154.         #if num+num2!=test+test2:
  155.         #   continue
  156.         for round in range(100):
  157.             (a,b)=run_game(i[0],j[0])
  158.             i[1]+=a
  159.             j[1]+=b
  160.             resultsarray[num][num2]+=a
  161.             resultsarray[num2][num]+=b
  162.  
  163. for i,line in zip(results,resultsarray):
  164.     if sum(line):
  165.         print i[0](1,1,explain=True),i[1],max(line),min(line),sum(line)/numstat
  166. #print i
  167. #   print sum(i)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top