import tkinter as tk
from tkinter import messagebox
# إعداد الواجهة
root = tk.Tk()
root.title("لعبة XO")
current_player = "X"
board = [["" for _ in range(3)] for _ in range(3)]
buttons = [[None for _ in range(3)] for _ in range(3)]
def check_winner():
for i in range(3):
# صفوف وأعمدة
if board[i][0] == board[i][1] == board[i][2] != "":
return True
if board[0][i] == board[1][i] == board[2][i] != "":
return True
# الأقطار
if board[0][0] == board[1][1] == board[2][2] != "":
return True
if board[0][2] == board[1][1] == board[2][0] != "":
return True
return False
def is_draw():
return all(cell != "" for row in board for cell in row)
def on_click(row, col):
global current_player
if board[row][col] == "":
board[row][col] = current_player
buttons[row][col]["text"] = current_player
buttons[row][col]["state"] = "disabled"
if check_winner():
messagebox.showinfo("انتهت اللعبة", f"اللاعب {current_player} فاز!")
root.quit()
elif is_draw():
messagebox.showinfo("انتهت اللعبة", "تعادل!")
root.quit()
else:
current_player = "O" if current_player == "X" else "X"
# إنشاء الأزرار
for i in range(3):
for j in range(3):
buttons[i][j] = tk.Button(root, text="", font=("Arial", 40), width=5, height=2,
command=lambda row=i, col=j: on_click(row, col))
buttons[i][j].grid(row=i, column=j)
# تشغيل الواجهة
root.mainloop()