Ai Ass 6
Ai Ass 6
LAB
MCA first year
Session: (2023-2024)
SECTION – A
Assignment
Q-1 Write a Python program to implement tic-tac-toe game using python.
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
# Check columns
for col in range(3):
if all(board[row][col] == player for row in range(3)):
return True
# Check diagonals
if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player
for i in range(3)):
return True
return False
def is_board_full(board):
for row in board:
if " " in row:
return False
return True
def play_game():
board = [[" " for _ in range(3)] for _ in range(3)]
current_player = "X"
while True:
row = int(input(f"Player {current_player}, enter row (0, 1, 2): "))
col = int(input(f"Player {current_player}, enter column (0, 1, 2): "))
if board[row][col] != " ":
print("That cell is already occupied. Try again.")
continue
board[row][col] = current_player
print_board(board)
if check_winner(board, current_player):
print(f"Player {current_player} wins!")
break
elif is_board_full(board):
print("It's a tie!")
break
Output
Python program to implement Breadth First