Create a tic tac toe game with beautiful graphic
- User
Create a tic tac toe game with beautiful graphic
Assistant
Here's a Python implementation of a Tic Tac Toe game with beautiful graphics using Tkinter:
import tkinter as tk from tkinter import messagebox class TicTacToe: def __init__(self, root): self.root = root self.root.title("Tic Tac Toe") self.root.configure(bg='#f0f0f0') # Game variables self.current_player = '❌' # Start with X (emoji) self.board = [['' for _ in range(3)] for _ in range(3)] self.buttons = [[None for _ in range(3)] for _ in range(3)] self.winner = False # Configure grid to expand for i in range(3): root.grid_rowconfigure(i, weight=1) root.grid_columnconfigure(i, weight=1) # Create game grid for row in range(3): for col in range(3): btn = tk.Button(root, text='', font=('Arial', 40), width=4, height=2, bg='white', relief='ridge', bd=2, command=lambda r=row, c=col: self.make_move(r, c)) btn.grid(row=row, column=col, sticky='nsew') self.buttons[row][col] = btn # Create restart button self.restart_btn = tk.Button(root, text="Restart Game", font=('Arial', 12), bg='#4CAF50', fg='white', command=self.restart_game) self.restart_btn.grid(row=3, column=0, columnspan=3, sticky='nsew', pady=10) def make_move(self, row, col): if not self.winner and self.board[row][col] == '': self.board[row][col] = self.current_player self.buttons[row][col].config(text=self.current_player, state='disabled') if self.check_win(): self.show_result(f"Player {self.current_player} wins!") self.winner = True elif self.check_tie(): self.show_result("It's a tie!") self.winner = True else: self.switch_player() def check_win(self): # Check rows for row in self.board: if len(set(row)) == 1 and row[0] != '': return True # Check columns for col in range(3): if self.board[0][col] == self.board[1][col] == self.board[2][col] != '': return True # Check diagonals if (self.board[0][0] == self.board[1][1] == self.board[2][2] != '' or self.board[0][2] == self.board[1][1] == self.board[2][0] != ''): return True return False def check_tie(self): return all(self.board[row][col] != '' for row in range(3) for col in range(3)) def show_result(self, message): messagebox.showinfo("Game Over", message) def switch_player(self): self.current_player = '⭕' if self.current_player == '❌' else '❌' def restart_game(self): self.current_player = '❌' self.winner = False self.board = [['' for _ in range(3)] for _ in range(3)] for row in self.buttons: for btn in row: btn.config(text='', state='normal') if __name__ == "__main__": root = tk.Tk() game = TicTacToe(root) root.mainloop()This implementation includes:
-
Beautiful UI:
- Modern styling with emojis (❌ and ⭕) instead of X/O
- Clean white buttons with borders
- Responsive grid layout
- Green restart button with contrasting colors
-
Game Features:
- Proper turn switching between players
- Win detection for all possible lines
- Tie detection when all cells are filled
- Restart functionality to reset the game
- Disable buttons after game ends
-
User Experience:
- Clear visual feedback with emojis
- Responsive design that looks good on different screen sizes
- Informative message boxes to announce game results
To run the game:
- Copy the code into a Python file
- Install Tkinter (usually pre-installed with Python)
- Run the script
- Click on cells to play
- Use the "Restart Game" button to start over
The game uses emojis for better visual appeal and modern styling while maintaining the classic Tic Tac Toe gameplay. The interface is clean and intuitive with clear visual feedback for all game states.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.