mardi 12 février 2019

Avoiding multiple (not nested) if statements in my code

I am writing a function to scan a specific map (2D array). In order to avoid scanning spots outside of the array, I wrote a few if statements, but it feels like the wrong, long, inefficient way of doing it.

H is the map's height value, int W is for width, int c is the current spot, a tuple containing x and y values.

    floorH = c[0]-D
    floorW = c[1]-D
    ceilingH = c[0]+D+1
    ceilingW = c[1]+D+1
    if floorH < 0:
        floorH = 0
    if floorW < 0:
        floorW = 0
    if ceilingH > H:
        ceilingH = H
    if ceilingW > W:
        ceilingW = W

How can I write this better?

Thanks in advance :)

Aucun commentaire:

Enregistrer un commentaire