Created
January 22, 2014 15:47
-
-
Save wtnb75/8561105 to your computer and use it in GitHub Desktop.
babanuki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# encoding: UTF-8 | |
import os,sys | |
import random | |
class babanuki: | |
allcards="A234567890JQK"*4+"X" | |
normcard="A234567890JQK" | |
cardtypes=normcard+"X" | |
def __init__(self): | |
pass | |
def do_one(self, members): | |
moves,xmoves=0,0 | |
inhand,wins=[],[] | |
iuid=-1 | |
uids=list(range(members)) | |
c=list(self.allcards) | |
random.shuffle(c) | |
sl=len(c)/members | |
for i in range(members): | |
inhand.append("".join(c[i*sl:(i+1)*sl])) | |
for i in range(members*sl, len(c)): | |
inhand[i%members]+=c[i] | |
print "#0", inhand | |
for i,x in enumerate(inhand): | |
r=[] | |
for k in self.cardtypes: | |
kn=x.count(k) | |
if kn%2!=0: | |
if k=="X": iuid=i | |
r.append(k) | |
inhand[i]="".join(r) | |
if iuid: inhand[0],inhand[iuid]=inhand[iuid],inhand[0] | |
iuid=0 | |
kv=list(enumerate(inhand)) | |
kv.reverse() | |
for i,x in kv: | |
if len(x)==0: | |
wins.append(uids[i]) | |
del inhand[i] | |
del uids[i] | |
initials=map(len, inhand) | |
print "#1", inhand, initials | |
turn=0 | |
while len(inhand)!=1: | |
moves+=1 | |
nextturn=(turn+1)%len(inhand) | |
c=random.choice(inhand[nextturn]) | |
inhand[nextturn]="".join(inhand[nextturn].split(c)) | |
if c=="X": xmoves+=1 | |
if len(inhand[nextturn])==0: | |
wins.append(uids[nextturn]) | |
del inhand[nextturn] | |
del uids[nextturn] | |
if nextturn==0: | |
turn-=1 | |
nextturn=(turn+1)%len(inhand) | |
if c in inhand[turn]: | |
inhand[turn]="".join(inhand[turn].split(c)) | |
if len(inhand[turn])==0: | |
wins.append(uids[turn]) | |
del inhand[turn] | |
del uids[turn] | |
nextturn=turn%len(inhand) | |
else: | |
inhand[turn]+=c | |
print "pick",turn,c,nextturn,inhand | |
turn=nextturn | |
return moves,xmoves,wins,uids[0],iuid,initials | |
b=babanuki() | |
for i in range(1000): | |
r=b.do_one(3) | |
print "result", r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment