Zadanie projektowe 1 - gra kółko krzyżyk

mail@pastecode.io avatar
unknown
python
2 months ago
4.0 kB
2
Indexable
Never
import tkinter as tk
from tkinter import messagebox
import random
import math

class TicTacToe:
    def __init__(self, root):
        self.root = root
        self.root.title("Kółko i krzyżyk")
        self.current_player = "X"
        self.is_computer_player = False

        self.board = [""] * 9
        self.buttons = []
        for i in range(3):
            row_buttons = []
            for j in range(3):
                button = tk.Button(root, text="", font=("Helvetica", 24), width=5, height=2,
                                   command=lambda i=i, j=j: self.make_move(i, j))
                button.grid(row=i, column=j)
                row_buttons.append(button)
            self.buttons.append(row_buttons)

        reset_button = tk.Button(root, text="Nowa gra", command=self.reset_game)
        reset_button.grid(row=3, columnspan=3)

        mode_button = tk.Button(root, text="Zmien tryb gry", command=self.toggle_mode)
        mode_button.grid(row=4, columnspan=3)

        self.info_label = tk.Label(root, text=f"Ruch gracza {self.current_player}", font=("Helvetica", 14))
        self.info_label.grid(row=5, columnspan=3)

    def make_move(self, row, col):
        if self.board[row * 3 + col] == "":
            self.board[row * 3 + col] = self.current_player
            self.buttons[row][col].config(text=self.current_player, state="disabled")

            winner = self.check_winner()
            if winner:
                messagebox.showinfo("Koniec gry", f"Gracz {winner} wygrywa!")
                self.reset_game()
            elif "" not in self.board:
                messagebox.showinfo("Koniec gry", "Remis!")
                self.reset_game()
            else:
                if self.is_computer_player and self.current_player == "X":
                    self.make_computer_move()
                else:
                    self.current_player = "O" if self.current_player == "X" else "X"
                    self.info_label.config(text=f"Ruch gracza {self.current_player}")

    def make_computer_move(self):

        available_list = []
        for i in range(0,9):
            if (self.board[i] != 'X') and (self.board[i] != 'O'):
                available_list.append(i)
                print(available_list)

        pc_board_index = random.choice(available_list)

        wiersz = math.floor(pc_board_index/3)
        kolumna = pc_board_index%3
        print(wiersz)
        print(kolumna)

        self.board[pc_board_index] = "O"
        self.buttons[wiersz][kolumna].config(text="O", state="disabled")

        winner = self.check_winner()
        if winner=='O':
            messagebox.showinfo("Koniec gry", f"Komputer wygrywa!")
            self.reset_game()
    def check_winner(self):

        for i in range(3):
            if self.board[i] == self.board[i + 3] == self.board[i + 6] != "":
                return self.board[i]
            if self.board[3 * i] == self.board[3 * i + 1] == self.board[3 * i + 2] != "":
                return self.board[3 * i]
        if self.board[0] == self.board[4] == self.board[8] != "":
            return self.board[0]
        if self.board[2] == self.board[4] == self.board[6] != "":
            return self.board[2]
        return None

    def reset_game(self):

        self.board = [""] * 9
        for row_buttons in self.buttons:
            for button in row_buttons:
                button.config(text="", state="normal")

        self.current_player = "X"
        self.info_label.config(text="Ruch gracza X")

    def toggle_mode(self):

        self.is_computer_player = not self.is_computer_player
        mode_text = "Gra z komputerem" if self.is_computer_player else "Gra z drugim graczem"
        messagebox.showinfo("Zmiana trybu gry", f"Aktualny tryb gry: {mode_text}")
        self.reset_game()

root = tk.Tk()
game = TicTacToe(root)
root.mainloop()
Leave a Comment