Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def gen_strategies(): #format of strategy is a 12 digit string, interpreted as each 4 digit string is for card 1,2,3.
- #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
- #1 is bet/call, 0 is fold. total of 1728 options.
- #remember that strings are 0-indexed
- #3 choices for player 1, 2 choices for player 2 for a total of 32 strategies
- #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.
- #function should return 0 or 1 for the result.
- cardstrats1a=['10','00'] #never call
- cardstrats1b=['01','00'] #never call
- cardstrats2a=['01','00'] #never bet
- cardstrats2b=['10','00'] #never bet
- #3cardstrats=['1010','1011','0110','0111'] #never fold
- cardstrats3a=['10','01'] #never fold
- cardstrats3b=['11'] #always bet as player 2 with 3
- strategies=[]
- for s1a in cardstrats1a:
- for s1b in cardstrats1b:
- for s2a in cardstrats2a:
- for s2b in cardstrats2b:
- for s3a in cardstrats3a:
- def s(card,state,a=s1a+s1b+s2a+s2b+s3a+cardstrats3b[0],explain=False):
- #print a
- if explain:
- return a
- return (a)[card*4+state-5]=='1' #if card is 1, this ranges from 0-3, etc
- #strategies.append(s)
- #randvalues=[0,.25,.5,.75,1]
- randvalues=[0,.33,.5,.66,1]
- def choose_rand(probability,choices,state):
- x=random.uniform(0, 1)
- if x<probability:
- return choices[0][state]=='1'
- return choices[1][state]=='1'
- for s1a in randvalues:
- for s1b in randvalues:
- for s2a in randvalues:
- for s2b in randvalues:
- for s3a in randvalues:
- def s(card,state,a=(s1a,s1b,s2a,s2b,s3a),explain=False):
- if explain:
- return a
- if state<=2: #player 1
- if card==1: #s1a
- return choose_rand(a[0],cardstrats1a,state-1)
- if card==2: #s2a
- return choose_rand(a[2],cardstrats2a,state-1)
- if card==3: #s3a
- return choose_rand(a[4],cardstrats3a,state-1)
- else: #player 2
- if card==1: #s1b
- return choose_rand(a[1],cardstrats1b,state-3)
- if card==2: #s2b
- return choose_rand(a[3],cardstrats2b,state-3)
- if card==3: #s3b
- return True
- strategies.append(s)
- return strategies
- debug=True
- debug=False
- def run_game(i,j):
- a,b=0,0
- #for game in range(0,3000):
- for game in [(1,2),(1,3),(2,3),(3,1),(3,2),(2,1)]:
- #acard=random.randint(1,3)
- #bcard=random.randint(1,3)
- #while (acard==bcard):
- # bcard=random.randint(1,3)
- (acard,bcard)=game
- if debug:
- print 'strategies: first: ',i,' second: ',j
- print 'cards ',acard,bcard
- if i(acard,1): #first player bets
- if j(bcard,3): #second player calls bet
- if acard>bcard: #a wins 2, b loses 2
- if debug:
- print "both bet, a wins 2, b loses 2"
- a+=2
- b-=2
- continue
- if bcard>acard: #b wins 2, a loses 2
- if debug:
- print "both bet, b wins 2, a loses 2"
- b+=2
- a-=2
- continue
- else: #second player folds, a wins 1, b loses 1
- if debug:
- print "second player folds, a wins 1, b loses 1"
- a+=1
- b-=1
- continue
- else: #first player checks
- if j(bcard,4): #second player bets
- if i(acard,2): # first player calls
- if acard>bcard: #a wins 2, b loses 2
- if debug:
- print "both bet after a checks, a wins 2, b loses 2"
- a+=2
- b-=2
- continue
- if bcard>acard: #b wins 2, a loses 2
- if debug:
- print "both bet after a checks, b wins 2, a loses 2"
- b+=2
- a-=2
- continue
- else: #first player folds, b wins 1, a loses 1
- if debug:
- print "first player folds, b wins 1, a loses 1"
- b+=1
- a-=1
- else: #second player checks
- if acard>bcard: #a wins 1, b loses 1
- if debug:
- print "both check, a wins 1, b loses 1"
- a+=1
- b-=1
- continue
- if bcard>acard: #b wins 1, a loses 1
- if debug:
- print "both check, a loses 1, b wins 1"
- b+=1
- a-=1
- continue
- return (a,b)
- strategies=gen_strategies()
- #strategies=['000101100111', '000001101011','000101101011', '000001100111']
- results=[[s,0] for s in strategies]
- numstat=len(strategies)
- resultsarray=[[0 for i in range(0,numstat)]for j in range(0,numstat)]
- #print resultsarray
- #for i in results:
- # print i
- for (num,i) in enumerate(results):
- # print num
- # continue
- # print i
- print i[0](1,1,explain=True)
- #break
- for (num2,j) in enumerate(results):
- test=1459
- test2=1724
- #if num!=test and num2!=test: #for only testing certain strategies
- # continue
- #if num+num2!=test+test2:
- # continue
- for round in range(100):
- (a,b)=run_game(i[0],j[0])
- i[1]+=a
- j[1]+=b
- resultsarray[num][num2]+=a
- resultsarray[num2][num]+=b
- for i,line in zip(results,resultsarray):
- if sum(line):
- print i[0](1,1,explain=True),i[1],max(line),min(line),sum(line)/numstat
- #print i
- # print sum(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement