Professional Documents
Culture Documents
Ai Outputs
Ai Outputs
Ai Outputs
import random
# User movement
while True:
# Update the grid with the agent's current position
grid[user_position[0]][user_position[1]] = 'A'
print("Current Grid:")
for row in grid:
print(row)
if user_position == gold_location:
print("Congratulations! You found the gold!")
break
if user_position == wumpus_location:
print("Game Over! You encountered the Wumpus.")
break
if user_position in pit_locations:
print("Game Over! You fell into a pit.")
break
**************OUTPUT**************
Current Grid:
['A', 'W', '', '']
['', '', 'G', '']
['', '', 'P', '']
['P', 'P', '', '']
Enter your move (up, down, left, right): down
Current Grid:
['', 'W', '', '']
['A', '', 'G', '']
['', '', 'P', '']
['P', 'P', '', '']
Enter your move (up, down, left, right): right
Current Grid:
['', 'W', '', '']
['', 'A', 'G', '']
['', '', 'P', '']
['P', 'P', '', '']
Enter your move (up, down, left, right): right
Congratulations! You found the gold!
Experiment No.3
while queue:
node, path = queue.popleft()
if node == goal:
return path
return None
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C']
}
start_node = 'A'
goal_node = 'F'
print(f"Starting BFS from node {start_node} to find node {goal_node}:")
if path:
print(f"Shortest path from {start_node} to {goal_node}: {' ->
'.join(path)}")
else:
print(f"No path found from {start_node} to {goal_node}")
***************OUTPUT***************
if current_node == goal:
print("Goal node reached!")
return
visited.add(current_node)
for neighbor in reversed(graph[current_node]):
if neighbor not in visited and neighbor not in (node for
node, _ in stack):
stack.append((neighbor, path + [neighbor]))
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B'],
'F': ['C']
}
start_node = 'A'
goal_node = 'F'
**************OUTPUT**************
import heapq
while open_set:
current_f_score, current_node = heapq.heappop(open_set)
if current_node == goal:
path = []
while current_node in came_from:
path.append(current_node)
current_node = came_from[current_node]
path.append(start)
path.reverse()
return path
return None
graph = {
(0, 0): [((0, 1), 1), ((1, 0), 1)],
(0, 1): [((0, 0), 1), ((1, 1), 1), ((2, 1), 2)],
(1, 0): [((0, 0), 1), ((1, 1), 1), ((1, 2), 2)],
(1, 1): [((0, 1), 1), ((1, 0), 1), ((1, 2), 1), ((2, 1), 1)],
(1, 2): [((1, 1), 1), ((2, 2), 2)],
(2, 1): [((1, 1), 1), ((1, 2), 1), ((2, 2), 1)],
(2, 2): [((1, 2), 2), ((2, 1), 1)]
}
start_node = (0, 0)
goal_node = (2, 2)
if path:
print("Optimal Path:", ' -> '.join(map(str, path)))
else:
print("Path not found")
**********OUTPUT************
Optimal Path: (0, 0) -> (0, 1) -> (2, 1) -> (2, 2)
Experiment No.6
import random
def print_board(board):
for i in range(0, 9, 3):
print(" | ".join(board[i:i+3]))
def available_moves(board):
return [i for i, cell in enumerate(board) if cell == " "]
def ai_move(board):
return random.choice(available_moves(board))
def tic_tac_toe():
board = [" " for _ in range(9)]
players = ["X", "O"]
current_player = players[random.randint(0, 1)]
while True:
print(f"Player {current_player}'s turn.")
if current_player == "X":
move = int(input("Enter cell number (1-9): ")) - 1
if board[move] != " ":
print("Cell already taken. Try again.")
continue
else:
move = ai_move(board)
board[move] = current_player
print_board(board)
if check_winner(board, current_player):
print(f"Player {current_player} wins!")
break
elif " " not in board:
print("It's a draw!")
break
current_player = players[(players.index(current_player) + 1) % 2]
tic_tac_toe()
*************OUTPUT************
% Define the capacities of the two jugs and the goal amount.
capacity1(4). % Capacity of the first jug
capacity2(3). % Capacity of the second jug
goal(2). % Goal: One of the jugs should have exactly 2 liters of
water
% Possible moves:
% 1. Fill Jug 1
move(state(_, Y), state(X2, Y), fill_jug1) :-
capacity1(X2),
X2 \= 0. % Avoid filling if the jug is already full.
% 2. Fill Jug 2
move(state(X, _), state(X, Y2), fill_jug2) :-
capacity2(Y2),
Y2 \= 0. % Avoid filling if the jug is already full.
% 3. Empty Jug 1
move(state(X, Y), state(0, Y), empty_jug1) :-
X > 0.
% 4. Empty Jug 2
move(state(X, Y), state(X, 0), empty_jug2) :-
Y > 0.
************OUTPUT************
compiling C:/Users/91908/Desktop/SEM-6/AI/programs/water_jug_problem.pl
for byte code...
C:/Users/91908/Desktop/SEM-6/AI/programs/water_jug_problem.pl compiled,
64 lines read - 5903 bytes written, 6 ms
| ?- solve(state(0, 0), Solution).
Solution =
[state(0,0),state(4,0),state(4,3),state(0,3),state(3,0),state(3,3),state(
4,2)] ?
Experiment No.8
class BlockWorldAgent:
def __init__(self, initial_state, goal_state):
self.initial_state = initial_state
self.goal_state = goal_state
def plan(self):
# Implement a planning algorithm here to generate a plan to reach
the goal state
plan = self.generate_plan()
return plan
def generate_plan(self):
# Implement a planning algorithm (e.g., STRIPS) to generate a
plan dynamically
# This is just a placeholder; replace it with your planning
algorithm
# For simplicity, let's return a simple plan to move each block
to its goal position
plan = []
for block, goal_location in self.goal_state.items():
current_location = self.initial_state[block]
if current_location != goal_location:
if goal_location == "on table":
plan.append(f"Move block {block} to the table")
else:
plan.append(f"Stack block {block} on top of block
{goal_location}")
return plan
Initial State:
Block A is on table
Block B is on table
Block C is on table
Block D is on E
Block E is on table
Goal State:
Block A should be on table
Block B should be on C
Block C should be on D
Block D should be on table
Block E should be on table