0

My program uses the minimax-algorithm to choose the most otimal move against the human player in tic-tac-toe. (this is working)

After that I tried to implement the alpha-beta-pruning to optimize the time the algorithm needs in order to get the optimal move. (this doesnt work)

I know why the Error is raised, but I dont get why the Error is raised by 'alpha' and 'beta' and not by 'board' or 'minX' or 'minY' too.

I think the Variables I declared should all be global variables, should they?

Error: UnboundLocalError: local Variable 'beta' referenced before assignment

#Global Variables
values = [0,1,2]
minX = 0
minY = 0
alpha = -999
beta = 999
global alpha
global beta
board = [["-" for x in range(3)] for y in range(3)] 


#ausgabe
def ausgabe():
  for zeile in range(3):
    for spalte in range(3):
      print("|"+str(board[zeile][spalte]),end="")
    print("|")


#auswertung
#liefert 0, wenn O gewonnen hat
#liefert 2, wenn X gewonnen hat
#liefert 1, wenn es unentschieden ausgeht
#liefert -1 wenn noch nicht klar
def auswertung():
  for i in ("X", "O"):
    for x in range(3):
      if board[x][0] == i and board[x][1] == i and board[x][2] == i:
        return i
  
    for y in range(3):
      if board[0][y] == i and board[1][y] == i and board[2][y] == i:
        return i

    if board[0][0] == i and board[1][1] == i and board[2][2] == i:
        return i
    if board[0][2] == i and board[1][1] == i and board[2][0] == i:
        return i

  for zeile in range(3):
    for spalte in range(3):
      if board[zeile][spalte] == "-":
        return -1
  return 1


#max
def max():
  temp = auswertung()
  if temp != -1:
    if temp == "X":
      return 2
    elif temp == "O":
      return 0
    else:
      return temp

  maximalwert = -999
  for x in range(3):
    for y in range(3):
      if board[x][y] == "-":
        board[x][y] = "X"
        temp = alpha
        alpha = beta
        beta = temp
        if alpha < beta:
          break
        wert = min()
        board[x][y] = "-"
        if wert > maximalwert:
          maximalwert = wert
      
      
        
  return maximalwert


#min
def min():
  temp = auswertung()
  if temp != -1:
    if temp == "X":
      return 2
    elif temp == "O":
      return 0
    else:
      return temp

  minimalwert = 999
  for x in range(3):
    for y in range(3):
      if board[x][y] == "-":
        board[x][y] = "O"
        temp = beta
        beta = alpha
        alpha = temp
        if alpha > beta:
          break
        wert = max()
        board[x][y] = "-"
        if wert < minimalwert:
          minimalwert = wert
  return minimalwert

#wo befindet sich das min
def minWo():
  temp = auswertung()
  if temp != -1:
    if temp == "X":
      return 2
    elif temp == "O":
      return 0
    else:
      return temp

  global minX
  global minY
  minimalwert = 999
  for x in range(3):
    for y in range(3):
      if board[x][y] == "-":
        board[x][y] = "O"
        temp = beta
        beta = alpha
        alpha = temp
        if alpha > beta:
          break
        wert = max()
        board[x][y] = "-"
        if wert < minimalwert:
          minX = x
          minY = y
      minimalwert = wert
      
      
        
  return minimalwert


def user_input():
  while True:
    try:
      number1 = int(input("number1: "))
      number2 = int(input("number2: "))
      if number1 in values and number2 in values:
        if board[number1][number2] == "-":
          return number1, number2
      else:
        print("Invalid Input!\ttry again")
    except ValueError:
      print("Invalid Input!\ttry again")


def game_still_going():
  winner = auswertung()
  if winner == "X" or winner == "O":
    print("Player "+winner+" won the game")
    return False
  elif winner == 1:
    print("It's a draw")
    return False
  return True


def play_game():
  while game_still_going():

    number1,number2 = user_input()
    board[number1][number2] = "X"
    minWo()
    board[minX][minY] = "O"
    ausgabe()

play_game()
Florian
  • 43
  • 1
  • 1
  • 5
  • I solved the mistake I had with the alpha-beta-pruning, but I still dont get, why the variables 'alpha' and 'beta' must be manually refered to, if they are global. – Florian Dec 04 '21 at 12:49

1 Answers1

1

You should set alpha respective beta to -999 and 999 instead of maximalwert, that should work. Look at the pseudo code for minimax.

eligolf
  • 1,682
  • 1
  • 6
  • 22