def SearchBestMove(depth): if ChessBoard.WhiteToMove: BestMove=None MaxEval=-100000 moves = ListEveryLegalMove(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) for move in moves: SBoard,SCastle,SWhiteToMove=deepcopy(ChessBoard.Board),ChessBoard.Castle,ChessBoard.WhiteToMove ChessBoard.Move(move) Eval=Search(depth-1,10000,-10000,False) if Eval>MaxEval: MaxEval=Eval BestMove=move ChessBoard.Board,ChessBoard.Castle,ChessBoard.WhiteToMove=SBoard,SCastle,SWhiteToMove return BestMove else: BestMove=None MinEval=100000 moves = ListEveryLegalMove(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) for move in moves: SBoard,SCastle,SWhiteToMove=deepcopy(ChessBoard.Board),ChessBoard.Castle,ChessBoard.WhiteToMove ChessBoard.Move(move) Eval=Search(depth-1,-10000,10000,True) if Eval<MinEval: MinEval=Eval BestMove=move ChessBoard.Board,ChessBoard.Castle,ChessBoard.WhiteToMove=SBoard,SCastle,SWhiteToMove return BestMove
def Search(depth,alpha,beta,MaxPlayer): if MaxPlayer: moves = ListEveryLegalMove(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) if len(moves)==0: if ChessBoard.CheckForEndgameConditions=="Mate": return -100000 if ChessBoard.CheckForEndgameConditions=="Stalemate": return 0 if depth==0: ChessBoardAfterMove=normalize(ChessBoard.Board) ChessBoardAfterMove=tf.expand_dims(ChessBoardAfterMove,0) Value=model.predict(ChessBoardAfterMove)[0] return Value for move in moves: SBoard,SCastle,SWhiteToMove=deepcopy(ChessBoard.Board),ChessBoard.Castle,ChessBoard.WhiteToMove ChessBoard.Move(move) Train(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) Eval=-Search(depth-1,alpha,beta,False) ChessBoard.Board,ChessBoard.Castle,ChessBoard.WhiteToMove=SBoard,SCastle,SWhiteToMove alpha = max(alpha,Eval) if beta<=alpha: break return alpha else: moves = ListEveryLegalMove(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) if len(moves)==0: if ChessBoard.CheckForEndgameConditions=="Mate": return -100000 if ChessBoard.CheckForEndgameConditions=="Stalemate": return 0 if depth==0: ChessBoardAfterMove=normalize(ChessBoard.Board) ChessBoardAfterMove=tf.expand_dims(ChessBoardAfterMove,0) Value=model.predict(ChessBoardAfterMove)[0] return Value for move in moves: SBoard,SCastle,SWhiteToMove=deepcopy(ChessBoard.Board),ChessBoard.Castle,ChessBoard.WhiteToMove ChessBoard.Move(move) Train(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) Eval=-Search(depth-1,alpha,beta,True) ChessBoard.Board,ChessBoard.Castle,ChessBoard.WhiteToMove=SBoard,SCastle,SWhiteToMove beta = min(alpha,Eval) if beta>=alpha: break return beta
def EvaluateBestMove(): t1=time() LegalMoves=ListEveryLegalMove(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) MovesValues={} for move in LegalMoves: SBoard,SCastle,SWhiteToMove=deepcopy(ChessBoard.Board),ChessBoard.Castle,ChessBoard.WhiteToMove ChessBoard.Move(move) #check for mate in one if len(ListEveryLegalMove(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle))==0 and IsKingInCheck(ChessBoard.Board,ChessBoard.WhiteToMove): print('mate in one') ChessBoard.Board,ChessBoard.Castle,ChessBoard.WhiteToMove=SBoard,SCastle,SWhiteToMove return move ChessBoardAfterMove=normalize(ChessBoard.Board) ChessBoardAfterMove=tf.expand_dims(ChessBoardAfterMove,0) Train(ChessBoard.Board,ChessBoard.WhiteToMove,ChessBoard.Castle) Value=model.predict(ChessBoardAfterMove)[0] MovesValues[Value[0]]=move ChessBoard.Board,ChessBoard.Castle,ChessBoard.WhiteToMove=SBoard,SCastle,SWhiteToMove BestKey=max(MovesValues.keys()) if ChessBoard.WhiteToMove else min(MovesValues.keys()) print('Evaulation in ',time()-t1) return MovesValues[BestKey]
def EngineMove(): move = SearchBestMove(2) ChessBoard.Move(move)
from utility import IsThisStalemate, IsKingInCheck, ListEveryLegalMove from Engine import EvaluateBestMove, SearchBestMove from Chess import ChessBoard MoveCount = 0 def EngineMove(): move = SearchBestMove(2) ChessBoard.Move(move) while True: print("Move", MoveCount) MoveCount += 1 if MoveCount > 300: ChessBoard.setup() MoveCount = 0 if len( ListEveryLegalMove(ChessBoard.Board, ChessBoard.WhiteToMove, ChessBoard.Castle)) > 0: EngineMove() ChessBoard.debug() if ChessBoard.CheckForEndgameConditions == "Mate": MoveCount = 0 ChessBoard.setup() elif ChessBoard.CheckForEndgameConditions == "Stalemate": MoveCount = 0 ChessBoard.setup()
PieceSelectedPos = False OneMoveBackChessBoard = False while True: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() SquareClicked = [pos[0] // 100, pos[1] // 100] if PieceSelectedPos: if MoveValidator(PieceSelectedPos, SquareClicked, PieceSelectedType, ChessBoard.Board, ChessBoard.WhiteToMove, ChessBoard.Castle): OneMoveBackChessBoard = copy.deepcopy(ChessBoard.Board) move = (PieceSelectedType, PieceSelectedPos, SquareClicked) ChessBoard.Move(move) ChessBoard.debug() ChessBoard.CheckForEndgameConditions() backgroundreset() RedCheckSquare() PieceSelectedPos = False if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() SquareClicked = [pos[0] // 100, pos[1] // 100] if ChessBoard.Board[SquareClicked[1]][SquareClicked[0]] != ' ': PieceSelectedPos = SquareClicked PieceSelectedType = ChessBoard.Board[PieceSelectedPos[1]][ PieceSelectedPos[0]] backgroundreset() DrawLegalMoves() if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and ChessBoard.Board != OneMoveBackChessBoard and OneMoveBackChessBoard: