import tkinter as tk from tkinter import messagebox import random class TicTacToe: def __init__(self, root, mode, scoreboard): self.root = root self.root.title("Tic Tac Toe") self.mode = mode self.current_player = "X" self.board = [[None for _ in range(3)] for _ in range(3)] self.buttons = [[None for _ in range(3)] for _ in range(3)] self.scoreboard = scoreboard self.create_scoreboard() self.create_board() def create_scoreboard(self): """Skor tahtasını oluştur.""" self.score_label = tk.Label(self.root, text=f"Oyuncu 1 (X): {self.scoreboard['X']} | Oyuncu 2/Yapay Zeka (O): {self.scoreboard['O']}", font=("Comic Sans MS", 16, "bold"), bg="#222244", fg="cyan") self.score_label.grid(row=0, column=0, columnspan=3, sticky="ew") def update_scoreboard(self): """Skor tahtasını güncelle.""" self.score_label.config(text=f"Oyuncu 1 (X): {self.scoreboard['X']} | Oyuncu 2/Yapay Zeka (O): {self.scoreboard['O']}") def create_board(self): """Oyunun tahtasını oluştur.""" for row in range(3): for col in range(3): button = tk.Button(self.root, text=" ", font=("Courier", 28, "bold"), height=2, width=5, bg="#1A1A40", fg="white", activebackground="#4040A1", command=lambda r=row, c=col: self.make_move(r, c)) button.grid(row=row+1, column=col, sticky="nsew") self.buttons[row][col] = button for i in range(3): self.root.grid_rowconfigure(i+1, weight=1) self.root.grid_columnconfigure(i, weight=1) def make_move(self, row, col): """Oyuncunun hamlesini işle.""" if self.board[row][col] is None: self.board[row][col] = self.current_player self.buttons[row][col].config(text=self.current_player, fg="cyan" if self.current_player == "X" else "orange") if self.check_winner(): winner = "Oyuncu 1 (X)" if self.current_player == "X" else ("Yapay Zeka (O)" if self.mode == "PVE" else "Oyuncu 2 (O)") messagebox.showinfo("Oyun Sonu", f"{winner} kazandı!") self.scoreboard[self.current_player] += 1 self.update_scoreboard() self.highlight_winner() return elif self.is_draw(): messagebox.showinfo("Oyun Sonu", "Oyun berabere!") self.reset_game() return if self.mode == "PVE" and self.current_player == "X": self.current_player = "O" self.ai_move() else: self.current_player = "O" if self.current_player == "X" else "X" def ai_move(self): """Yapay zeka hamlesi.""" empty_cells = [(r, c) for r in range(3) for c in range(3) if self.board[r][c] is None] if empty_cells: row, col = random.choice(empty_cells) self.board[row][col] = "O" self.buttons[row][col].config(text="O", fg="orange") if self.check_winner(): messagebox.showinfo("Oyun Sonu", "Yapay Zeka (O) kazandı!") self.scoreboard["O"] += 1 self.update_scoreboard() self.highlight_winner() return elif self.is_draw(): messagebox.showinfo("Oyun Sonu", "Oyun berabere!") self.reset_game() return self.current_player = "X" def check_winner(self): """Kazananı kontrol et.""" for i in range(3): # Satır kontrolü if self.board[i][0] == self.board[i][1] == self.board[i][2] and self.board[i][0] is not None: self.winning_line = [(i, 0), (i, 1), (i, 2)] return True # Sütun kontrolü if self.board[0][i] == self.board[1][i] == self.board[2][i] and self.board[0][i] is not None: self.winning_line = [(0, i), (1, i), (2, i)] return True # Çapraz kontrol if self.board[0][0] == self.board[1][1] == self.board[2][2] and self.board[0][0] is not None: self.winning_line = [(0, 0), (1, 1), (2, 2)] return True if self.board[0][2] == self.board[1][1] == self.board[2][0] and self.board[0][2] is not None: self.winning_line = [(0, 2), (1, 1), (2, 0)] return True return False def highlight_winner(self): """Kazanan çizgisini vurgula.""" for r, c in self.winning_line: self.buttons[r][c].config(bg="cyan" if self.current_player == "X" else "orange") self.root.after(2000, self.reset_game) def is_draw(self): """Beraberlik durumunu kontrol et.""" for row in self.board: if None in row: return False return True def reset_game(self): """Oyunu sıfırla.""" self.current_player = "X" self.board = [[None for _ in range(3)] for _ in range(3)] for row in range(3): for col in range(3): self.buttons[row][col].config(text=" ", bg="#1A1A40") class MainMenu: def __init__(self, root): self.root = root self.root.title("Tic Tac Toe - Ana Menü") self.root.configure(bg="#101020") self.scoreboard = {"X": 0, "O": 0} self.create_menu() def create_menu(self): """Ana menü oluştur.""" tk.Label(self.root, text="Tic Tac Toe", font=("Courier", 36, "bold"), bg="#101020", fg="cyan").pack(pady=20) tk.Button(self.root, text="Oyuncu vs Oyuncu", font=("Courier", 20), bg="#333366", fg="white", command=self.start_pvp).pack(pady=10) tk.Button(self.root, text="Oyuncu vs Yapay Zeka", font=("Courier", 20), bg="#4040A1", fg="white", command=self.start_pve).pack(pady=10) tk.Button(self.root, text="Çıkış", font=("Courier", 20), bg="#993333", fg="white", command=self.root.quit).pack(pady=10) def start_pvp(self): """PvP modunu başlat.""" self.root.destroy() root = tk.Tk() TicTacToe(root, mode="PVP", scoreboard=self.scoreboard) root.mainloop() def start_pve(self): """PvE modunu başlat.""" self.root.destroy() root = tk.Tk() TicTacToe(root, mode="PVE", scoreboard=self.scoreboard) root.mainloop() if __name__ == "__main__": root = tk.Tk() menu = MainMenu(root) root.mainloop()