I have written a Chess Engine Program in python in which one of my python file is responsible for the finding the Computer Moves . This uses MinMax algorithm with alpha-beta pruning and some other features. The move finding python file looks something like this :
def findBestMove():
minMax()
return BestMove
This file also contain some other functions like ScoreBoard and Openings. I have a separate file where I have implemented my move making. So I call this function in that file.
Now this MinMax function takes up a lot of time so I decided to use multiprocessing library in python . All the examples that I saw had the code structure like this :
def f(args):
#Do Something
if __name__ == '__main__':
p = Process(target = f , args = ())
p.start()
p.join()
In my program I want to apply multiprocessing to my MinMax Function but am not able to do it .
I tried something like this :
def findBestMove(gs, validMoves):
global nextMove
nextMove = None
p = Process(target = findMoveNegaMaxAlphaBeta, args=(gs, validMoves, DEPTH, -CHECKMATE, CHECKMATE, 1 if gs.whiteToMove else -1))
p.start()
p.join()
return nextMove
def findMoveNegaMaxAlphaBeta(gs, validMoves, depth, alpha, beta, turnMultiplier):
global nextMove
if depth == 0 :
return turnMultiplier * scoreBoard(gs)
maxScore = -CHECKMATE
for move in validMoves :
gs.makeMove(move)
nextMoves = gs.getValidMoves()
score = -findMoveNegaMaxAlphaBeta(gs, nextMoves, depth - 1 , -beta, -alpha, -turnMultiplier)
if score > maxScore:
maxScore = score
if depth == DEPTH :
nextMove = move
print("Lol")
gs.undoMove()
if maxScore > alpha: # This is were pruning happens
alpha = maxScore
if alpha >= beta :
break
return maxScore
When I run the code this "Lol" statement gets printed but the overall findBestMove function returns None which means that the program calls my findMoveNegaMaxAlphaBeta function and also reaches till the point when a value is assigned to the NextMove but that value does not change in the findBestMove function even when Next Move is a global variable. How do I get to return the BestMove which my minmax function finds but is not returned in the findBestMove function.