0

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
  • 2
    FYI `eval` is a built-in name and it's bad practice to overwrite/shadow built-ins. Same with `max` and `set`. Also have you stepped through the code in a [debugger](https://www.jetbrains.com/help/pycharm/debugging-your-first-python-application.html) yet to see what's going on? – Random Davis Dec 03 '21 at 19:21
  • Yes, I have. Also, I have changed the eval, max and set. – ThatOneCoder Dec 03 '21 at 19:23
  • 1
    In that case I'd put more info in your post on what you found while stepping through line-by-line, explaining where the execution starts differing from your expectations. Any and all info you can provide is helpful. – Random Davis Dec 03 '21 at 19:29
  • 1
    How is your algorithm supposed to work? What moves do you expect it to play? If you don't have a specific reason to expect a particular output, your question just seems to be "my algorithm is worse than I thought it would be" and improving it is beyond the scope of Stack Overflow. – kaya3 Dec 03 '21 at 19:42
  • 1
    Unless I'm mistaken, the condition `x < 4 and i <4 and x>3 and i>3:` is never true, so your `evaluate` function just returns the total piece values. – user3386109 Dec 03 '21 at 21:00

1 Answers1

0

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.

eligolf
  • 1,682
  • 1
  • 6
  • 22