from ast import Return
from multiprocessing.context import SpawnProcess
from platform import python_branch
from tkinter.tix import ROW
import pygame
import numpy
import sys
pygame.init()

#Constants
DISPLAY_HEIGHT = 900
DISPLAY_WIDTH = 900
LINE_WIDTH = 25
SQUARE_SIZE= 300
ROWS = 3
COLUMNS=3
CIRCLE_RADIUS = 60
CIRCLE_WIDTH = 15
SPACE = 60

#Color Constants
RED = (255, 0, 0)
BG_COLOR = (20, 200, 160)
LINE_COLOR = (23, 145, 135)
CIRCLE_COLOR = (239, 231, 200)
CROSS_COLOR = (66, 66, 66)

display = pygame.display.set_mode((DISPLAY_WIDTH,DISPLAY_HEIGHT))
display.fill(BG_COLOR)

#Layout
pygame.draw.line(display , LINE_COLOR ,(0,SQUARE_SIZE) , (DISPLAY_WIDTH , SQUARE_SIZE)  ,LINE_WIDTH)
pygame.draw.line(display , LINE_COLOR ,(0,2*SQUARE_SIZE) , (DISPLAY_WIDTH , 2*SQUARE_SIZE)  ,LINE_WIDTH)
pygame.draw.line(display , LINE_COLOR ,(SQUARE_SIZE,0) , (SQUARE_SIZE,DISPLAY_HEIGHT)  ,LINE_WIDTH)
pygame.draw.line(display , LINE_COLOR ,(2*SQUARE_SIZE,0) , (2*SQUARE_SIZE,DISPLAY_HEIGHT)  ,LINE_WIDTH)

#MAIN LIST
game =  numpy.zeros((ROWS,COLUMNS))

#Functions
def draw_fiq():
    for x in range(ROWS):
        for y in range(COLUMNS):
            if game[x][y] == 1:
                pygame.draw.circle(display,CIRCLE_COLOR,(int(COLUMNS*SQUARE_SIZE + SQUARE_SIZE//2)) , (int(ROWS*SQUARE_SIZE + SQUARE_SIZE//2)) ,60,15 )
            elif game[x][y] == 2:
                pygame.draw.line(display,CROSS_COLOR,(COLUMNS*SQUARE_SIZE + SPACE , ROWS*SQUARE_SIZE+SPACE) , (COLUMNS*SQUARE_SIZE +SQUARE_SIZE-SPACE , ROWS*SQUARE_SIZE+SQUARE_SIZE-SPACE) , 25)
                pygame.draw.line(display,CROSS_COLOR,(COLUMNS*SQUARE_SIZE+SPACE ,ROWS*SQUARE_SIZE+SQUARE_SIZE-SPACE) , (COLUMNS*SQUARE_SIZE+SQUARE_SIZE-SPACE , ROWS*SQUARE_SIZE+SPACE) , 25)

def win(player):
    #By Columns
    for i in range(COLUMNS):
        if game[0][i] == player and game[1][i] == player and game[2][i] == player:
            print("PLAYER " + player + " WON ! /n" "Coordinates : 0,"+i+ "| 1,"+i+" | 2,"+i)
            return True
    #By Rows        
    for i in range(ROWS):
        if game[i][0] == player and game[i][1] == player and game[i][2] == player :
            print("PLAYER " + player + " WON ! /n" "Coordinates :" + i+",0 | "+i+",1 | "+i+",2 " )
            return True
            
    #Diagnol Lines
    if game[0][0] == player and game[1][1] == player and game[2][2] == player:
        print("PLAYER " + player + " WON ! /n" "Coordinates : 0,0 | 1,1 | 2,2 " )
        return True
    
    if game[2][0] == player and game[1][1] == player and game[0][2] == player:
        print("PLAYER " + player + " WON ! /n" "Coordinates : 2,0 | 1,1 | 0,2 " )
        return True

    return False

def available(r,c):
    if game[r][c] == 0 :
        return True
    return False

def mark(row, col, player):
	game[row][col] = player

def check_full():
    for i in ROWS:
        for j in COLUMNS:
            if game[i][j] == 0:
                return False
    return True
        

player = 1
game_OVER = False

#MAIN LOOP
while True:
	for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
		    sys.exit()

        if check_full():
                print(" TIE !")

        if event.type == pygame.MOUSEBUTTONDOWN and not game_OVER:
            X_= event.pos[0]
            Y_ = event.pos[1]
            row = int(Y_ // SQUARE_SIZE)
			col = int(X_ // SQUARE_SIZE)

            if available(row,col):
                mark(row,col ,player)
                if win(player):
                    game_OVER = True
                player = player %2 + 1
                


        


    pygame.display.update()