def generateNextMoves(game_state): grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None) gridCollection = [] stepsAhead = 5 i = 0 gridQueue = Queue.Queue() future = nextThreeGrids(sg) gridQueue.put([future[0], ["up"]]) gridQueue.put([prune(future[1]), ["right"]]) gridQueue.put([future[2], ["left"]]) while (i < countFuture(stepsAhead) and not gridQueue.empty()): #print i #print len(gridCollection) newSgPair = gridQueue.get() i = i + 1 if (newSgPair[0] == False): continue gridCollection.append(newSgPair) upMoveList = newSgPair[1][:] rightMoveList = newSgPair[1][:] leftMoveList = newSgPair[1][:] upMoveList.append('up') rightMoveList.append('right') leftMoveList.append('left') future = nextThreeGrids(newSgPair[0]) gridQueue.put([prune(future[0]), upMoveList]) gridQueue.put([prune(future[1]), rightMoveList]) gridQueue.put([prune(future[2]), leftMoveList]) if (len(gridCollection) == 0): return ["down"] bestIndex = 0 bestScore = -1 i = 0 for sgPair in (gridCollection): currScore = evaluate(sgPair[0]) if (currScore > bestScore): bestScore = currScore bestIndex = i i = i + 1 print bestScore return gridCollection[bestIndex][1]
def generateNextMoves(game_state): grid = Tools.getGridState(game_state); sg = smartGrid(True, grid, None); gridCollection = []; stepsAhead = 5; i = 0; gridQueue = Queue.Queue(); future = nextThreeGrids(sg); gridQueue.put([future[0], ["up"]]); gridQueue.put([prune(future[1]), ["right"]]); gridQueue.put([future[2], ["left"]]); while (i < countFuture(stepsAhead) and not gridQueue.empty()): #print i #print len(gridCollection) newSgPair = gridQueue.get(); i = i + 1 if (newSgPair[0] == False): continue; gridCollection.append(newSgPair); upMoveList = newSgPair[1][:]; rightMoveList = newSgPair[1][:]; leftMoveList = newSgPair[1][:]; upMoveList.append('up'); rightMoveList.append('right'); leftMoveList.append('left'); future = nextThreeGrids(newSgPair[0]); gridQueue.put([prune(future[0]), upMoveList]); gridQueue.put([prune(future[1]), rightMoveList]); gridQueue.put([prune(future[2]), leftMoveList]); if (len(gridCollection) == 0): return ["down"]; bestIndex = 0; bestScore = -1; i = 0; for sgPair in (gridCollection): currScore = evaluate(sgPair[0]); if (currScore > bestScore): bestScore = currScore; bestIndex = i; i = i + 1; print bestScore return gridCollection[bestIndex][1];
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5; # create the smart grid of the base grid grid = Tools.getGridState(game_state); sg = smartGrid(True, grid, None); # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0); nodeCollection = getNodeCollection(stepsAhead, firstNode); if (not nodeCollection): downSg = sg.simulateMove("down"); downScore = evaluate(downSg, firstNode); downNode = sgNode(downSg, downScore, firstNode, "down", 1); nodeDq = deque(); nodeDq.append(downNode); return nodeDq; # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0]; for node in (nodeCollection): if (node.score > timeTraveler.score): timeTraveler = node; nodeDq = deque(); # trace node backwards to build moves while (True): if (timeTraveler.isBaseNode()): break; assert (not timeTraveler.stepToHere is None); assert (not timeTraveler.prevNode is None); # push sg onto deque nodeDq.appendleft(timeTraveler); timeTraveler = timeTraveler.prevNode; return nodeDq
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5 # create the smart grid of the base grid grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None) # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0) nodeCollection = getNodeCollection(stepsAhead, firstNode) if (not nodeCollection): downSg = sg.simulateMove("down") downScore = evaluate(downSg, firstNode) downNode = sgNode(downSg, downScore, firstNode, "down", 1) nodeDq = deque() nodeDq.append(downNode) return nodeDq # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0] for node in (nodeCollection): if (node.score > timeTraveler.score): timeTraveler = node nodeDq = deque() # trace node backwards to build moves while (True): if (timeTraveler.isBaseNode()): break assert (not timeTraveler.stepToHere is None) assert (not timeTraveler.prevNode is None) # push sg onto deque nodeDq.appendleft(timeTraveler) timeTraveler = timeTraveler.prevNode return nodeDq
def generateNextMove(game_state): grid = Tools.getGridState(game_state); sg = smartGrid(True, grid, np.zeros((4, 4), bool)); sgUp = sg.simulateMove("up"); sgRight = sg.simulateMove("right"); sgLeft = sg.simulateMove("left"); if (not Tools.staleMove(sg, sgUp) and sgUp.getHighestTile() == sgUp.intGrid[0, 0]): return "up"; elif (not Tools.staleMove(sg, sgLeft) and sgLeft.getHighestTile() == sgLeft.intGrid[0, 0]): return "left"; elif (not Tools.staleMove(sg, sgRight) and sgRight.getHighestTile() == sgRight.intGrid[0, 0]): return "right"; elif (not Tools.staleMove(sg, sgUp)): return "up"; elif (not Tools.staleMove(sg, sgLeft)): return "left"; elif (not Tools.staleMove(sg, sgRight)): return "right"; else: return "down";
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5 # create the smart grid of the base grid grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None) # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0) nodeCollection = getNodeCollection(stepsAhead, firstNode, pickyFilter) if (not nodeCollection): nodeCollection = getNodeCollection(stepsAhead, firstNode, easyFilter) if (not nodeCollection): return ['down'] # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0] for node in (nodeCollection): if (node.score > timeTraveler.score): timeTraveler = node moves = [] # trace node backwards to process moves while (True): if (timeTraveler.isBaseNode()): break assert (not timeTraveler.stepToHere is None) assert (not timeTraveler.prevNode is None) moves.append(timeTraveler.stepToHere) timeTraveler = timeTraveler.prevNode moves.reverse() return moves
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5 # create the smart grid of the base grid grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None) # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0) nodeCollection = getNodeCollection(stepsAhead, firstNode, pickyFilter) if not nodeCollection: nodeCollection = getNodeCollection(stepsAhead, firstNode, easyFilter) if not nodeCollection: return ["down"] # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0] for node in nodeCollection: if node.score > timeTraveler.score: timeTraveler = node moves = [] # trace node backwards to process moves while True: if timeTraveler.isBaseNode(): break assert not timeTraveler.stepToHere is None assert not timeTraveler.prevNode is None moves.append(timeTraveler.stepToHere) timeTraveler = timeTraveler.prevNode moves.reverse() return moves
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5; # create the smart grid of the base grid grid = Tools.getGridState(game_state); sg = smartGrid(True, grid, None); # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0); nodeCollection = getNodeCollection(stepsAhead, firstNode, pickyFilter); if (not nodeCollection): nodeCollection = getNodeCollection(stepsAhead, firstNode, easyFilter); if (not nodeCollection): return ['down'] # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0]; for node in (nodeCollection): if (node.score > timeTraveler.score): timeTraveler = node; moves = []; # trace node backwards to process moves while (True): if (timeTraveler.isBaseNode()): break; assert (not timeTraveler.stepToHere is None); assert (not timeTraveler.prevNode is None); moves.append(timeTraveler.stepToHere) timeTraveler = timeTraveler.prevNode; moves.reverse(); return moves
def generateNextMove(game_state): grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, np.zeros((4, 4), bool)) sgUp = sg.simulateMove("up") sgRight = sg.simulateMove("right") sgLeft = sg.simulateMove("left") if (not Tools.staleMove(sg, sgUp) and sgUp.getHighestTile() == sgUp.intGrid[0, 0]): return "up" elif (not Tools.staleMove(sg, sgLeft) and sgLeft.getHighestTile() == sgLeft.intGrid[0, 0]): return "left" elif (not Tools.staleMove(sg, sgRight) and sgRight.getHighestTile() == sgRight.intGrid[0, 0]): return "right" elif (not Tools.staleMove(sg, sgUp)): return "up" elif (not Tools.staleMove(sg, sgLeft)): return "left" elif (not Tools.staleMove(sg, sgRight)): return "right" else: return "down"
########################################## ##### Driver/Global Variables Setup ###### ########################################## print "hello, world"; driver = webdriver.Firefox(); driver.get("http://gabrielecirulli.github.io/2048/"); gridElement = driver.find_element_by_class_name('grid-container'); ########################################## ######## Start 2048 Manipulation ######### ########################################## print "start!"; game_state = Tools.getGameState(driver); grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None); print; highestTile = sg.getHighestTile(); # move up #Tools.move(gridElement, "up"); # move right #Tools.move(gridElement, "left"); moveC = 0; while(highestTile < 2048): moves = generateNextMoves(game_state); for move in (moves): print "move: " + str(moveC);
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5 # create the smart grid of the base grid grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None) # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0) nodeCollection = deque() dq = deque() # add first node dq.append(firstNode) while (dq): currentNode = dq.popleft() # base node case # don't add to collection # but add its future if (currentNode.isBaseNode()): pass # non-base node doesn't pass filter # don't add to collection # do't add its future to collection elif (not passFilter(currentNode)): continue # non-base node passes filter # add it to collection # add its future to collection else: nodeCollection.append(currentNode) # break if looking too far into future if (currentNode.moveCount > stepsAhead): break # create three more nodes (its future) # append them to deque # get current smart grid currentSg = currentNode.sg # get new move count newMoveCount = currentNode.moveCount + 1 # get future smart grids leftSg = currentSg.simulateMove("left") rightSg = currentSg.simulateMove("right") upSg = currentSg.simulateMove("up") # get future scores for smart grids leftScore = evaluate(leftSg) rightScore = evaluate(rightSg) upScore = evaluate(upSg) # create nodes leftNode = sgNode(leftSg, leftScore, currentNode, "left", newMoveCount) rightNode = sgNode(rightSg, rightScore, currentNode, "right", newMoveCount) upNode = sgNode(upSg, upScore, currentNode, "up", newMoveCount) # add them to queue dq.append(leftNode) dq.append(rightNode) dq.append(upNode) # no moves possible; move down if (not nodeCollection): return ["down"] # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0] for node in (nodeCollection): if (node.score > timeTraveler.score): timeTraveler = node moves = [] # trace node backwards to process moves while (True): if (timeTraveler.isBaseNode()): break assert (not timeTraveler.stepToHere is None) assert (not timeTraveler.prevNode is None) moves.append(timeTraveler.stepToHere) timeTraveler = timeTraveler.prevNode moves.reverse() return moves
def generateNextMove(game_state): grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, np.zeros((4, 4), bool)) sgUp = sg.simulateMove("up") sgRight = sg.simulateMove("right") sgLeft = sg.simulateMove("left") # no moves possible if (Tools.staleMove(sg, sgUp) and Tools.staleMove(sg, sgRight) and Tools.staleMove(sg, sgLeft)): return "down" # one move possible elif (Tools.staleMove(sg, sgUp) and Tools.staleMove(sg, sgRight)): return "left" # one move possible elif (Tools.staleMove(sg, sgUp) and Tools.staleMove(sg, sgLeft)): return "right" # one move possible elif (Tools.staleMove(sg, sgRight) and Tools.staleMove(sg, sgLeft)): return "up" # right and left moves possible elif (Tools.staleMove(sg, sgUp)): scoresRL = compareFutures(sgRight, sgLeft) if (scoresRL[0] > scoresRL[1]): return "right" # on tie, choose left else: return "left" # right and up moves possible elif (Tools.staleMove(sg, sgLeft)): scoresRU = compareFutures(sgRight, sgUp) if (scoresRU[0] > scoresRU[1]): return "right" # on tie, choose up else: return "up" # left and up moves possible elif (Tools.staleMove(sg, sgRight)): scoresLU = compareFutures(sgLeft, sgUp) if (scoresLU[0] > scoresLU[1]): return "left" # on tie, choose up else: return "up" # all three moves possible else: scoresUL = compareFutures(sgUp, sgLeft) # up is better than left if (scoresUL[0] > scoresUL[1]): #compare up and right scoresUR = compareFutures(sgUp, sgRight) # up is best if (scoresUR[0] > scoresUR[1]): return "up" # right is best else: return "right" # left is better than up else: # compare left and right scoresLR = compareFutures(sgLeft, sgRight) # left is best if (scoresLR[0] > scoresLR[1]): return "left" # right is best else: return "right"
def generateNextMoves(game_state): # how many steps to look ahead stepsAhead = 5; # create the smart grid of the base grid grid = Tools.getGridState(game_state); sg = smartGrid(True, grid, None); # use the base smart grid to create the first node firstNode = sgNode(sg, None, None, None, 0); nodeCollection = deque(); dq = deque(); # add first node dq.append(firstNode); while (dq): currentNode = dq.popleft(); # base node case # don't add to collection # but add its future if (currentNode.isBaseNode()): pass; # non-base node doesn't pass filter # don't add to collection # do't add its future to collection elif (not passFilter(currentNode)): continue; # non-base node passes filter # add it to collection # add its future to collection else: nodeCollection.append(currentNode); # break if looking too far into future if (currentNode.moveCount > stepsAhead): break; # create three more nodes (its future) # append them to deque # get current smart grid currentSg = currentNode.sg; # get new move count newMoveCount = currentNode.moveCount + 1; # get future smart grids leftSg = currentSg.simulateMove("left"); rightSg = currentSg.simulateMove("right"); upSg = currentSg.simulateMove("up"); # get future scores for smart grids leftScore = evaluate(leftSg); rightScore = evaluate(rightSg); upScore = evaluate(upSg); # create nodes leftNode = sgNode(leftSg, leftScore, currentNode, "left", newMoveCount); rightNode = sgNode(rightSg, rightScore, currentNode, "right", newMoveCount); upNode = sgNode(upSg, upScore, currentNode, "up", newMoveCount); # add them to queue dq.append(leftNode); dq.append(rightNode); dq.append(upNode); # no moves possible; move down if (not nodeCollection): return ["down"]; # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = nodeCollection[0]; for node in (nodeCollection): if (node.score > timeTraveler.score): timeTraveler = node; moves = []; # trace node backwards to process moves while (True): if (timeTraveler.isBaseNode()): break; assert (not timeTraveler.stepToHere is None); assert (not timeTraveler.prevNode is None); moves.append(timeTraveler.stepToHere) timeTraveler = timeTraveler.prevNode; moves.reverse(); return moves
def generateNextMoves(game_state, stepsAhead=5): # create the smart grid of the base grid grid = Tools.getGridState(game_state) sg = smartGrid(True, grid, None) # use the base smart grid to create the first node firstNode = sgNode(sg, 0, None, None, 0) frontier = deque() frontier.append(firstNode) newFrontier = deque() for i in xrange(0, stepsAhead + 1): # for each node in old frontier... while frontier: currentNode = frontier.popleft() # generate three new nodes from current node # get future smart grids leftSg = currentNode.sg.simulateMove("left") rightSg = currentNode.sg.simulateMove("right") upSg = currentNode.sg.simulateMove("up") # create nodes leftNode = sgNode(leftSg, currentNode.score + evaluate(leftSg), currentNode, "left", currentNode.moveCount + 1) rightNode = sgNode(rightSg, currentNode.score + evaluate(rightSg), currentNode, "right", currentNode.moveCount + 1) upNode = sgNode(upSg, currentNode.score + evaluate(upSg), currentNode, "up", currentNode.moveCount + 1) # add them to queue if (not isStaleMove(leftNode)): newFrontier.append(leftNode) if (not isStaleMove(rightNode)): newFrontier.append(rightNode) if (not isStaleMove(upNode)): newFrontier.append(upNode) # copy over new frontier to old frontier while newFrontier: frontier.append(newFrontier.popleft()) if (not frontier): downSg = sg.simulateMove("down") downScore = evaluate(downSg, firstNode) downNode = sgNode(downSg, downScore, firstNode, "down", 1) nodeDq = deque() nodeDq.append(downNode) return nodeDq # find best smart grid node # arbitrarily set to first node to begin search timeTraveler = frontier[0] for node in (frontier): if (node.score > timeTraveler.score): timeTraveler = node print timeTraveler.score nodeDq = deque() # ignore first one timeTraveler = timeTraveler.prevNode.prevNode # trace node backwards to build moves while (True): if (timeTraveler.isBaseNode()): break assert (not timeTraveler.stepToHere is None) assert (not timeTraveler.prevNode is None) # push sg onto deque nodeDq.appendleft(timeTraveler) timeTraveler = timeTraveler.prevNode return nodeDq
def generateNextMove(game_state): grid = Tools.getGridState(game_state); sg = smartGrid(True, grid, np.zeros((4, 4), bool)); sgUp = sg.simulateMove("up"); sgRight = sg.simulateMove("right"); sgLeft = sg.simulateMove("left"); # no moves possible if (Tools.staleMove(sg, sgUp) and Tools.staleMove(sg, sgRight) and Tools.staleMove(sg, sgLeft)): return "down"; # one move possible elif (Tools.staleMove(sg, sgUp) and Tools.staleMove(sg, sgRight)): return "left"; # one move possible elif (Tools.staleMove(sg, sgUp) and Tools.staleMove(sg, sgLeft)): return "right"; # one move possible elif (Tools.staleMove(sg, sgRight) and Tools.staleMove(sg, sgLeft)): return "up"; # right and left moves possible elif (Tools.staleMove(sg, sgUp)): scoresRL = compareFutures(sgRight, sgLeft); if (scoresRL[0] > scoresRL[1]): return "right"; # on tie, choose left else: return "left"; # right and up moves possible elif (Tools.staleMove(sg, sgLeft)): scoresRU = compareFutures(sgRight, sgUp); if (scoresRU[0] > scoresRU[1]): return "right"; # on tie, choose up else: return "up"; # left and up moves possible elif (Tools.staleMove(sg, sgRight)): scoresLU = compareFutures(sgLeft, sgUp); if (scoresLU[0] > scoresLU[1]): return "left"; # on tie, choose up else: return "up"; # all three moves possible else: scoresUL = compareFutures(sgUp, sgLeft); # up is better than left if (scoresUL[0] > scoresUL[1]): #compare up and right scoresUR = compareFutures(sgUp, sgRight); # up is best if (scoresUR[0] > scoresUR[1]): return "up"; # right is best else: return "right"; # left is better than up else: # compare left and right scoresLR = compareFutures(sgLeft, sgRight); # left is best if (scoresLR[0] > scoresLR[1]): return "left"; # right is best else: return "right";