0% found this document useful (0 votes)
3 views7 pages

Practical No.-5

Ai practical 5

Uploaded by

prem28pre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Practical No.-5

Ai practical 5

Uploaded by

prem28pre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICAL NO.

-5

A. Write a program to solve water jug problem.

Code-

From collections import deque

Def is_goal_reached(jug1, jug2, target):


Return jug1 == target or jug2 == target or jug1 + jug2 == target

Def water_jug_bfs(jug1_capacity, jug2_capacity, target_amount):


# Check if the target is greater than the combined capacity
If target_amount > jug1_capacity + jug2_capacity:
Return “No solution possible.”

Visited = set()
Queue = deque([(0, 0)]) # Starting with both jugs empty
Steps = []

While queue:
Jug1, jug2 = queue.popleft()
If (jug1, jug2) in visited:
Continue

# Mark this state as visited


Visited.add((jug1, jug2))
Steps.append((jug1, jug2))

# Check if we have reached the goal


If is_goal_reached(jug1, jug2, target_amount):
Return steps

# Possible operations:
# Fill jug1
Queue.append((jug1_capacity, jug2))
# Fill jug2
Queue.append((jug1, jug2_capacity))
# Empty jug1
Queue.append((0, jug2))
# Empty jug2
Queue.append((jug1, 0))
# Pour jug1 into jug2
Pour_to_jug2 = min(jug1, jug2_capacity – jug2)
Queue.append((jug1 – pour_to_jug2, jug2 + pour_to_jug2))
# Pour jug2 into jug1
Pour_to_jug1 = min(jug2, jug1_capacity – jug1)
Queue.append((jug1 + pour_to_jug1, jug2 – pour_to_jug1))

Return “No solution possible.”

# Example Usage:
Jug1_capacity = 4
Jug2_capacity = 3
Target_amount = 2

Result = water_jug_bfs(jug1_capacity, jug2_capacity, target_amount)


Print(“Steps to reach the goal:”)
For step in result:
Print(step)

OUTPUT:-
B. Design the simulation of tic – tac – toe game using min –
max algorithm.

Code:-
import math

# Constants

PLAYER_X = 'X'

PLAYER_O = 'O'

EMPTY = ' '

# Initialize the board

board = [

[EMPTY, EMPTY, EMPTY],

[EMPTY, EMPTY, EMPTY],

[EMPTY, EMPTY, EMPTY]

def print_board(board):

for row in board:

print("|".join(row))

print("-" * 5)

def check_winner(board):

# Check rows, columns, and diagonals for a winner

for row in board:

if row[0] == row[1] == row[2] != EMPTY:

return row[0]
for col in range(3):

if board[0][col] == board[1][col] == board[2][col] != EMPTY:

return board[0][col]

if board[0][0] == board[1][1] == board[2][2] != EMPTY:

return board[0][0]

if board[0][2] == board[1][1] == board[2][0] != EMPTY:

return board[0][2]

return None

def is_board_full(board):

for row in board:

if EMPTY in row:

return False

return True

def minimax(board, depth, is_maximizing):

winner = check_winner(board)

if winner == PLAYER_X:

return 1

elif winner == PLAYER_O:

return -1

elif is_board_full(board):

return 0

if is_maximizing:

best_score = -math.inf

for i in range(3):

for j in range(3):
if board[i][j] == EMPTY:

board[i][j] = PLAYER_X

score = minimax(board, depth + 1, False)

board[i][j] = EMPTY

best_score = max(score, best_score)

return best_score

else:

best_score = math.inf

for i in range(3):

for j in range(3):

if board[i][j] == EMPTY:

board[i][j] = PLAYER_O

score = minimax(board, depth + 1, True)

board[i][j] = EMPTY

best_score = min(score, best_score)

return best_score

def best_move(board):

best_score = -math.inf

move = None

for i in range(3):

for j in range(3):

if board[i][j] == EMPTY:

board[i][j] = PLAYER_X

score = minimax(board, 0, False)

board[i][j] = EMPTY

if score > best_score:

best_score = score
move = (i, j)

return move

def make_move(board, row, col, player):

if board[row][col] == EMPTY:

board[row][col] = player

return True

return False

def play_game():

current_player = PLAYER_O

while True:

print_board(board)

if current_player == PLAYER_X:

row, col = best_move(board)

make_move(board, row, col, PLAYER_X)

else:

row, col = map(int, input("Enter row and column: ").split())

if not make_move(board, row, col, PLAYER_O):

print("Invalid move! Try again.")

continue

winner = check_winner(board)

if winner:

print_board(board)

print(f"Player {winner} wins!")

break

if is_board_full(board):
print_board(board)

print("It's a draw!")

break

current_player = PLAYER_X if current_player == PLAYER_O else PLAYER_O

if __name__ == "__main__":

play_game()

OUTPUT:-

You might also like