Search code examples
pythonclasschess

Python Classes: Appending a list & Incorporating a Method


I just started learning about Python classes today and I had a quick question. I'm pretty amazed how much more succinct it has made my code, but I'm trying to figure out if the following is possible for a chess problem I'm working on.

(1) Can I append a list somehow from within a class method? I'm trying to figure out if there's a way to accumulate the pieces in a list each time capture is called.

(2) How can I call a method from within the class to be used in another method? I would like to be able to check if a move is valid before even proceeding if the piece should try to capture another or move.

class Piece(Board):

def __init__(self, piece, r, c):
    self.piece = piece
    self.r = r
    self.c = c

This is the function I would like to incorporate into the functions below to avoid the redundancy (Question 2)

def valid_move(self,r,c,r_offset,c_offset):
    #r, c are integers for the coordinates on the board
    #r_offset,c_offset are the cells the piece might move to
    self.tgt_r, self.tgt_c = r+r_offset, c+c_offset

    if self.tgt_r <= 7 or self.tgt_c >= 0:
            return True
    return False

These functions are the same for now. I'm trying to see how I can use the capture function to accumulate a list of pieces once they're taken. (Question 1)

def capture(self,r,c, r_offset, c_offset): 

    piece = self.piece
    self.tgt_r, self.tgt_c = r+r_offset, c+c_offset 

    if self.tgt_r > 7 or self.tgt_c < 0:
            return None             
    else:
        nb = Board(curr).copy_board() #this board is just 8x8 np.array
        nb[self.tgt_r,self.tgt_c], nb[r,c] = piece,'-'  
    return nb

def move(self,r,c, r_offset, c_offset):

    piece = self.piece
    self.tgt_r, self.tgt_c = r+r_offset, c+c_offset 

    if self.tgt_r > 7 or self.tgt_c < 0:
        return None
    else:
        nb = Board(curr).copy_board()
        nb[self.tgt_r,self.tgt_c], nb[r,c] = piece,'-'  
    return nb

Thanks as always.


Solution

  • 1. Can I append a list somehow from within a class method?

    create a list -piecesList in your class for storing the pieces:

    class Piece(Board):
        def __init__(self, piece, r, c):
            self.piece = piece
            self.r = r
            self.c = c
            self.piecesList = [] #or init using some argument if you want to use some list from outside of the class
    

    and whenever your capture method is called, simply append the piece in the piecesList :

    def capture(self,r,c, r_offset, c_offset): 
        self.piecesList.append(self.piece)
        piece = self.piece
    

    2. How can I call a method from within the class to be used in another method?

    you can simply call it using self.method(arg1, arg2...) :

    def capture(self,r,c, r_offset, c_offset): 
    
        piece = self.piece
    
        if self.valid_move(r,c,r_offset,c_offset) == False:
                return None             
        else:
            nb = Board(curr).copy_board() #this board is just 8x8 np.array
            nb[self.tgt_r,self.tgt_c], nb[r,c] = piece,'-'  
        return nb