Practical No.-5
Practical No.-5
-5
Code-
Visited = set()
Queue = deque([(0, 0)]) # Starting with both jugs empty
Steps = []
While queue:
Jug1, jug2 = queue.popleft()
If (jug1, jug2) in visited:
Continue
# 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))
# Example Usage:
Jug1_capacity = 4
Jug2_capacity = 3
Target_amount = 2
OUTPUT:-
B. Design the simulation of tic – tac – toe game using min –
max algorithm.
Code:-
import math
# Constants
PLAYER_X = 'X'
PLAYER_O = 'O'
board = [
def print_board(board):
print("|".join(row))
print("-" * 5)
def check_winner(board):
return row[0]
for col in range(3):
return board[0][col]
return board[0][0]
return board[0][2]
return None
def is_board_full(board):
if EMPTY in row:
return False
return True
winner = check_winner(board)
if winner == PLAYER_X:
return 1
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
board[i][j] = EMPTY
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
board[i][j] = EMPTY
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
board[i][j] = EMPTY
best_score = score
move = (i, j)
return move
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:
else:
continue
winner = check_winner(board)
if winner:
print_board(board)
break
if is_board_full(board):
print_board(board)
print("It's a draw!")
break
if __name__ == "__main__":
play_game()
OUTPUT:-