import tkinter as tk from tkinter import messagebox import random class TicTacToe: def __init__(self, root): self.root = root self.root.title("Tic Tac Toe") 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.mode = None # PvP veya PvE self.create_mode_selection() def create_mode_selection(self): """PvP veya PvE modunu seçmek için başlangıç ekranı oluştur.""" self.mode_selection_frame = tk.Frame(self.root) self.mode_selection_frame.pack() tk.Label(self.mode_selection_frame, text="Oyun Modunu Seçin:", font=("Arial", 16)).pack(pady=10) tk.Button(self.mode_selection_frame, text="PvP (İki Oyuncu)", font=("Arial", 14), command=self.start_pvp).pack(pady=5) tk.Button(self.mode_selection_frame, text="PvE (Yapay Zeka)", font=("Arial", 14), command=self.start_pve).pack(pady=5) def start_pvp(self): """PvP modunu başlat.""" self.mode = "PvP" self.mode_selection_frame.destroy() self.create_board() def start_pve(self): """PvE modunu başlat.""" self.mode = "PvE" self.mode_selection_frame.destroy() self.create_board() 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=("Arial", 24), height=2, width=5, command=lambda r=row, c=col: self.make_move(r, c)) button.grid(row=row, column=col) self.buttons[row][col] = button def make_move(self, row, col): """Hamle 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) if self.check_winner(): messagebox.showinfo("Oyun Sonu", f"Oyuncu {self.current_player} kazandı!") self.reset_game() return elif self.is_draw(): messagebox.showinfo("Oyun Sonu", "Oyun berabere!") self.reset_game() return if self.mode == "PvP": # PvP: Sıradaki oyuncuya geç self.current_player = "O" if self.current_player == "X" else "X" elif self.mode == "PvE": # PvE: Oyuncu X, AI O oynar if self.current_player == "X": self.current_player = "O" self.ai_move() self.current_player = "X" def ai_move(self): """Alfa-beta budama kullanarak yapay zeka hamlesi yap.""" best_score = float('-inf') best_move = None for row in range(3): for col in range(3): if self.board[row][col] is None: # Boş hücreyi bul self.board[row][col] = "O" # Yapay zekanın hamlesini simüle et score = self.alpha_beta(0, False, float('-inf'), float('inf')) # Alfa-beta budamasını kullan self.board[row][col] = None # Simülasyonu geri al if score > best_score: # En iyi skoru güncelle best_score = score best_move = (row, col) if best_move: row, col = best_move self.board[row][col] = "O" self.buttons[row][col].config(text="O") if self.check_winner(): messagebox.showinfo("Oyun Sonu", "Yapay Zeka (O) kazandı!") self.reset_game() elif self.is_draw(): messagebox.showinfo("Oyun Sonu", "Oyun berabere!") self.reset_game() def alpha_beta(self, depth, is_maximizing, alpha, beta): """Alfa-beta budama algoritması.""" if self.check_winner_for("O"): return 1 # AI (O) kazandı if self.check_winner_for("X"): return -1 # İnsan (X) kazandı if self.is_draw(): return 0 # Beraberlik if is_maximizing: # Yapay zekanın hamlesi best_score = float('-inf') for row in range(3): for col in range(3): if self.board[row][col] is None: self.board[row][col] = "O" score = self.alpha_beta(depth + 1, False, alpha, beta) self.board[row][col] = None best_score = max(best_score, score) alpha = max(alpha, best_score) if beta <= alpha: break # Budama return best_score else: # Rakibin hamlesi best_score = float('inf') for row in range(3): for col in range(3): if self.board[row][col] is None: self.board[row][col] = "X" score = self.alpha_beta(depth + 1, True, alpha, beta) self.board[row][col] = None best_score = min(best_score, score) beta = min(beta, best_score) if beta <= alpha: break # Budama return best_score def check_winner(self): """Kazananı kontrol et.""" if self.check_winner_for("X"): return "X" if self.check_winner_for("O"): return "O" return None def check_winner_for(self, player): """Belirli bir oyuncu için kazananı kontrol et.""" for row in self.board: if row[0] == row[1] == row[2] and row[0] == player: return True for col in range(3): if self.board[0][col] == self.board[1][col] == self.board[2][col] and self.board[0][col] == player: return True if self.board[0][0] == self.board[1][1] == self.board[2][2] and self.board[0][0] == player: return True if self.board[0][2] == self.board[1][1] == self.board[2][0] and self.board[0][2] == player: return True return False 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=" ") # Tkinter uygulamasını başlat if __name__ == "__main__": root = tk.Tk() game = TicTacToe(root) root.mainloop()