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 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 getNodeCollection(stepsAhead, firstNode, nodeFilter): assert (type(stepsAhead) is int) assert (stepsAhead >= 1) assert (isinstance(firstNode, sgNode)) 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 # don't add its future to collection elif (not nodeFilter(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, currentNode) rightScore = evaluate(rightSg, currentNode) upScore = evaluate(upSg, currentNode) # 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) return nodeCollection
def getNodeCollection(stepsAhead, firstNode, nodeFilter): assert type(stepsAhead) is int assert stepsAhead >= 1 assert isinstance(firstNode, sgNode) 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 # don't add its future to collection elif not nodeFilter(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, currentNode) rightScore = evaluate(rightSg, currentNode) upScore = evaluate(upSg, currentNode) # 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) return nodeCollection
def getNodeCollection(stepsAhead, firstNode): assert (type(stepsAhead) is int); assert (stepsAhead >= 1); assert (isinstance(firstNode, sgNode)); 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 # don't add its future to collection elif (not filterNode(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, currentNode); rightScore = evaluate(rightSg, currentNode); upScore = evaluate(upSg, currentNode); # 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); return nodeCollection;
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): # 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