May-24-2023, 05:12 PM
Is there anyway I can make my minimax algorithm stronger? (For a tic-tac-toe game.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
def minimax(mark, square, alpha = - 1000 , beta = 1000 , depth = 0 ): """minimax algorithm with alpha/beta pruning""" # Place the mark and check for a win square.mark = mark if square.check_win(): # Give extra weight to earlier wins/losses score = 10 - depth if mark is ROBOT else depth - 10 board.depth = min (board.depth, depth) elif len (empty_squares : = board.empty_squares()) = = 0 : # No plays left. Draw. score = 0 elif mark is PLAYER: # Pick best move for robot score = - 1000 for s in empty_squares: score = max (score, minimax(ROBOT, s, alpha, beta, depth + 1 )) alpha = max (alpha, score) if alpha > beta: break else : # Guess what move player will make score = 1000 for s in empty_squares: score = min (score, minimax(PLAYER, s, alpha, beta, depth + 1 )) beta = min (beta, score) if alpha > beta: break # Remove mark and return score for the square square.mark = EMPTY return score |