Implemetacion de la tercra solapa que es el juego del tictactou

This commit is contained in:
Santiago Parra 2024-12-13 23:15:46 +01:00
parent 19b77c2349
commit 332d0465b6
3 changed files with 91 additions and 3 deletions

View File

@ -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)

View File

@ -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

Binary file not shown.