1

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.

Guillaume
  • 5,497
  • 3
  • 24
  • 42
user6142489
  • 512
  • 2
  • 10
  • 28
  • 1
    Slicing makes a shallow copy; it doesn't copy the sublists. – user2357112 Oct 13 '17 at 23:38
  • Ah gotcha - so guess I have to use deepcopy(). Given there will be a lot of boards expanded presumably, is this the best option for memory/speed or is there another route I should explore? – user6142489 Oct 13 '17 at 23:40
  • @user6142489 it's unclear what you're doing with the new board, but I'm certain there's a better route to explore! – Adam Smith Oct 13 '17 at 23:41
  • Thanks - this new board was just meant to be treated as a transitory state where I would evaluate it and then figure out among all possible moves, which to pick. – user6142489 Oct 13 '17 at 23:44
  • Consider actual chess notation. Producing [PGN](https://en.wikipedia.org/wiki/Portable_Game_Notation) lists of possible moves is probably both useful for your program and good practice for computer chess in general. – Adam Smith Oct 13 '17 at 23:46
  • This seems a very confusing way to implement possible moves. I was under the impression that you will want to save each possible board state in a separate array, so you have many boards that come about due to many moves. It seems you only have 1 which will not work once you start propagating moves. – Austin A Oct 13 '17 at 23:46

0 Answers0