

# 18/10/2024
# Term 1 Mini Project - Ultimate Tic Tac Toe / Super TTT

#\\ IMPORTS //#
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1' # Disables that really annoying welcome message pygame outputs

import sys
import playsound3
import pygame
from pygame import MOUSEBUTTONDOWN

#\\ CONSTANTS //#

# Sizes
SCREEN_WIDTH = 646 # px
SCREEN_HEIGHT = 646 # px

GRID_SIZE = 9
CELL_SIZE = (SCREEN_WIDTH + 2) // GRID_SIZE

# Colours
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREY = (128, 128, 128)

#\\ FUNCTIONS //#

#\\ CLASSES //#

class TTTGame:
    def __init__(self):
        self.grid = [["#" for i in range(9)] for i in range(9)]
        self.ultimate_grid = [["#" for i in range(3)] for i in range(3)]
        self.turn = "X"
        self.restriction_x = -1
        self.restriction_y = -1
        self.running = True

        pygame.init()

        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("Super T-T-T")

        self.game()


    def draw_grid(self):
        colour = WHITE

        self.screen.fill(colour)

        for row in range(9):
            for col in range(9):
                row_grid = row // 3
                col_grid = col // 3

                if row_grid % 2 == 0:
                    if col_grid % 2 == 1:
                        colour = GREY
                    else:
                        colour = WHITE
                else:
                    if col_grid % 2 == 0:
                        colour = GREY
                    else:
                        colour = WHITE

                x_pos = (row * 70) + (2 * row)
                y_pos = (col * 70) + (2 * col)



                # Draws the cells
                grid = pygame.draw.rect(self.screen, colour,(x_pos, y_pos, CELL_SIZE - 2, CELL_SIZE - 2))

                # Restriction highlights
                if self.restriction_x != -1 and self.restriction_y != -1:
                    rx_pos = (self.restriction_x * 210) + (6 * self.restriction_x)
                    ry_pos = (self.restriction_y * 210) + (6 * self.restriction_y)

                    highlight = pygame.Surface((214, 214), pygame.SRCALPHA)  # per-pixel alpha
                    highlight.fill((0, 200, 0, 12))  # notice the alpha value in the color

                    self.screen.blit(highlight, (rx_pos, ry_pos))


                # Draws the marked cells
                if self.grid[col][row] == "X":
                    imp = pygame.image.load("./assets/70x70/x70.png")
                    self.screen.blit(imp, (x_pos, y_pos))
                elif self.grid[col][row] == "O":
                    imp = pygame.image.load("./assets/70x70/o70.png")
                    self.screen.blit(imp, (x_pos, y_pos))

                # Draws the gridlines
                x_gridline = pygame.draw.rect(self.screen, BLACK, (col * CELL_SIZE, row * CELL_SIZE - 2, CELL_SIZE, 2))
                y_gridline = pygame.draw.rect(self.screen, BLACK, (col * CELL_SIZE - 2, row * CELL_SIZE, 2, CELL_SIZE - 2))
        for row in range(3):
            for col in range(3):
                x_pos = (row * 210) + (6 * row)
                y_pos = (col * 210) + (6 * col)

                if self.ultimate_grid[col][row] == "X":
                    imp = pygame.image.load("./assets/214x214/x214.png")
                    self.screen.blit(imp, (x_pos, y_pos))
                elif self.ultimate_grid[col][row] == "O":
                    imp = pygame.image.load("./assets/214x214/o214.png")
                    self.screen.blit(imp, (x_pos, y_pos))

        pygame.display.flip()

    def validate_move(self, x, y):
        if (x // 3 == self.restriction_x and y // 3 == self.restriction_y) or (self.restriction_x == -1 and self.restriction_y == -1):
            if self.grid[y][x] == "#":
                return True
            else:
                return False
        else:
            playsound3.playsound("./assets/sounds/denied.mp3")
            return False

    def game(self):
        self.draw_grid()

        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                if event.type == MOUSEBUTTONDOWN:
                    if event.button == 1:
                        x, y = pygame.mouse.get_pos()

                        x_cell = x // 72
                        y_cell = y // 72

                        x_pos = (x_cell * 70) + (2 * x_cell)
                        y_pos = (y_cell * 70) + (2 * y_cell)

                        ux_cell = x_cell % 3
                        uy_cell = y_cell % 3

                        if self.validate_move(x_cell, y_cell):
                            print("valid")
                            if self.turn == "X":
                                self.grid[y_cell][x_cell] = "X"
                                self.turn = "O"
                            elif self.turn == "O":
                                self.grid[y_cell][x_cell] = "O"
                                self.turn = "X"

                            if self.ultimate_grid[uy_cell][ux_cell] == "#":
                                self.restriction_x = ux_cell
                                self.restriction_y = uy_cell

                            print(self.restriction_x, self.restriction_y)

                            self.draw_grid()



        pygame.quit()




if __name__ == "__main__":
    game = TTTGame()