Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Minimax algorithm
#1
Is there anyway I can make my minimax algorithm stronger? (For a tic-tac-toe game.)

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
Reply


Messages In This Thread
Minimax algorithm - by freethrownucleus - May-24-2023, 05:12 PM
RE: Minimax algorithm - by deanhystad - May-24-2023, 08:16 PM
RE: Minimax algorithm - by freethrownucleus - May-29-2023, 06:56 PM
RE: Minimax algorithm - by deanhystad - Jun-01-2023, 07:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020