ai_lab_5
class WumpusWorld:
def init(self, size=4):
self.size = size
self.grid = [['' for _ in range(size)] for _ in range(size)]
self.agent_position = (0, 0)
self.gold_position = (size - 1, size - 1)
self.visited = set()
self.found_gold = False
def add_pit(self, row, col):
self.grid[row][col] = 'P'
def add_wumpus(self, row, col):
self.grid[row][col] = 'W'
def dfs(self, row, col):
if (row, col) == self.gold_position:
print("Found gold!")
self.found_gold = True
return True
self.visited.add((row, col))
for move in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
new_row, new_col = row + move[0], col + move[1]
if 0 <= new_row < self.size and 0 <= new_col < self.size and (new_row, new_col) not in
self.visited:
if self.grid[new_row][new_col] != 'P' and self.grid[new_row][new_col] != 'W' and not
self.found_gold:
print(f"Moving to ({new_row}, {new_col})")
if self.dfs(new_row, new_col):
Update agent's position after moving
self.agent_position = (new_row, new_col)
return True
return False
def find_gold(self):
self.dfs(*self.agent_position)
def main():
world = WumpusWorld()
world.add_pit(2, 1)
world.add_pit(1, 2)
world.add_wumpus(2,3)
world.find_gold()
if name == "main":
main()