Cannot pass in parameter between classes despite being defined

Question:

I am a beginner programmer writing a chess program that makes use of multiple classes.

One class, the chessboard class, defines some variables that are used through out my program. The relevant ones are board and pieceCaptured.

class chessboard(
):  #creates a chessboard visualised through a 2D list. This list will determine the positions and types of pieces on the board
    def __init__(self):
      self.board = [
            ['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
            ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
            ['--', '--', '--', '--', '--', '--', '--', '--'],
            ['--', '--', '--', '--', '--', '--', '--', '--'],
            ['--', '--', '--', '--', '--', '--', '--', '--'],
            ['--', '--', '--', '--', '--', '--', '--', '--'],
            ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
            ['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR'],
        ]
      self.whiteTurn = True
      self.moveLog = []
      self.pieceCaptured = [] #list of all the pieces that have been captured

The makeMove class makes the move input by the user (I am aware of the inefficiency of my code).

  • coords is a list of the two squares that the user clicks on to move a piece from one square to the next.
class move():
  def makeMove(self, coords, board, pieceCaptured):
    x = board.board[coords[0][0]][coords[0][1]]
    if board.board[coords[1][0]][coords[1][1]] == '--' and x != '--':
      board.board[coords[0][0]][coords[0][1]] = board.board[coords[1][0]][coords[1][1]]
      board.board[coords[1][0]][coords[1][1]] = x
      self.capture = False #a piece has not been captured
    elif board.board[coords[1][0]][coords[1][1]] != '--': #if a piece has been captured
      pieceCaptured.append(board.board[coords[1][0]][coords[1][1]]) #adds the captured piece to the list
      self.capture = True #a piece has been captured
      board.board[coords[1][0]][coords[1][1]] = board.board[coords[0][0]][coords[0][1]]
      board.board[coords[0][0]][coords[0][1]] = '--'
    board.moveLog.append([coords[0], coords[1]]) #adds the move to the list

the validMoves function uses a list of moves, returned in the legalMoves function, and checks whether they will result in the king being put in check. Only this section of the code should be relevant.

class defineMoves():
  def validMoves(self, coords, whiteTurn, board, pieceCaptured): #return all of the valid moves that take into account whether the king will be put in check or not
    #1. return a list of all the possible moves
    moveList = self.legalMoves(coords, whiteTurn, board)
    #2. make every move possible
    for l in range(len(moveList)-1, -1, -1): #iterates backwards through the move list
      #moveList[l] = coords
      move.makeMove(moveList[l], board, pieceCaptured) <-- PROBLEM HERE

When running the line ‘move.makeMove(moveList[l], board, pieceCaptured)’, which runs the makeMove function, this error is returned:

TypeError: move.makeMove() missing 1 required positional argument: ‘pieceCaptured’

Manually adding self as a parameter does not solve the problem.
Other than that I cannot think what could be causing the problem as I have clearly brought in the parameter where needed and in the correct location.

For context, the chessboard class is used in a different module.

c = chess.chessboard()

Also, validMoves is defined and run in a different module.


validMoves = m.validMoves(coords, c.whiteTurn, c.board, c.pieceCaptured)
if coords in validMoves:
     move.makeMove(coords, c, c.pieceCaptured)

Any help on why the error occurs would be appreciated.

Asked By: bigpython

||

Answers:

the problem is that you call the class function without making an instance of it. That’s why the class also needs the "self" parameter. You can create an instance as follows.

move_instance = move()
move_instance.makeMove(moveList[l], board, pieceCaptured)

or

move().makeMove(moveList[l], board, pieceCaptured)
Answered By: Melon Pie