diff --git a/app/main.py b/app/main.py index b22d801..4088df4 100644 --- a/app/main.py +++ b/app/main.py @@ -1,19 +1,19 @@ +import threading import tkinter as tk from tkinter import Menu from tkinter import ttk -import threading +from app.ConfigMgr import ConfigMgr from app.chat_server.Server import Server from app.widgets import ClockLabel -from app.ConfigMgr import ConfigMgr from app.widgets.ChatTab import ChatTab from app.widgets.MusicDownloadTab import MusicDownloadTab from app.widgets.MusicPlayerTab import MusicPlayerTab +from app.widgets.TicTacToeTab import TicTacToeTab from app.widgets.TodoTab import TodoTab from app.widgets.UsageLabels import CPULabel, RAMLabel, BatteryLabel, NetworkLabel from app.widgets.WeatherTab import WeatherTab - stop_event = threading.Event() def on_closing(): @@ -128,6 +128,11 @@ todo_tab = TodoTab(notebook, stop_event=stop_event) todo_tab.pack(fill="both", expand=True) notebook.add(todo_tab, text="Todo List") +# Add the TodoTab to the notebook +tic_tac_toe_tab = TicTacToeTab(notebook, stop_event=stop_event) +tic_tac_toe_tab.pack(fill="both", expand=True) +notebook.add(tic_tac_toe_tab, text="Tic Tac Toe") + # Create the chat and music player frames within the right frame frame_chat = tk.Frame(frame_right, bg="lightgreen") frame_music_player = tk.Frame(frame_right) diff --git a/app/widgets/TicTacToeTab.py b/app/widgets/TicTacToeTab.py new file mode 100644 index 0000000..d69cf3f --- /dev/null +++ b/app/widgets/TicTacToeTab.py @@ -0,0 +1,83 @@ +import tkinter as tk +from tkinter import Frame, Button, Label, StringVar, messagebox +from app.widgets.abc import ThreadedTab + +class TicTacToeTab(ThreadedTab): + + def __init__(self, root: Frame | tk.Tk, **kwargs): + self.current_player = StringVar(value="X") # Start with player X + self.board = [[None for _ in range(3)] for _ in range(3)] # 3x3 board + super().__init__(root, **kwargs) + + def build(self): + # Create the main frame for the Tic Tac Toe interface + self.game_frame = Frame(self) + self.game_frame.pack(fill="both", expand=True) + + # Create the label to display the current player + self.player_label = Label(self.game_frame, textvariable=self.current_player, font=("Arial", 16)) + self.player_label.pack(padx=5, pady=5) + + # Create the grid for the Tic Tac Toe board + self.board_frame = Frame(self.game_frame) + self.board_frame.pack(expand=True) + + for row in range(3): + for col in range(3): + button = Button( + self.board_frame, + text="", + font=("Arial", 24), + width=5, + height=2, + command=lambda r=row, c=col: self.make_move(r, c) + ) + button.grid(row=row, column=col, padx=5, pady=5) + self.board[row][col] = button + + def make_move(self, row, col): + button = self.board[row][col] + # Check if the cell is already occupied + if button["text"] == "": + button["text"] = self.current_player.get() + if self.check_winner(): + messagebox.showinfo("Game Over", f"Player {self.current_player.get()} wins!") + self.reset_board() + elif self.is_draw(): + messagebox.showinfo("Game Over", "It's a draw!") + self.reset_board() + else: + self.switch_player() + else: + messagebox.showwarning("Invalid Move", "This cell is already taken!") + + def switch_player(self): + self.current_player.set("O" if self.current_player.get() == "X" else "X") + + def check_winner(self): + # Check rows, columns, and diagonals + for i in range(3): + if self.board[i][0]["text"] == self.board[i][1]["text"] == self.board[i][2]["text"] != "": + return True + if self.board[0][i]["text"] == self.board[1][i]["text"] == self.board[2][i]["text"] != "": + return True + if self.board[0][0]["text"] == self.board[1][1]["text"] == self.board[2][2]["text"] != "": + return True + if self.board[0][2]["text"] == self.board[1][1]["text"] == self.board[2][0]["text"] != "": + return True + return False + + def is_draw(self): + # Check if all cells are filled + return all(self.board[row][col]["text"] != "" for row in range(3) for col in range(3)) + + def reset_board(self): + # Clear the board + for row in range(3): + for col in range(3): + self.board[row][col]["text"] = "" + self.current_player.set("X") + + def task(self): + # Placeholder for threaded behavior if needed + pass diff --git a/app/widgets/__pycache__/TicTacToeTab.cpython-313.pyc b/app/widgets/__pycache__/TicTacToeTab.cpython-313.pyc new file mode 100644 index 0000000..9c9fb43 Binary files /dev/null and b/app/widgets/__pycache__/TicTacToeTab.cpython-313.pyc differ