[Python] How can I prevent direction-biased sorting while moving values in a grid of tiles?

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am attempting to make a simple, flowing dynamic across a grid of tiles for a game. Essentially, I want water values of cells in the grid to flow to one adjacent cell each frame according to a couple rules.

    Pls consult image below. I ran out of words trying to explain and include code. Context_img_bcIranoutofwords

    Below is the flow function I am calling on the tiles. I apologize I know it's excessive to write it out like this, and if you have a way of consolidating I would love to hear it! I mainly did this to assure that I could see every single step because I cannot figure out why program is behaving how it is.

    The question is: Is there something specifically about iterating through a list of lists like this that would cause the flow function to preferentially flow to the east? I have tried my best in the function to force a fair decision between the east and west tiles. And if it's not that, is there something else in this function that could be causing this?

    (I have double checked that the adjacency functions are working as they should. So I know it has nothing to do with that.)

    In the function "self.BLANK_adj" is a list assigned when finding adjacent squares. "self.BLANK_adj[0]" = the beta tile object. "self.BLANK_adj[_1]" = a check that is True if the adjacent square being looked

    def gravity_water_flow_nl_2(self):
    #self._adj [0] = beta tile
    #self._adj [1] = greater alt check

    flow_tile = self
    if self.water > FLOW:

    #S exists
    if self.S_adj != None:

    #S not tall
    if self.S_adj[1] != True:

    #S not full
    if self.S_adj[0].water < (100-FLOW):
    flow_tile = self.S_adj[0]

    #S Full
    else:

    #S full, W exists
    if self.W_adj != None:

    #W not tall
    if self.W_adj[1] != True:

    #W not full
    if self.W_adj[0].water < (100 - FLOW):

    #Check E
    #E exists
    if self.E_adj != None:

    #E tall
    if self.E_adj[1] == True:

    flow_tile = self.W_adj[0]

    #E not tall
    else:

    #E not full
    if self.E_adj[0].water <= (100 - FLOW):

    #E less water than self
    if self.E_adj[0].water < self.water:

    #W less water than self
    if self.W_adj[0].water < self.water:

    if self.E_adj[0].water > self.W_adj[0].water:
    flow_tile = self.W_adj[0]
    elif self.E_adj[0].water < self.W_adj[0].water:
    flow_tile = self.E_adj[0]
    else:
    flow_tile = random.choice([self.E_adj[0],self.W_adj[0]])

    #W equal or more than self
    else:
    flow_tile = self.E_adj[0]

    #E equal or more than self
    else:
    flow_tile = self.W_adj[0]

    #E doesn't exist
    else:
    flow_tile = self.W_adj[0]

    #W full
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj

    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #W tall
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj

    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #S full, W doesn't exist
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #S tall
    else:

    #W exists
    if self.W_adj != None:

    #W not tall
    if self.W_adj[1] != True:

    #W not full
    if self.W_adj[0].water < (100 - FLOW):

    #Check E
    #E exists
    if self.E_adj != None:

    #E tall
    if self.E_adj[1] == True:

    flow_tile = self.W_adj[0]

    #E not tall
    else:

    #E not full
    if self.E_adj[0].water < (100 - FLOW):

    #E less water than self
    if self.E_adj[0].water < self.water:

    #W less water than self
    if self.W_adj[0].water < self.water:

    if self.E_adj[0].water > self.W_adj[0].water:
    flow_tile = self.W_adj[0]
    elif self.E_adj[0].water < self.W_adj[0].water:
    flow_tile = self.E_adj[0]
    else:
    flow_tile = random.choice([self.E_adj[0],self.W_adj[0]])

    #W equal or more than self
    else:
    flow_tile = self.E_adj[0]

    #E equal or more than self
    else:
    flow_tile = self.W_adj[0]

    #E doesn't exist
    else:
    flow_tile = self.W_adj[0]

    #W full
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #W tall
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #W doesn't exist
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #S doesn't exist
    else:

    #W exists
    if self.W_adj != None:

    #W not tall
    if self.W_adj[1] != True:

    #W not full
    if self.W_adj[0].water < (100 - FLOW):

    #Check E
    #E exists
    if self.E_adj != None:

    #E tall
    if self.E_adj[1] == True:

    flow_tile = self.W_adj[0]

    #E not tall
    else:

    #E not full
    if self.E_adj[0].water < (100 - FLOW):

    #E less water than self
    if self.E_adj[0].water < self.water:

    #W less water than self
    if self.W_adj[0].water < self.water:

    if self.E_adj[0].water > self.W_adj[0].water:
    flow_tile = self.W_adj[0]
    elif self.E_adj[0].water < self.W_adj[0].water:
    flow_tile = self.E_adj[0]
    else:
    flow_tile = random.choice([self.E_adj[0],self.W_adj[0]])

    #W equal or more than self
    else:
    flow_tile = self.E_adj[0]

    #E equal or more than self
    else:
    flow_tile = self.W_adj[0]


    #E doesn't exist
    else:
    flow_tile = self.W_adj[0]

    #W full
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #W tall
    else:

    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #W doesn't exist
    else:
    #E exists
    if self.E_adj != None:

    #E not tall
    if self.E_adj[1] != True:

    #E not full
    if self.E_adj[0].water < (100-FLOW):
    flow_tile = self.E_adj
    #E full
    else:
    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E tall
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    #E doesn't exist
    else:

    #N exists
    if self.N_adj != None:

    #N not tall
    if self.N_adj[1] != True:

    #N not full
    if self.N_adj[0].water < (100 - FLOW):
    flow_tile = self.N_adj

    Continue reading...

Compartilhe esta Página