SAVITIBAI PHULE UNIVERSITY
M.Sc.(Computer Science) Sem-I
Practical Examination (From 2023-2024)
SUBJECT: CS-505-MJP: Lab Course on CS-502-MJ (Artificial Intelligence)
x
Aartificial inteligence practical solutions sppu 2023
slip no 12
Q 1. Write a python program to generate Calendar for the given month and year?
import calendar
def generate_calendar(year, month):
cal = calendar.monthcalendar(year, month)
month_name = calendar.month_name[month]
# Display the header
print(f"\n{month_name} {year}")
print("Mo Tu We Th Fr Sa Su")
# Display the calendar
for week in cal:
for day in week:
if day == 0:
print(" ", end=" ")
else:
print(f"{day:2} ", end=" ")
print()
# Input: Year and Month
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))
# Generate and display the calendar
generate_calendar(year, month)
Q2. )Write a Python program to simulate 4-Queens problem.
def print_solution(board):
for row in board:
print(" ".join(row))
print()
def is_safe(board, row, col):
# Check if there is a queen in the same row to the left
for i in range(col):
if board[row][i] == 'Q':
return False
# Check if there is a queen in the upper diagonal on the left
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
# Check if there is a queen in the lower diagonal on the left
for i, j in zip(range(row, len(board)), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
return True
def solve_n_queens(board, col):
if col == len(board):
print_solution(board)
return
for i in range(len(board)):
if is_safe(board, i, col):
board[i][col] = 'Q'
solve_n_queens(board, col + 1)
board[i][col] = '.' # backtrack
def four_queens():
n = 4
empty_board = [['.' for _ in range(n)] for _ in range(n)]
solve_n_queens(empty_board, 0)
if __name__ == "__main__":
four_queens()
************************************************
************************************************
slip no 13
Q1. Write a Python program to implement Mini-Max Algorithm.
import math
def evaluate(board):
# This function evaluates the current state of the board.
# In a real game, you would replace this with a more complex evaluation function.
if "X" in board and "O" in board:
return 0 # The game is still ongoing
elif "X" in board:
return 1 # Player 'X' wins
elif "O" in board:
return -1 # Player 'O' wins
else:
return 0 # It's a draw
def is_terminal(board):
# Check if the game has ended (terminal state)
return evaluate(board) != 0 or "." not in board
def minimax(board, depth, is_maximizing):
if is_terminal(board):
return evaluate(board)
if is_maximizing:
max_eval = -math.inf
for i in range(len(board)):
if board[i] == ".":
board[i] = "X"
eval = minimax(board, depth + 1, False)
board[i] = "." # undo the move
max_eval = max(max_eval, eval)
return max_eval
else:
min_eval = math.inf
for i in range(len(board)):
if board[i] == ".":
board[i] = "O"
eval = minimax(board, depth + 1, True)
board[i] = "." # undo the move
min_eval = min(min_eval, eval)
return min_eval
def find_best_move(board):
best_val = -math.inf
best_move = -1
for i in range(len(board)):
if board[i] == ".":
board[i] = "X"
move_val = minimax(board, 0, False)
board[i] = "." # undo the move
if move_val > best_val:
best_val = move_val
best_move = i
return best_move
def print_board(board):
for i in range(0, len(board), 3):
print(" ".join(board[i:i + 3]))
if __name__ == "__main__":
# Example usage for a simple tic-tac-toe-like game
board = ["X", ".", ".", ".", "O", ".", ".", ".", "."]
print("Initial Board:")
print_board(board)
best_move = find_best_move(board)
board[best_move] = "X"
print("\nAfter 'X' makes the best move:")
print_board(board)
Q2.Write a Python program to simulate 8-Queens problem
def print_solution(board):
for row in board:
print(" ".join(row))
print()
def is_safe(board, row, col):
# Check if there is a queen in the same row to the left
for i in range(col):
if board[row][i] == 'Q':
return False
# Check if there is a queen in the upper diagonal on the left
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
# Check if there is a queen in the lower diagonal on the left
for i, j in zip(range(row, len(board)), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
return True
def solve_n_queens(board, col):
if col == len(board):
print_solution(board)
return
for i in range(len(board)):
if is_safe(board, i, col):
board[i][col] = 'Q'
solve_n_queens(board, col + 1)
board[i][col] = '.' # backtrack
def eight_queens():
n = 8
empty_board = [['.' for _ in range(n)] for _ in range(n)]
solve_n_queens(empty_board, 0)
if __name__ == "__main__":
eight_queens()