def stepBack(name, stack): while len(stack) != 0: # go backward nextDirection = getNextDirection_Backward(stack.pop()) # step backward print updatewumpusNowWithRocks.take_action(name, nextDirection) print updatewumpusNowWithRocks.take_action(name, "Step")
def go_back_home(world, home, percepts, order_to_visit): location = percepts[5] adjacents = updatewumpusNowWithRocks.look_ahead(world) for cell in order_to_visit[order_to_visit.index(home):]: if cell in adjacents: updatewumpusNowWithRocks.take_action(world, face_to(cell, percepts)) percepts = updatewumpusNowWithRocks.take_action(world, 'Step') return percepts
def tryToKillWumpus(name, smellFlag, adjacentCell, solidList, notSolidList, currentCell): if smellFlag == 'nasty': for cell in adjacentCell: if cell not in solidList and cell not in notSolidList: shootDirection = getNextDirection(currentCell, cell) print updatewumpusNowWithRocks.take_action( name, shootDirection) print updatewumpusNowWithRocks.take_action(name, 'Shoot') return cell
def killWumpusProcess(name, maybeWumpusList, solidList, notSolidList, stack): # find Wumpus pos wumpus_pos = Counter(maybeWumpusList).most_common() if len(wumpus_pos) == 0: return 'TBD' print wumpus_pos wumpus_pos = wumpus_pos[0][0] # initialize lists maybePitList = [] maybeWumpusList = [] visited = [] res = ''; # find wumpus pos while True: adjacentCell = updatewumpusNowWithRocks.look_ahead(name) [smellFlag, airFlag, goldFlag, currentCell, livingFlag] = libWumpusWorld.getCurrentState(name) # add currentCell to visited List visited.append(currentCell) if currentCell not in solidList: solidList.append(currentCell) # check stop sign if wumpus_pos in adjacentCell: # find wumpus, kill it shootDirection = libWumpusWorld.getNextDirection(currentCell, wumpus_pos) print updatewumpusNowWithRocks.take_action(name, shootDirection) print updatewumpusNowWithRocks.take_action(name, 'Shoot') shootedState = updatewumpusNowWithRocks.take_action(name, "Left") if shootedState[8] == 100 or shootedState[8] == 1100: res = wumpus_pos break else: res = 'NotSucceeded' break # add facts to current list libWumpusWorld.addFactsProcess(adjacentCell, smellFlag, airFlag, maybeWumpusList, maybePitList, solidList, notSolidList) # next step flag = libWumpusWorld.nextStep(name, adjacentCell, visited, solidList, notSolidList, currentCell, stack) if flag: res = 'NotFound' # after kill step back libWumpusWorld.stepBack(name, stack) return res
def pitFindProcess(name, PitList, solidList, notSolidList, stack): # initialize lists maybePitList = [] maybeWumpusList = [] visited = [] # return value resSolidList = [] resNotSolidList = [] # find pit pos while True: adjacentCell = updatewumpusNowWithRocks.look_ahead(name) [smellFlag, airFlag, goldFlag, currentCell, livingFlag] = libWumpusWorld.getCurrentState(name) # add currentCell to visited List visited.append(currentCell) if currentCell not in solidList: solidList.append(currentCell) # add facts to current list libWumpusWorld.addFactsProcess(adjacentCell, smellFlag, airFlag, maybeWumpusList, maybePitList, solidList, notSolidList) # add value to res Lists for cell in adjacentCell: if cell in PitList: tossDirection = libWumpusWorld.getNextDirection(currentCell, cell) print updatewumpusNowWithRocks.take_action(name, tossDirection) sign = updatewumpusNowWithRocks.take_action(name, 'Toss') if sign == 'Quiet': resNotSolidList.append(cell) elif sign == 'Clink': resSolidList.append(cell) else: break PitList = filter(lambda a: a != cell, PitList) # check stop sign if len(PitList) == 0: break # next step flag = libWumpusWorld.nextStep(name, adjacentCell, visited, solidList, notSolidList, currentCell, stack) if flag: break # after toss step back libWumpusWorld.stepBack(name, stack) return [resSolidList, resNotSolidList]
def getCurrentState(name): currentState = updatewumpusNowWithRocks.take_action(name, "Left") smellFlag = currentState[0] airFlag = currentState[1] goldFlag = currentState[2] currentCell = currentState[5] livingFlag = currentState[7] return [smellFlag, airFlag, goldFlag, currentCell, livingFlag]
def pickUpGold(name, stack): currentState = updatewumpusNowWithRocks.take_action(name, "Left") goldFlag = currentState[2] if goldFlag == "glitter": # gold finded and back to start pos print updatewumpusNowWithRocks.take_action(name, 'PickUp') while len(stack) != 0: # go backward nextDirection = getNextDirection_Backward(stack.pop()) # step backward print updatewumpusNowWithRocks.take_action(name, nextDirection) print updatewumpusNowWithRocks.take_action(name, "Step")
def tryToDie(name): visited = [] stack = [] # find gold pos while True: adjacentCell = updatewumpusNowWithRocks.look_ahead(name) currentState = updatewumpusNowWithRocks.take_action(name, "Left") if currentState == None: break currentCell = currentState[5] # add currentCell to visited List visited.append(currentCell) # next step flag = libWumpusWorld.nextStep(name, adjacentCell, visited, adjacentCell, [], currentCell, stack) if flag: break # after toss step back libWumpusWorld.stepBack(name, stack)
def nextStep(name, adjacentCell, visited, solidList, notSolidList, currentCell, stack): forwardFlag = False for cell in adjacentCell: if (cell in visited) or (cell not in solidList) or (cell in notSolidList): continue forwardFlag = True nextDirection = getNextDirection(currentCell, cell) # add nextDirection to stack stack.append(nextDirection) # step forward print updatewumpusNowWithRocks.take_action(name, nextDirection) print updatewumpusNowWithRocks.take_action(name, "Step") break if len(stack) == 0: return True if not forwardFlag: # if there is no solid cell near, go backward and the current pos is not the start pos nextDirection = getNextDirection_Backward(stack.pop()) # step backward print updatewumpusNowWithRocks.take_action(name, nextDirection) print updatewumpusNowWithRocks.take_action(name, "Step")
def find_gold(world, percepts, facts, rules, order_to_visit, trace): location = percepts[5] if location in order_to_visit: order_to_visit.remove(location) order_to_visit.append(location) trace.append(location) # add facts and run inferences add_percepts(world, percepts, facts) inference_engine(rules, facts) adjacents = updatewumpusNowWithRocks.look_ahead(world) if ['try_rock', location] in facts: print "trying rock" for cell in adjacents: if ['pit', cell] not in facts and ['safe', cell] not in facts: print "found a guess" updatewumpusNowWithRocks.take_action(world, face_to(cell, percepts)) sound = updatewumpusNowWithRocks.take_action(world, 'Toss') if sound == 'Quiet': insert_fact(['pit', cell], facts) if sound == 'Clink': insert_fact(['solid', cell], facts) facts.remove(['try_rock', location]) for cell in adjacents: if ['wumpus', cell] in facts: updatewumpusNowWithRocks.take_action(world, face_to(cell, percepts)) percepts = updatewumpusNowWithRocks.take_action(world, 'Shoot') for fact in facts: if fact[0] == 'wumpus' or fact[0] == 'nasty': facts.remove(fact) print "removing fact" + str(fact) return percepts # this block moves to an adjacent cell if any are safe & unvisited for cell in adjacents: if cell not in order_to_visit: if ['safe', cell] in facts: print "\n*********************************\nTrying unvisited cell: " + str( cell) updatewumpusNowWithRocks.take_action(world, face_to(cell, percepts)) percepts = updatewumpusNowWithRocks.take_action(world, 'Step') return percepts # this block ranks cells to move to, choosing the one that was visited the longest ago, then moves # that cell to the end of the order rankings = [] for cell in adjacents: if ['safe', cell] in facts: rankings.append(order_to_visit.index(cell)) else: rankings.append(999) index = rankings.index(min(rankings)) if min(rankings) < 999: # get the min scored cell move_to = adjacents[index] # move to end of list order_to_visit.remove(move_to) order_to_visit.append(move_to) # move to that cell print "\n*********************************\nTrying to revisit " + str( move_to) updatewumpusNowWithRocks.take_action(world, face_to(move_to, percepts)) percepts = updatewumpusNowWithRocks.take_action(world, 'Step') return percepts move_to = trace[-2] print "\n*********************************\nTrying to step back to " + str( move_to) updatewumpusNowWithRocks.take_action(world, face_to(move_to, percepts)) percepts = updatewumpusNowWithRocks.take_action(world, 'Step') return percepts
def go_back_home(world, home, percepts, order_to_visit): location = percepts[5] adjacents = updatewumpusNowWithRocks.look_ahead(world) for cell in order_to_visit[order_to_visit.index(home):]: if cell in adjacents: updatewumpusNowWithRocks.take_action(world, face_to(cell, percepts)) percepts = updatewumpusNowWithRocks.take_action(world, 'Step') return percepts #start the actual solutione order_to_visit = [] trace = [] percepts = updatewumpusNowWithRocks.take_action(world, "Up") home = percepts[5] while percepts[2] != 'glitter' and percepts[ 7] != 'dead': #until we find the gold percepts = find_gold(world, percepts, facts, rules, order_to_visit, trace) print order_to_visit add_percepts(world, percepts, facts) inference_engine(rules, facts) updatewumpusNowWithRocks.take_action(world, "PickUp") while percepts[5] != home: percepts = go_back_home(world, home, percepts, order_to_visit)
maybeWumpusList] = impWumpusWorld.findGoldProcess(name, solidList, notSolidList, stack) print stack # try to kill wumpus killRes = impWumpusWorld.killWumpusProcess(name, maybeWumpusList, solidList, notSolidList, stack) if killRes in maybeWumpusList: print killRes + ' : Kill Succeeded' solidList.append(killRes) maybePitList = filter(lambda a: a != killRes, maybePitList) else: print killRes # game ending flag check currentState = updatewumpusNowWithRocks.take_action(name, "Left") score = currentState[8] if score == 1100: updatewumpusNowWithRocks.take_action(name, "Exit") break else: [resSolidList, resNotSolidList ] = impWumpusWorld.pitFindProcess(name, maybePitList, solidList, notSolidList, stack) for e in resNotSolidList: if e not in notSolidList: notSolidList.append(e) for e in resSolidList: if e not in solidList: solidList.append(e)