Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

hi

  1. User

    hi

  2. Assistant

    Hello! How may I assist you today?

  3. User

    can you write a code for xo game

  4. Assistant

    Sure, here's a sample code for a Tic Tac Toe game in Python:

    import random
    
    def print_board(board):
        print("\n")
        print(board[0], "|", board[1], "|", board[2])
        print("---------")
        print(board[3], "|", board[4], "|", board[5])
        print("---------")
        print(board[6], "|", board[7], "|", board[8])
        print("\n")
    
    def player_move(board, player):
        while True:
            try:
                move = int(input("Pick a spot (0-8): "))
                if board[move] == " ":
                    board[move] = player
                    break
                else:
                    print("That spot is already taken!")
            except:
                print("Invalid input. Please try again.")
        print_board(board)
    
    def computer_move(board, computer):
        print("Computer's Turn:")
        while True:
            move = random.randint(0, 8)
            if board[move] == " ":
                board[move] = computer
                break
        print_board(board)
    
    def is_winner(board, player):
        if (board[0] == player and board[1] == player and board[2] == player) or \
           (board[3] == player and board[4] == player and board[5] == player) or \
           (board[6] == player and board[7] == player and board[8] == player) or \
           (board[0] == player and board[3] == player and board[6] == player) or \
           (board[1] == player and board[4] == player and board[7] == player) or \
           (board[2] == player and board[5] == player and board[8] == player) or \
           (board[0] == player and board[4] == player and board[8] == player) or \
           (board[2] == player and board[4] == player and board[6] == player):
            return True
        else:
            return False
    
    def is_board_full(board):
        if " " in board:
            return False
        else:
            return True
    
    def main():
        board = [" "] * 9
        print_board(board)
        player = "X"
        computer = "O"
        while True:
            player_move(board, player)
            if is_winner(board, player):
                print("Congratulations! You've won the game!")
                break
            if is_board_full(board):
                print("The game is tied!")
                break
            computer_move(board, computer)
            if is_winner(board, computer):
                print("The computer has won the game!")
                break
            if is_board_full(board):
                print("The game is tied!")
                break
    
    main()
    

    This should get you started on building your own Tic Tac Toe game in Python.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.