# Arup Guha
# 6/18/2024
# Day 12 Version of Connect 4 for SI@UCF Camp

import pygame, sys
from pygame.locals import *
from connect4file import connect4

def main():

    # Create an object to help track time
    clock = pygame.time.Clock()

    # Create the object.
    mygame = connect4()

    # Will set to false when the game is over.
    status = True

    # Flag for printing an error message.
    validMove = True

    # Game loop.
    while True:

        for event in pygame.event.get():

            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # This is for teams moving in one of the 7 columns.
            if event.type == KEYDOWN and status and (not mygame.dropping):

                # Use the ascii value...
                if event.unicode >= '1' and event.unicode <= '7':
                    validMove = mygame.executeTurn(ord(event.unicode) - ord('1'))
                
            # Looking for a mouse click.
            if event.type == MOUSEBUTTONDOWN and not status:

                # Get where it happened.
                pos = pygame.mouse.get_pos()

                # Hit the quit button
                if event.button == 1:
                    if mygame.quitbutton.collidepoint(pos):
                        pygame.quit()
                        sys.exit()

        # Just set the status to this.
        if mygame.winner != mygame.PIECES[2]:
            status = False
        
        # Draw and update the display.    
        mygame.drawgame()

        # Print error message if necessary.
        if status and not validMove:
            mygame.drawerror()

        # For end game.
        if not status and not mygame.dropping:
            mygame.drawend()
                
        pygame.display.update()
        clock.tick(30)

# Run it.
main()
            

            
