Nov-04-2018, 03:04 AM
Context: game of checkers, populating list of possible moves, but if player must make a jump, non-jumping moves should be removed from the list.
Problem description: I have a block of code which iterates over a list of tuples, each three items long. It checks if the last item is a zero. If so, it should remove that tuple from the list. However, it seems to only remove some such items. The zeros are integers, not strings.
Output from print statements:
[('f8', 'g7', 0), ('h8', 'g7', 0), ('e7', 'f6', 0), ('b6', 'c5', 0), ('b6', 'a5', 0), ('d6', 'c5', 0), ('h6', 'g5', 0), ('e5', 'c3', 'd4')]
0
removing ('f8', 'g7', 0)
0
removing ('e7', 'f6', 0)
0
removing ('b6', 'a5', 0)
0
removing ('h6', 'g5', 0)
[('h8', 'g7', 0), ('b6', 'c5', 0), ('d6', 'c5', 0), ('e5', 'c3', 'd4')]
The process works for a few moves into the game, but then this happens. Above, it appears that it does not see the three remaining offenders, as if they are added later, but nothing new happens before the last print statement.
I am stumped.
Problem description: I have a block of code which iterates over a list of tuples, each three items long. It checks if the last item is a zero. If so, it should remove that tuple from the list. However, it seems to only remove some such items. The zeros are integers, not strings.
1 2 3 4 5 6 7 8 9 10 |
if jumps: # jumps is a boolean signaling the need to remove non-jump moves print (possible) # possible is list of allowed moves as tuples (starting square, end square, jumped square) for i in possible: print (i[ 2 ]) if i[ 2 ] = = 0 : print ( 'removing' , i) possible.remove(i) else : print ( 'not removed' , i) print (possible) |
[('f8', 'g7', 0), ('h8', 'g7', 0), ('e7', 'f6', 0), ('b6', 'c5', 0), ('b6', 'a5', 0), ('d6', 'c5', 0), ('h6', 'g5', 0), ('e5', 'c3', 'd4')]
0
removing ('f8', 'g7', 0)
0
removing ('e7', 'f6', 0)
0
removing ('b6', 'a5', 0)
0
removing ('h6', 'g5', 0)
[('h8', 'g7', 0), ('b6', 'c5', 0), ('d6', 'c5', 0), ('e5', 'c3', 'd4')]
The process works for a few moves into the game, but then this happens. Above, it appears that it does not see the three remaining offenders, as if they are added later, but nothing new happens before the last print statement.
I am stumped.