R. Reed
R. Reed

Reputation: 146

Why is my chess algorithm producing seemingly random moves?

I am using a simple minimax/alpha-beta pruning algorithm to create a chess "AI", but it keeps producing the same moves to start, no matter the move I make. For instance, white plays e4, black (AI) plays a6, white plays d4, black plays Ra7, and then move the rook back and forth no matter what I do. (depth is 2 currently)

def evaluate(set, to_move): # the "set" is the list i am working from - pop_board does not get committed to the board
    eval = 0
    for i in range(8):
        for x in range(8): # looping through the current grid
            if set[x][i] != "A":
                if not set[x][i].hidden:
                    if set[x][i].colour == "W":
                        final_pos(set[x][i]).count
                        if x < 4 and i <4 and x>3 and i>3: #if the piece is in the middle of the board
                            eval += 50
                        if set[x][i].type == "P":
                            eval += 100
                        elif set[x][i].type == "N" or set[x][i].type == "B":
                            eval += 300
                        elif set[x][i].type == "R":
                            eval += 500
                        elif set[x][i].type == "Q":
                            eval += 900
                    if set[x][i].colour == "B":
                        if x < 4 and i <4 and x>3 and i>3: #if the piece is in the middle of the board
                            eval -= 50
                        if set[x][i].type == "P":
                            eval -= 100
                        elif set[x][i].type == "N" or set[x][i].type == "B":
                            eval -= 300
                        elif set[x][i].type == "R":
                            eval -= 500
                        elif set[x][i].type == "Q":
                            eval -= 900
    eval = eval * to_move
    return eval

def minimax(depth, board, moving, alpha, beta):
    best_move = None
    if depth == 0:
        return evaluate(board, moving), None
    max = -math.inf
    for i in getAllMoves(board): #gets a list of pieces
        for move in i[0]: #for every move in the piece's moves
            pop_board = copy.deepcopy(board) #deepcopying the board
            pop_board[move[0]][move[1]] = i[1] #making the move
            pop_board[i[1].x][i[1].y] = "A"
            score = -minimax( depth - 1, pop_board, moving*-1, -beta, -alpha)[0]#
            if score > max:
                best_move= i[1], move
                max = score
            if alpha >= beta:
                break
    return max, best_move

Upvotes: 0

Views: 171

Answers (1)

eligolf
eligolf

Reputation: 1856

It seems like your program plays the first move it gets in your possible move list, i.e. it doesn't find a better move when going through the list in your minimax function.

What does this line do in your code? pop_board[i[1].x][i[1].y] = "A" Otherwise after a quick look the negamax code looks okay.

Since it never finds a better move I would guess you get the wrong evaluation from your evaluation function. Do some prints and see if it makes sense.

Upvotes: 0

Related Questions