Ai PRA1
Ai PRA1
Ai PRA1
for _ in range(9):
print_board(board)
if current_player == player_symbol:
while True:
try:
row = int(input("Enter row (0, 1, or 2): "))
col = int(input("Enter column (0, 1, or 2): "))
if board[row][col] == " ":
board[row][col] = player_symbol
break
else:
print("That cell is already occupied. Try again.")
except (ValueError, IndexError):
print("Invalid input. Please enter valid row and column numbers.")
else:
print("Computer's turn...")
make_computer_move(board, computer_symbol)
if check_win(board, current_player):
print_board(board)
if current_player == player_symbol:
print("You win!")
else:
print("The computer wins!")
break
current_player = player_symbol if current_player == computer_symbol else
computer_symbol
else:
print_board(board)
print("It's a draw!")
# Check for a possible winning move for the player and block it
player_symbol = "X" if computer_symbol == "O" else "O"
for row in range(3):
for col in range(3):
if board[row][col] == " ":
board[row][col] = player_symbol
if check_win(board, player_symbol):
board[row][col] = computer_symbol
return
board[row][col] = " "
while True:
row = random.randint(0, 2)
col = random.randint(0, 2)
if board[row][col] == " ":
board[row][col] = computer_symbol
return
for _ in range(9):
print_board(board)
while True:
try:
row = int(input(f"Player {current_player}'s turn. Enter row (0, 1, or 2): "))
col = int(input(f"Player {current_player}'s turn. Enter column (0, 1, or 2): "))
if board[row][col] == " ":
board[row][col] = current_player
break
else:
print("That cell is already occupied. Try again.")
except (ValueError, IndexError):
print("Invalid input. Please enter valid row and column numbers.")
if check_win(board, current_player):
print_board(board)
print(f"Player {current_player} wins!")
break
elif " " not in board[0] and " " not in board[1] and " " not in board[2]:
print_board(board)
print("It's a draw!")
break
if choice == "1":
play_vs_computer()
elif choice == "2":
play_vs_user()
elif choice == "3":
print("Goodbye!")
break
else:
print("Invalid choice. Please choose 1, 2, or 3.")
Output: