0% found this document useful (0 votes)
13 views4 pages

Ai Ass 6

The document discusses implementing a tic-tac-toe game in Python. It includes functions to print the game board, check for a winner, and check if the board is full. The main function runs a game loop, alternating between players and checking for a winner or tie after each turn.

Uploaded by

khanaadil8650
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)
13 views4 pages

Ai Ass 6

The document discusses implementing a tic-tac-toe game in Python. It includes functions to print the game board, check for a winner, and check if the board is full. The main function runs a game loop, alternating between players and checking for a winner or tie after each turn.

Uploaded by

khanaadil8650
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/ 4

ARTIFICIAL INTELLIGENCE

LAB
MCA first year

Session: (2023-2024)

SUBMITTED BY: SUBMITTED TO:


Aadil khan Umakant Ahirwar
Roll No: 2384200001 (Class Advisor)

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)

def check_winner(board, player):


# Check rows
for row in board:
if all(cell == player for cell in row):
return True

# 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"

print("Let's play Tic-Tac-Toe!")


print_board(board)

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

current_player = "O" if current_player == "X" else "X"

# Start the game


if name == " main ":
play_game()

Output
Python program to implement Breadth First

You might also like