Not sure what's wrong with my chess Ai Minimax Algorithm - Running to Max recursion depth

99 Views Asked by At

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
0

There are 0 best solutions below