def add_percepts(world, percepts, facts): location = percepts[5] insert_fact([percepts[0], location], facts) insert_fact([percepts[1], location], facts) adjacents = updatewumpusNowWithRocks.look_ahead(world) for adjacent in adjacents: insert_fact(['adjacent', location, adjacent], facts)
def findGoldProcess(name, solidList, notSolidList, stack): maybePitList = [] maybeWumpusList = [] visited = [] # find gold 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) # check current game status if goldFlag == 'glitter' or livingFlag == 'dead': break # next step flag = libWumpusWorld.nextStep(name, adjacentCell, visited, solidList, notSolidList, currentCell, stack) if flag: break # pick up gold libWumpusWorld.pickUpGold(name, stack) return [maybePitList, maybeWumpusList]
def tryToGetPoint(name, solidList, notSolidList, stack): visited = [] # find gold 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) # try to kill wumpus pos = libWumpusWorld.tryToKillWumpus(name, smellFlag, adjacentCell, solidList, notSolidList, currentCell) solidList.append(pos) # check current game status if goldFlag == 'glitter' or livingFlag == 'dead': break # next step flag = libWumpusWorld.nextStep(name, adjacentCell, visited, solidList, notSolidList, currentCell, stack) if flag: break # pick up gold libWumpusWorld.pickUpGold(name, stack) # after toss step back libWumpusWorld.stepBack(name, stack)
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 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 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 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