🤖 Alpha-Beta pruning
This commit is contained in:
@ -7,10 +7,10 @@ from . import fourInARowDraw
|
||||
from funcs import logThis
|
||||
|
||||
AIScores = {
|
||||
"middle": 1,
|
||||
"two in a row": 5,
|
||||
"middle": 3,
|
||||
"two in a row": 10,
|
||||
"three in a row": 50,
|
||||
"enemy two in a row": -25,
|
||||
"enemy two in a row": -35,
|
||||
"enemy three in a row": -200,
|
||||
"enemy win": -10000,
|
||||
"win": 420,
|
||||
@ -222,7 +222,7 @@ def fourInARowAI(channel):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
scores[column] = minimax(testBoard,4,player%2+1,player,False)
|
||||
scores[column] = minimax(testBoard,5,player%2+1,player,-math.inf,math.inf,False)
|
||||
logThis("Best score for column "+str(column)+" is "+str(scores[column]))
|
||||
|
||||
possibleScores = scores.copy()
|
||||
@ -295,7 +295,7 @@ def evaluateWindow(window,player,otherPlayer):
|
||||
else:
|
||||
return 0
|
||||
|
||||
def minimax(board, depth, player , originalPlayer, maximizingPlayer):
|
||||
def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer):
|
||||
terminal = ((isWon(board)[0] != 0) or (0 not in board[0]))
|
||||
if depth == 0 or terminal:
|
||||
points = AICalcPoints(board,originalPlayer)
|
||||
@ -306,7 +306,11 @@ def minimax(board, depth, player , originalPlayer, maximizingPlayer):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
value = max(value,minimax(testBoard,depth-1,player%2+1,originalPlayer,False))
|
||||
evaluation = minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,False)
|
||||
value = max(value,evaluation)
|
||||
alpha = max(alpha,evaluation)
|
||||
if beta <= alpha:
|
||||
break
|
||||
return value
|
||||
else:
|
||||
value = math.inf
|
||||
@ -314,6 +318,10 @@ def minimax(board, depth, player , originalPlayer, maximizingPlayer):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
value = min(value,minimax(testBoard,depth-1,player%2+1,originalPlayer,True))
|
||||
evaluation = minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,True)
|
||||
value = min(value,evaluation)
|
||||
beta = min(beta,evaluation)
|
||||
if beta <= alpha:
|
||||
break
|
||||
return value
|
||||
|
||||
|
Reference in New Issue
Block a user