Tic-Tac-Toe game in Python: def print_board(board): for row in board: print(" | ".join(row)) print("-" * 9) def check_winner(board, player): for row in board: if all([cell == player for cell in row]): return True for col in range(3): if all([board[row][col] == player for row in range(3)]): return True 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): return all([cell != " " for row in board for cell in row]) def main(): board = [[" " for _ in range(3)] for _ in range(3)] players = ["X", "O"] current_player = 0 while True: print_board(board) player_symbol = players[current_player] print(f"Player {player_symbol}'s turn.") row = int(input("Enter the row (0, 1, or 2): ")) col = int(input("Enter the column (0, 1, or 2): ")) if board[row][col] == " ": board[row][col] = player_symbol if check_winner(board, player_symbol): print_board(board) print(f"Player {player_symbol} wins!") break if is_board_full(board): print_board(board) print("It's a tie!") break current_player = 1 - current_player else: print("That cell is already taken. Try again.") if __name__ == "__main__": main() OUTPUT : | | --------- | | --------- | | --------- Player X's turn. Enter the row (0, 1, or 2): 1 Enter the column (0, 1, or 2): 0 | | --------- X | | --------- | | --------- Player O's turn. Enter the row (0, 1, or 2): 0 Enter the column (0, 1, or 2): 0 O | | --------- X | | --------- | | --------- Player X's turn. Enter the row (0, 1, or 2): 1 Enter the column (0, 1, or 2): 1 O | | --------- X | X | --------- | | --------- Player O's turn. Enter the row (0, 1, or 2): 0 Enter the column (0, 1, or 2): 1 O | O | --------- X | X | --------- | | --------- Player X's turn. Enter the row (0, 1, or 2): 1 Enter the column (0, 1, or 2): 2 O | O | --------- X | X | X --------- | | --------- Player X wins! >
Tic-Tac-Toe game in Python
Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated