create tik tak game
- User
create tik tak game
Assistant
Tic Tac Toe Game Implementation
Game Description
Tic Tac Toe is a simple game where two players, X and O, take turns marking a square on a 3x3 grid. The first player to get three in a row (horizontally, vertically, or diagonally) wins the game. If all squares are filled and no player has won, the game is a draw.
Code Implementation
# tic_tac_toe.py def print_board(board): """Print the current state of the game board.""" print(f" {board[0]} | {board[1]} | {board[2]} ") print("---+---+---") print(f" {board[3]} | {board[4]} | {board[5]} ") print("---+---+---") print(f" {board[6]} | {board[7]} | {board[8]} ") def check_win(board): """Check if the game has been won.""" win_conditions = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)] for condition in win_conditions: if board[condition[0]] == board[condition[1]] == board[condition[2]] != " ": return board[condition[0]] if " " not in board: return "Draw" return False def main(): """Main game loop.""" board = [" "] * 9 current_player = "X" while True: print_board(board) move = input(f"Player {current_player}, enter your move (1-9): ") if board[int(move) - 1] != " ": print("Invalid move, try again.") continue board[int(move) - 1] = current_player result = check_win(board) if result: print_board(board) if result == "Draw": print("It's a draw!") else: print(f"Player {result} wins!") break current_player = "O" if current_player == "X" else "X" if __name__ == "__main__": main()Example Usage
- Run the game by executing
python tic_tac_toe.py. - Players take turns entering their moves (1-9) when prompted.
- The game will print the current state of the board after each move.
- The game will end when a player wins or when the board is full (draw).
How to Play
- The game starts with an empty board.
- Player X makes the first move.
- Enter the number of the square where you'd like to place your mark (1-9).
- The game will update the board and check for a win or draw.
- Players alternate turns until the game is won or drawn.
- Run the game by executing
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.