0

Im currently working on a game in python and a made a funktion to check if a made move is possible, but every time i run the funktion the source variable "Xlist" gets changed.

The function:

def testIfPossible(key, Xlist):
    print(Xlist, '\n')
    print(move(key, Xlist), '\n')
    print(Xlist, '\n')

    if Xlist==move(key, Xlist):
        return False
    return True

The other Function I'm using in this Code:

def move(key, lst):
if key=='down arrow':
    for _ in range(lenY*lenX):
        for y in range(lenY):
            for x in range(lenX):                               # Basically just moves evrery element to the bottom
                with suppress(IndexError):
                    if lst[y+1][x]==lst[y][x] and lst[y][x]!=0:
                        lst[y+1][x]=lst[y][x]*2; lst[y][x]=0

                    elif lst[y][x] != 0 and lst[y+1][x]==0:
                        lst[y+1][x]=lst[y][x]; lst[y][x]=0       

    del x, y
    return lst

elif key=='up arrow':
    for _ in range(lenY*lenX):
        for y in range(lenY):
            for x in range(lenX):
                with suppress(IndexError):
                    if y!=0:
                        if lst[y-1][x]==lst[y][x] and lst[y][x]!=0:
                            lst[y-1][x]=lst[y][x]*2; lst[y][x]=0

                        elif lst[y][x] != 0 and lst[y-1][x]==0:
                            lst[y-1][x]=lst[y][x]; lst[y][x]=0

    del x, y
    return lst

elif key=='right arrow':
    for _ in range(lenY*lenX):
        for y in range(lenY):
            for x in range(lenX):
                with suppress(IndexError):
                    if lst[y][x+1]==lst[y][x] and lst[y][x]!=0: 
                        lst[y][x+1]=lst[y][x]*2; lst[y][x]=0

                    elif lst[y][x] != 0 and lst[y][x+1]==0:
                        lst[y][x+1]=lst[y][x]; lst[y][x]=0
    del x, y
    return lst

elif key=='left arrow':
    for _ in range(lenY*lenX):
        for y in range(lenY):
            for x in range(lenX):
                with suppress(IndexError):
                    if x!=0:
                        if lst[y][x-1]==lst[y][x] and lst[y][x]!=0:
                            lst[y][x-1]=lst[y][x]*2; lst[y][x]=0

                        elif lst[y][x] != 0 and lst[y][x-1]==0:
                            lst[y][x-1]=lst[y][x]; lst[y][x]=0
    del x, y
    return lst

else:
    print('No match for: ',key)
    return lst

The list Syntax for Xlist:

e.g.

[[0, 8, 0], [4, 0, 0], [2, 0, 4]]

What am I making wrong? Thanks in Andvance!

(I didn't put the Whole Code In this Question because it's long and stackoverflow didn't really accept it)

sophros
  • 14,672
  • 11
  • 46
  • 75
Nilusink
  • 47
  • 1
  • 5
  • But how would i stop this? I need lst to have the same content as Xlist. – Nilusink Dec 20 '20 at 18:53
  • 1
    Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – MisterMiyagi Dec 20 '20 at 18:54

0 Answers0