Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

write a simple game code with gradio, the game have to be exciting for single player

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Perplexitypplx-7b-online’s answer.
  1. User

    write a simple game code with gradio, the game have to be exciting for single player

  2. Claude Assistant

    Here is a simple tic-tac-toe game code with Gradio that can be played by a single player against the computer:

    import random
    import gradio as gr
    
    board = ["-", "-", "-", 
             "-", "-", "-",  
             "-", "-", "-"]
    
    def print_board():
        print(board[0] + "|" + board[1] + "|" + board[2])
        print(board[3] + "|" + board[4] + "|" + board[5]) 
        print(board[6] + "|" + board[7] + "|" + board[8])
    
    def check_win(b):
        if (b[0] == b[1] == b[2] != "-") or (b[3] == b[4] == b[5] != "-") or (b[6] == b[7] == b[8] != "-") or (b[0] == b[3] == b[6] != "-") or (b[1] == b[4] == b[7] != "-") or (b[2] == b[5] == b[8] != "-") or (b[0] == b[4] == b[8] != "-") or (b[2] == b[4] == b[6] != "-"):
            return True
        else:
            return False
    
    def player_move():
        p_move = int(input("Enter your move (1-9): "))
        if board[p_move-1] == "-":
            board[p_move-1] = "X" 
        else:
            print("Invalid move")
            player_move()
            
    def computer_move():
        possible_moves = [x for x, letter in enumerate(board) if letter == "-"] 
        move = random.choice(possible_moves)  
        board[move] = "O"
    
    def play_game():
        print_board()
    
        while True:
            player_move()
            print_board()
            if check_win(board):
                print("You won!") 
                break
            
            computer_move() 
            print_board()
            if check_win(board):
                print("You lost!")
                break 
    
    # Gradio UI
    iface = gr.Interface(fn=play_game, inputs=[], outputs="text") 
    
    iface.launch()
    

    This implements a basic tic-tac-toe board and logic, allows the player to make moves, randomly generates computer moves, and checks for a winning condition after each move. The Gradio UI wraps it into an interface with no inputs and text outputs to print the board and game results.

    Let me know if you would like any modifications or have additional requirements for the game!

  3. User

    i want with gradio ui

  4. Claude Assistant

    Here is a version of the tic-tac-toe game with a graphical Gradio UI:

    import random
    import gradio as gr
    
    board = ["-", "-", "-", 
             "-", "-", "-",  
             "-", "-", "-"]  
    
    def check_win(b):
        # Same win-checking logic
    
    def play_game(move):
        move = move - 1
        
        if board[move] == "-": 
            board[move] = "X"  
        else:
            print("Invalid move") 
            return "".join(board)
        
        if check_win(board):
            return "You won!" 
        
        possible_moves = [x for x, letter in enumerate(board) if letter == "-"]
        computer_move = random.choice(possible_moves)  
        board[computer_move] = "O"
        
        if check_win(board):
            return "You lost!"
        
        return "".join(board)
    
    iface = gr.Interface(
        play_game, 
        gr.inputs.Number(default=1, label="Select your move"),
        "text",
        examples=[[1], [2], [3]]
    )
    
    iface.launch()
    

    In this version:

    • The play_game function takes in the player's move as input and returns the updated board or game result as text
    • The Gradio interface has a number input to select the move
    • Updated board and game results are displayed as text output

    The player and computer take turns making moves until there is a winner.

    Some ways this could be improved:

    • Show the tic-tac-toe board visually in the UI instead of as text
    • Add restart button to play again
    • Allow two players to play on the same interface

    Let me know if you have any other questions!

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