def __init__(self): AbstractBoard.__init__(self) BacktrackingSolvable.__init__(self) self._piecedict = { } self._minx = 0 self._maxx = 0 self._miny = 0 self._maxy = 0
def randomGreedyGame(n): A = AbstractBoard(4) A.populateRandom(2) isGame = True moves = 0 score = 0 while isGame: M = [] msc = -1 A_ = A.deepcopy() for m in MOVES: hasmoved,scp,_ = A.shift(m) if hasmoved: A_ = A.deepcopy() if scp>msc: msc = scp M = [(scp,m)] elif scp==msc: M.append((scp,m)) scp,m = choice(M) score+=scp A.shift(m) isGame = A.populateRandom(1) moves+=1 return A,moves,score
def randomCycleGame(n): A = AbstractBoard(4) A.populateRandom(2) isGame = True moves = 0 score = 0 for m in cycle([(0,1),(-1,0),(0,-1),(1,0)]): hasmoved,scp,_ = A.shift(m) if hasmoved: score+=scp moves+=1 isGame = A.populateRandom(1) if not isGame:break return A,moves,score
def randomcyclePreferedMoves(F): A = AbstractBoard(4) A.populateRandom(2) isGame = True moves = 0 score = 0 c = 0 for m in cycle(F): if c==len(F): m = choice([i for i in MOVES if i not in F]) c+=1 hasmoved,scp,_ = A.shift(m) if hasmoved: score+=scp moves+=1 isGame = A.populateRandom(1) if not isGame:break c=0 return A,moves,score
def randomPreferMoves(F): A = AbstractBoard(4) A.populateRandom(2) isGame = True moves = 0 score = 0 tested = set() while True: if len(tested)==len(F): m = choice([i for i in MOVES if i not in F]) tested = set() else: m = choice(F) hasmoved,scp,_ = A.shift(m) if hasmoved: score+=scp moves+=1 isGame = A.populateRandom(1) if not isGame:break else: tested.add(m) return A,moves,score
def randomGame(n): A = AbstractBoard(4) A.populateRandom(2) isGame = True moves = 0 score = 0 while True: MOVE_RED = MOVES[:] while True: m = choice(MOVE_RED) hasmoved,scp,_ = A.shift(m) score+=scp if hasmoved: break MOVE_RED.remove(m) if not MOVE_RED: return A,moves,score A.populateRandom(1) moves+=1
isGame = A.populateRandom(1) moves+=1 return A,moves,score def randomBiasGame(n,(p1,p2,p3,p4)): assert p1+p2+p3+p4==1 def iterateBiasedMoves(): while True: x = random() if x<p1:yield MOVES[0] if x<p1+p2:yield MOVES[1] if x<p1+p2+p3:yield MOVES[2] yield MOVES[3] A = AbstractBoard(4) A.populateRandom(2) isGame = True moves = 0 score = 0 for m in iterateBiasedMoves(): hasmoved,scp,_ = A.shift(m) if hasmoved: score+=scp moves+=1 isGame = A.populateRandom(1) if not isGame:break return A,moves,score def randomPreferMoves(F): A = AbstractBoard(4)
def __init__(self, gridlen): AbstractBoard.__init__(self) BacktrackingSolvable.__init__(self) self._piecedict = collections.defaultdict(lambda: collections.defaultdict(dict)) self._piececnt = 0 self._gridlen = gridlen