Python list variables not getting modified by if-statement clauses -


i making game using langton's ant algorithm. want list tiles update number 0 in case... doesn't. why?

note: direction variable based on compass (n, e, w, s)

posx = 4 posy = 4 direction = 'w'  tiles = [[1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1],      [1,1,1,1,1,1,1,1,1,1]] def posision(posx, posy, tiles, direction):     if tiles[posx][posy] == 1:         tiles[posx][posy] = 0     if tiles[posx][posy] == 0:         tiles[posx][posy] = 1      oldtiles = tiles      if direction == 'n':         if oldtiles[posx][posy] == 1:             posx = posx+1             return 'w', tiles         if oldtiles[posx][posy] == 0:             posx = posx-1             return 'e', tiles     if direction == 's':         if oldtiles[posx][posy] == 0:             posx = posx+1             return 'w', tiles         if oldtiles[posx][posy] == 1:             posx = posx-1             return 'e', tiles     if direction == 'e':         if oldtiles[posx][posy] == 1:             posy = posy +1             return 'n', tiles         if oldtiles[posx][posy] == 0:             posy = posy -1             return 's', tiles     if direction == 'w':         if oldtiles[posx][posy] == 0:             posy = posy +1             return 'n', tiles         if oldtiles[posx][posy] == 1:             posy = posy -1             return 's', tiles  direction, tiles = posision(posx, posy, tiles, direction) print(tiles) 

on line:

if tiles[posx][posy] == 1:         tiles[posx][posy] = 0     if tiles[posx][posy] == 0:         tiles[posx][posy] = 1 

you saying:

if some_var 1     change 0  # i've changed 0    if some_var 0 # changing 1?      change 1 

i not sure if correct logic game? should change to:

if tiles[posx][posy] == 1:         tiles[posx][posy] = 0 elif tiles[posx][posy] == 0:  # else-if condition         tiles[posx][posy] = 1 

i recommend revisit flow control logics i.e. if-else ones see if explaination makes sense you. if-else sphagetti 1 of common problems made experts sometimes. figure out, it's okay.

an obvious 1 oldtiles modification inside if block further down code.


Comments

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

amazon web services - Installing MobileFirst 7.0 server on AWS -

Non Unique Username with ASP.Net Identity 2.0 -