vendredi 20 mars 2015

Python list variables not getting modified by if-statement clauses

I am making a game using the Langton's ant algorithm. And I want the list tiles to update the number to 0 in this case... But it doesn't. Why? Notes: - I am using python 2.7

- The direction variable is based on a 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)

Aucun commentaire:

Enregistrer un commentaire