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): """Yapay zeka hamlesi yap.""" # Basit AI: Boş bir kareye rastgele hamle yapar 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") 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 check_winner(self): """Kazananı kontrol et.""" for row in self.board: if row[0] == row[1] == row[2] and row[0] is not None: 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] is not None: return True if self.board[0][0] == self.board[1][1] == self.board[2][2] and self.board[0][0] is not None: return True if self.board[0][2] == self.board[1][1] == self.board[2][0] and self.board[0][2] is not None: 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()