Aiandmlextra Programs
Aiandmlextra Programs
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
def check_winner(board):
for row in board:
if row[0] == row[1] == row[2] != ' ':
return row[0]
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] != ' ':
return board[0][col]
return None
def check_draw(board):
for row in board:
if ' ' in row:
return False
return True
if is_maximizing:
best_score = -math.inf
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
score = minimax(board, depth + 1, False)
board[i][j] = ' '
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] == ' ':
board[i][j] = 'X'
score = minimax(board, depth + 1, True)
board[i][j] = ' '
best_score = min(score, best_score)
return best_score
def find_best_move(board):
best_score = -math.inf
move = None
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
score = minimax(board, 0, False)
board[i][j] = ' '
if score > best_score:
best_score = score
move = (i, j)
return move
def get_valid_input():
while True:
try:
row, col = map(int, input("Enter row and column (0, 1, 2): ").split())
if row in range(3) and col in range(3):
return row, col
else:
print("Invalid input. Please enter numbers between 0 and 2.")
except ValueError:
print("Invalid input. Please enter two numbers separated by a space.")
def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X'
while True:
print_board(board)
if current_player == 'X':
print(f"Player {current_player}'s turn")
row, col = get_valid_input()
if board[row][col] != ' ':
print("Cell already taken. Try again.")
continue
board[row][col] = 'X'
else:
print(f"Player {current_player}'s turn (AI)")
move = find_best_move(board)
if move:
board[move[0]][move[1]] = 'O'
winner = check_winner(board)
if winner:
print_board(board)
print(f"Player {winner} wins!")
break
if check_draw(board):
print_board(board)
print("The game is a draw!")
break
if __name__ == "__main__":
tic_tac_toe()
OUTPUT:
| |
-----
| |
-----
| |
-----
Player X's turn
Enter row and column (0, 1, 2): 0 0
X| |
-----
| |
-----
| |
-----
Player O's turn (AI)
X| |
-----
|O|
-----
| |
-----
Player X's turn
Enter row and column (0, 1, 2): 1 0
X| |
-----
X|O|
-----
| |
-----
Player O's turn (AI)
X| |
-----
X|O|
-----
O| |
-----
Player X's turn
Enter row and column (0, 1, 2): 0 2
X| |X
-----
X|O|
-----
O| |
-----
Player O's turn (AI)
X|O|X
-----
X|O|
-----
O| |
-----
Player X's turn
Enter row and column (0, 1, 2): 2 1
X|O|X
-----
X|O|
-----
O|X|
-----
Player O's turn (AI)
X|O|X
-----
X|O|O
-----
O|X|
-----
Player X's turn
Enter row and column (0, 1, 2): 2 2
X|O|X
-----
X|O|O
-----
O|X|X
-----
The game is a draw!
2. WATER JUG(DFS)
PROGRAM: (program name WATER_JUG.py in VS)
from collections import defaultdict
if visited is None:
print(f"({amt1}, {amt2})")
return True
# Checks if we have already visited the combination or not. If not, then it proceeds further.
print(f"({amt1}, {amt2})")
return False
if __name__ == "__main__":
print("Steps: ")
OUTPUT:
Enter the capacity of jug 1: 3
Steps:
(0, 0)
(3, 0)
(3, 5)
(0, 5)
(3, 2)
(0, 2)
(2, 0)
(2, 5)
(3, 4)
(0, 4)
3. 8 QUEENS PUZZLE
for i in range(row):
if board[i][col] == 1:
return False
if board[i][j] == 1:
return False
if board[i][j] == 1:
return False
return True
return True
board[row][col] = 1
return True
board[row][col] = 0
return False
def print_board(board):
def solve_8_queens():
if solve_queens(board, 0):
print("Solution found:")
print_board(board)
else:
if __name__ == "__main__":
solve_8_queens()
OUTPUT:
Solution found:
10000000
00001000
00000001
00000100
00100000
00000010
01000000
00010000
DFS IN TREE
PROGRAM: ( Program name: DFS_TREE in VS )
class TreeNode:
self.value = value
self.children = []
def add_child(self, child):
self.children.append(child)
def dfs(node):
if node is None:
return
print(node.value)
dfs(child)
def build_tree():
root = TreeNode(root_value)
for _ in range(num_children):
child = TreeNode(child_value)
root.add_child(child)
build_subtree(child)
return root
def build_subtree(parent):
for _ in range(num_children):
child = TreeNode(child_value)
parent.add_child(child)
build_subtree(child)
if __name__ == "__main__":
root = build_tree()
print("\nDFS Traversal:")
dfs(root)
OUTPUT:
/ | \
B C D
/ \ |
E F G
DFS Traversal: