gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
hi
- User
hi
- Assistant
Hello! How may I assist you today?
- User
can you write a code for xo game
- 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()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.