简体   繁体   中英

Update List of objects in chess game in Python

I am trying to update a list containing the chessboard pieces, which are objects.

When a piece can be move to the location, I can append it to the list, but I need to delete the self and the taken piece (or substitute it)

in particular the remove command it does not seem to find the piece in the list, B[1].remove(Rook(self.pos_x, self.pos_y, self.side_))

the pop method would require another for loop to slice by index, can someone suggest a better way?


def can_move_to(self, pos_X: int, pos_Y: int, B: Board) -> bool:
            '''
            checks if this rook can move to coordinates pos_X, pos_Y
            on board B according to all chess rules
            Hints:
            - firstly, check [Rule2] and [Rule4] using can_reach
            - secondly, check if result of move is capture using is_piece_at
            - if yes, find the piece captured using piece_at
            - thirdly, construct new board resulting from move
            - finally, to check [Rule5], use is_check on new board
             '''
            if self.can_reach (pos_X,pos_Y,B):#finds if rook can move to location returns true or false
                if is_piece_at(pos_X,pos_Y,B):#finds if it there is another piece at this location
                 if piece_at(pos_X,pos_Y,B).side_!=self.side_:#check if the piece at location is different side
                     #for piece in range (len (B[1])):#check all the pieces in the board
                      for piece in B[1]:
                          if piece==piece_at(pos_X,pos_Y,B):
                              print("I am here now--------------------------", piece)

                              B[1].append(Rook(pos_X, pos_Y, self.side_))
                              B[1].remove(piece)
                              B[1].remove(Rook(self.pos_x, self.pos_y, self.side_))

         #                    my_move= is_check(self.side_, B)
         #                    return ("you are here",my_move)

            else:
                return False
            for x in B[1]:
                print (x)
            print(B)

It is impossible that this instruction:

B[1].remove(Rook(self.pos_x, self.pos_y, self.side_))

succeeds.

Here you create a new Rook object and you try to remove it from the list. This can not succeed since the object has just been created and cannot thus not be part of the list.

If self is the rook that is moving why not just replace:

B[1].append(Rook(pos_X, pos_Y, self.side_))
B[1].remove(piece)
B[1].remove(Rook(self.pos_x, self.pos_y, self.side_))

by:

B[1].remove(piece)  # remove the piece that was at (pos_X, pos_Y)
B[1].remove(self)   # remove the Rook
self.pos_x = pos_X  # update the Rook position
self.pos_y = pos_Y
B[1].append(self)   # put back the Rook in the list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM