I'm in the very early stages of my chess implementation and I'm working on how to move the pawn. The problem is, I can't figure out what is going on with the list slicing!
This is my variable board
:
board = [['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
['-', '-', '-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-', '-', '-'],
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']]
which looks like :
R N B Q K B N R
P P P P P P P P
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
p p p p p p p p
r n b q k b n r
So, I'm starting to loop through the pieces to find acceptable moves for the pawn by executing the following:
for i in range(1):
for j in range(1):
if board[i][j].lower() == 'p':
p_moves(board,i,j)
where p_moves
is:
def p_moves(board_state, row, col):
valid_moves = []
#copy the board to make modifications to the new one and store it
new = board_state[:]
#find if the space ahead of it is blank
if board_state[row+1][col] == "-":
#use a throwaway value just to test the function and we can see easily what changed
new[row+1][col] = "Z"
#clear where the piece was
new[row][col] = "-"
#add the board to the list of possible moves
valid_moves.append(new)
return valid_moves
What ends up happening is the new
board doesn't seem to clear each time the function is called, which I don't get why because I'm slicing it as a new variable and it has a different ID in memory than board_state, so I'm not really sure what's going on here.
To clarify, after looping through the top (I haven't even got to considering the lower half - how is the smallest piece so painful?!), the next possible board states ends up looking like:
R N B Q K B N R
- - - - - - - -
Z Z Z Z Z Z Z Z
- - - - - - - -
- - - - - - - -
- - - - - - - -
p p p p p p p p
r n b q k b n r
Thanks for any help.