0

As the title says, I've been running into issues with my code not running properly. It reaches maximum recursion depth, and after some debugging with print statements, it seems like it runs through the pawns possible moves, but then gets stuck on Rooks moves, repeating this set of values (included 1 repeat):

pos: 0 0
count 1
<chessPieces.Rook object at 0x115cc5610>
[]
pos: 7 0
[]
pos: 7 1
[]
pos: 3 4
[(0, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0)]
pos: 1 0
count 1
<chessPieces.Rook object at 0x115cc5610>
[]
pos: 7 0
[]
pos: 7 1
[]
pos: 3 4
[(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0)]
pos: 0 0
count 1
<chessPieces.Rook object at 0x115cc5610>
[]
pos: 7 0
[]
pos: 7 1
[]
pos: 3 4
[(0, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0)]

The current legal moves for each piece is the list of tuples that is printed (which for some reason is sometimes an empty list even though it seems to make a move). Here is my code below:

def makeMinimaxMove(app,isMax,depth):
    score, pieceMoved, move = minimax(app,isMax,depth,None,None,0)

    newLoc = findPiece(app,move[0],move[1])
    app.pieceSelected = pieceMoved
    moveToSelection(app,newLoc,move[0],move[1])

def minimax(app,isMax,depth,pieceMoved,move,count): #board just pieces so can be found w app
    #print(depth)
    curBoard = copy.copy(app.pieces)
    score = evaluate(app)

    if score >= 900: #if maximizer captured opponents King
        return score, pieceMoved, move #not sure how to pass move and piece thru

    elif score <= -900: #if minimizer captured opponents King
        return score, pieceMoved, move

    elif depth <= 0:
        print('depth exceeded')
        return score, pieceMoved, move

    if isMax:
        team = 'white'
        best = [-10000,None,None]
        for piece in app.pieces:
            if piece.team == team:
                findPossibleMoves(app,piece)
                for (row,col) in app.currentLegalMoves:
                    count += 1
                    print('count',count)
                    curBoard = copy.deepcopy(app.pieces)

                    move = (row,col)
                    newLoc = findPiece(app,row,col)
                    app.pieceSelected = piece
                    moveToSelection(app,newLoc,row,col)

                    score, pieceMoved, move = minimax(app,not isMax,depth-1,piece,move,count)

                    if score >= best[0]:
                        best = [score,pieceMoved,move] #update all characteristics of best
                    app.pieces = curBoard #undo move

    else:
        team = 'black'
        best = [10000,None,None]
        for piece in app.pieces:
            if piece.team == team:
                findPossibleMoves(app,piece)
                print(app.currentLegalMoves)
                print('pos:',piece.row,piece.col)
                for (row,col) in app.currentLegalMoves:
                    curBoard = copy.deepcopy(app.pieces)

                    count += 1
                    print('count',count)
                    move = (row,col)                    
                    newLoc = findPiece(app,row,col)
                    print(piece)
                    app.pieceSelected = piece
                    moveToSelection(app,newLoc,row,col)

                    score, pieceMoved, move = minimax(app,not isMax,depth-1,piece,move)
                    print('made it')
                    if score >= best[0]:
                        best = [score,pieceMoved,move] #update all characteristics of best
                    app.pieces = curBoard #undo move
                    #reset all other app instances too
AKapoor30
  • 13
  • 2
  • The fact that you have a functional error rather than a bug and that your problem (chess) and algorithm is fairly complex makes this a hard problem to debug. Rather than expecting someone to go through this code and find out why it's not working, it might be more reasonable to ask for advice on what kind of testing/debugging strategy would be best in this case. – Bill Apr 22 '22 at 04:14
  • 2
    It seems you do a lot of extra steps/functions that is not in the pseudo code found on e.g. Wikipedia. Try to first get all legal moves into a list, each move should e.g. be an object containing start pos and end pos (later also other useful information such as move type, piece moved etc). Then you loop over this list and in each step you have a function MakeMove(currentMove), score = Minimax(...), UnmakeMove(currentMove). I would also suggest trying Negamax instead which basically cuts your code in half and makes it lots easier to debug. – eligolf Apr 22 '22 at 04:34

0 Answers0