# Ian Binstock
# 6/12/2026
# Modification of drawmaze.py to fill screen and include mouse detection

import pygame, sys
from pygame.locals import *

SCREEN_W = 1000
SCREEN_H = 600
MARGIN = 100

def main():

    # Basic set up
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Load Maze from File")

    # Open file
    myfile = open("track.txt", "r")

    # Get the grid size
    toks = myfile.readline().split()
    r = int(toks[0])
    c = int(toks[1])

    # Calculate size of grid piece based on screen dimensions and grid size
    sqsize = [(SCREEN_W)//c, (SCREEN_H)//r]
    width = sqsize[0]*c
    height = sqsize[1]*r

    # Store the board here
    board = []

    # Read each line and add it to the board
    for i in range(r):
        tmp = myfile.readline().strip()
        board.append(tmp)

    # Close file
    myfile.close()
        
    # Game loop
    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        DISPLAYSURF.fill("white")

        # Get mouse position
        mouse_pos = pygame.mouse.get_pos()

        # Find starting location to center
        startx = (SCREEN_W - width)//2
        starty = (SCREEN_H - height)//2

        # Loop through the input board indexes
        for i in range(r):
            for j in range(c):

                # Calculate the corresponding x y coordinates
                x = startx + j*sqsize[0]
                y = starty + i*sqsize[1]

                # Create grid piece object
                cell_rect = pygame.Rect(x, y, sqsize[0], sqsize[1])

                # Color grid piece
                if cell_rect.collidepoint(mouse_pos):
                    color = "red"
                elif board[i][j] == 'X':
                    color = "black"
                else:
                    color = "gold"

                # Draw grid piece
                pygame.draw.rect(DISPLAYSURF, color, cell_rect, 0)
    
        pygame.display.update()

# Run it
main()