# Arup Guha
# 6/23/2025
# Constants and Functions for Minesweeper not part of the object.

import pygame, sys
from pygame.locals import *

# Where the board will start.
TOP_X = 50
TOP_Y = 150
STEP = 50
OFF_X = 18
OFF_Y = 13

# Size of board.
ROWS = 8
COLS = 8
NUMB = 10

# Codes for game states.
NOT_STARTED = -1
NO_MOVE = 0
LOST = 1
CONT = 2
WIN = 3

# Directions to adjacent squares.
DX = [-1,-1,-1,0,0,1,1,1]
DY = [-1,0,1,-1,1,-1,0,1]

# Let's us cheat to test!
DEBUG = False

# Function to put text onto the screen.
def draw(text, font, color, surface, x, y):
    
    # Draw text on a new Surface. Title, antialias and color are used.
    text = font.render(text, 1, color)

    # Returns a new rectangle covering the entire surface.
    # This rectangle will always start at (0, 0) with a width and height the same size as the image.
    textrect = text.get_rect()

    # Sets location of text.
    textrect.topleft = (x, y)

    # Go ahead and draw it to the surface.
    surface.blit(text, textrect)

# Draws an empty board.
def drawEmptyBoard(surface):

    # Total size of grid.
    myw = STEP*COLS
    myh = STEP*ROWS

    # Gray in the background.
    pygame.draw.rect(surface, 'gray', (TOP_X, TOP_Y, myw, myh))

    # Horizontal lines.
    for i in range(ROWS+1):
        pygame.draw.line(surface, 'black', (TOP_X, TOP_Y+STEP*i), (TOP_X+myw, TOP_Y+STEP*i))

    # Vertical lines.
    for i in range(COLS+1):
        pygame.draw.line(surface, 'black', (TOP_X+STEP*i ,TOP_Y) , (TOP_X+STEP*i, TOP_Y+myh))

# For a given position returns the valid (x,y) coordinates or returns None if it's not a valid
# click.
def getXY(pos):

    # Calculate mathematically.
    myC = (pos[0] - TOP_X)//STEP
    myR = (pos[1] - TOP_Y)//STEP

    # Out of bounds test.
    if myR < 0 or myR >= ROWS or myC < 0 or myC >= COLS:
        return None

    # This is good.
    return (myR, myC)
