def getMove(self): currentPlayer = self.board.currentPlayer myPieces = self.board.calculateActivePieces(currentPlayer) allLegals = self.board.calculateLegalMoves(myPieces, self.board) self.highestSeenValue = -sys.maxsize self.lowestSeenValue = sys.maxsize for myMoves in allLegals: makeMove = Move(self.board, myMoves[1], myMoves[0]) newboard = makeMove.createNewBoard() if newboard is not False: if currentPlayer == "White": self.currentValue = self.min(newboard, self.depth) else: self.currentValue = self.max(newboard, self.depth) if currentPlayer == "White" and self.currentValue > self.highestSeenValue: self.highestSeenValue = self.currentValue self.bestMove = newboard if currentPlayer == "Black" and self.currentValue < self.lowestSeenValue: self.lowestSeenValue = self.currentValue self.bestMove = newboard return self.bestMove
def predictOkMove(self): self.nnThinking = True myPieces = self.firstBoard.calculateActivePieces(self.firstBoard.currentPlayer) allLegals = self.firstBoard.calculateLegalMoves(myPieces, self.firstBoard) #boardString = ' '.join(str(x) for x in self.firstBoard.getBoardArrSide()) allBoardStrings = "" validMoves = [] for myMoves in allLegals: makeMove = Move(self.firstBoard, myMoves[1], myMoves[0]) newboard = makeMove.createNewBoard() if newboard is not False: validMoves.append(makeMove) boardString = ' '.join(str(x) for x in newboard.getBoardArrSide()) allBoardStrings += boardString + "\n" # sideResult = subprocess.check_output( # ["python", "C:\\Users\\Jack\\Documents\\GitHub\\ChessInPy\\PyChess\\chessNN\\predictOkMoveScript.py", # boardString]) # sideToString = sideResult.decode("utf-8").replace("\r", "").replace("\n", "") # print(sideToString) #print(allBoardStrings) sideResult = subprocess.check_output( ["python", "C:\\Users\\Jack\\Documents\\GitHub\\ChessInPy\\PyChess\\chessNN\\predictOkMoveScript.py", allBoardStrings]) sideToString = sideResult.decode("utf-8").replace("\r", "").replace("\n", "") print(sideToString) arr = list(sideToString) currentP = self.firstBoard.currentPlayer square = [] for i in range(len(arr)): if currentP == "White": if arr[i] == "0": print(validMoves[i].movedPiece.toString(), validMoves[i].destination) if validMoves[i].destination not in square: square.append(validMoves[i].destination) elif currentP == "Black": if arr[i] == "1": print(validMoves[i].movedPiece.toString(), validMoves[i].destination) if validMoves[i].destination not in square: square.append(validMoves[i].destination) # inputs = allBoardStrings.split('\n') # for i in range(len(inputs)-1): # print(inputs[i]) #print(allBoardStrings) self.colorGoodMoves(square) self.nnThinking = False
def getMove(self): #nBoard = Board() #nBoard.printBoard() currentPlayer = self.board.currentPlayer print(currentPlayer) myPieces = self.board.calculateActivePieces(currentPlayer) allLegals = self.board.calculateLegalMoves(myPieces, self.board) print(allLegals) bestMove = None highestSeenValue = -1000000 lowestSeenValue = 1000000 currentValue = None print(self.depth) for myMoves in allLegals: makeMove = Move(self.board, myMoves[1], myMoves[0]) newboard = makeMove.createNewBoard() if not newboard == False: if currentPlayer == "White": #print("Work w") currentValue = self.min(newboard, self.depth) #print(currentValue) else: print("Work b") currentValue = self.max(newboard, self.depth) print(currentValue) if currentPlayer == "White" and currentValue > highestSeenValue: highestSeenValue = currentValue bestMove = newboard if currentPlayer == "Black" and currentValue < lowestSeenValue: lowestSeenValue = currentValue bestMove = newboard #test# print("---------------------------------\nEl mayor valor es:" + highestSeenValue.__str__()) print("El menor valor es:" + lowestSeenValue.__str__()) #print("jugador actual es: "+currentPlayer) #bestMove.printBoard() return bestMove
def getMove(self): #nBoard = Board() #nBoard.printBoard() currentPlayer = self.board.currentPlayer #print(currentPlayer) myPieces = self.board.calculateActivePieces(currentPlayer) allLegals = self.board.calculateLegalMoves(myPieces, self.board) #print(allLegals) bestMove = None highestSeenValue = -1000000 lowestSeenValue = 1000000 currentValue = None #print(self.depth) for myMoves in allLegals: makeMove = Move(self.board, myMoves[1], myMoves[0]) newboard = makeMove.createNewBoard() if not newboard == False: if currentPlayer == "White": #print("Work w") currentValue = self.min(newboard, self.depth) #print(currentValue) else: #print("Work b") currentValue = self.max(newboard, self.depth) #print(currentValue) if currentPlayer == "White" and currentValue > highestSeenValue: highestSeenValue = currentValue bestMove = newboard if currentPlayer == "Black" and currentValue < lowestSeenValue: lowestSeenValue = currentValue bestMove = newboard return bestMove
def min(self, board, depth): # TODO checkmate/stalemate if depth == 0 and not Move.checkCheckmateOrStalemate( board, board.currentPlayer): return self.boardEvaluator.evaluate(board, depth) lowestSeenValue = sys.maxsize myPieces = board.calculateActivePieces(board.currentPlayer) allLegals = board.calculateLegalMoves(myPieces, board) for myMoves in allLegals: makeMove = Move(self.board, myMoves[1], myMoves[0]) newboard = makeMove.createNewBoard() if not newboard == False: value = self.min(newboard, depth - 1) if value <= lowestSeenValue: lowestSeenValue = value return lowestSeenValue
def getNextMove(self, board, alpha, beta, level): # return if depth reached if (level >= self.maxLevel): boardValue = self.boardEvaluator.evaluate(self.player, board, level) return Node(board, boardValue) # explore childe nodes currentPlayer = board.currentPlayer # get all posible moves allPieces = board.calculateActivePieces(currentPlayer) allLegalMoves = board.calculateLegalMoves(allPieces, board) selectedNode = None maxEva = -100000000 minEva = 100000000 for move in allLegalMoves: newMove = Move(board, move[1], move[0]) newboard = newMove.createNewBoard() # extra check for move validity if not isinstance(newboard, bool): minmaxNode = self.getNextMove(newboard, alpha, beta, level + 1) if (minmaxNode == None): continue newNode = Node(newboard, minmaxNode.value) if (self.isMax(level)): selectedNode = self.getMaxNode(selectedNode, newNode) maxEva = max(maxEva, selectedNode.value) alpha = max(alpha, maxEva) if (beta <= alpha): # print("prunned") break else: selectedNode = self.getMinNode(selectedNode, newNode) minEva = min(minEva, selectedNode.value) beta = min(beta, minEva) if (beta <= alpha): # print("prunned") break return selectedNode
def max(self, board, depth): #TODO checkmate/stalemate if depth == 0: #print("Hello") #return 99 return self.boardEvaluator.evaluate(board, depth) #self.min(board, depth-1) highestSeenValue = -1000000 myPieces = board.calculateActivePieces(board.currentPlayer) allLegals = board.calculateLegalMoves(myPieces, board) for myMoves in allLegals: makeMove = Move(self.board, myMoves[1], myMoves[0]) newboard = makeMove.createNewBoard() if not newboard == False: value = self.min(newboard, depth-1) if value >= highestSeenValue: highestSeenValue = value return highestSeenValue
theMove = moveDes if legal == False: allPieces[selectedImage][1][0] = prevx allPieces[selectedImage][1][1] = prevy else: allPieces[selectedImage][1][0] = allSqParams[theMove][0] allPieces[selectedImage][1][1] = allSqParams[theMove][2] # TODO make it so it updates board # TODO update moved piece's legal moves some how # print(allPieces[selectedImage][2]) # print(theMove) # print(firstBoard) thisMove = Move(firstBoard, allPieces[selectedImage][2], theMove) newBoard = thisMove.createNewBoard() if not newBoard == False: firstBoard = newBoard # else: # print(newBoard) #firstBoard.printBoard() # TODO update game pieces newP = updateChessPieces() allPieces = newP #print(len(newP)) #print(firstBoard.currentPlayer) currentPlayer = newBoard.currentPlayer
def __init__(self): pygame.init() self.gameDisplay = pygame.display.set_mode((800, 800)) pygame.display.set_caption("PyChess") self.clock = pygame.time.Clock() self.firstBoard = chessBoard.Board() self.firstBoard.createBoard() self.allTiles = [] self.allPieces = [] self.currentPlayer = self.firstBoard.currentPlayer self.allSqParams = self.createSqParams() self.drawChessPieces() self.selectedImage = None self.selectedLegals = None self.resetColors = [] self.quitGame = False self.mx, self.my = pygame.mouse.get_pos() self.prevx, self.prevy = [0, 0] self.aiBoard = False while not self.quitGame: for event in pygame.event.get(): # HANDLE QUIT GAME if event.type == pygame.QUIT: self.quitGame = True pygame.quit() quit() # HANDLE SELECTING PIECE AND THEIR LEGAL MOVES # ONLY WHEN NO PIECE IS SELECTED YET if event.type == pygame.MOUSEBUTTONDOWN and not self.aiBoard: if self.selectedImage == None: self.mx, self.my = pygame.mouse.get_pos() for piece in range(len(self.allPieces)): if self.allPieces[piece][2].alliance == self.currentPlayer: if self.allPieces[piece][1][0] < self.mx < self.allPieces[piece][1][0] + 100: if self.allPieces[piece][1][1] < self.my < self.allPieces[piece][1][1] + 100: self.selectedImage = piece self.prevx = self.allPieces[piece][1][0] self.prevy = self.allPieces[piece][1][1] self.selectedLegals = self.allPieces[self.selectedImage][2].calculateLegalMoves( self.firstBoard) for legals in self.selectedLegals: self.resetColors.append([legals, self.allTiles[legals][0]]) if self.allTiles[legals][0] == (66, 134, 244) or self.allTiles[legals][ 0] == (29, 81, 39): self.allTiles[legals][0] = (135, 46, 40) else: self.allTiles[legals][0] = (183, 65, 56) # HANDLE DRAGGING PIECE if event.type == pygame.MOUSEMOTION and not self.selectedImage == None and not self.aiBoard: self.mx, self.my = pygame.mouse.get_pos() self.allPieces[self.selectedImage][1][0] = self.mx - 50 self.allPieces[self.selectedImage][1][1] = self.my - 50 # HANDLE LETTING GO ON PIECE TO THEIR MOVED SPOT if event.type == pygame.MOUSEBUTTONUP and not self.aiBoard: if not self.selectedImage == None: for resets in self.resetColors: self.allTiles[resets[0]][0] = resets[1] # self.resetColors.remove(resets) try: pieceMoves = self.allPieces[self.selectedImage][2].calculateLegalMoves(self.firstBoard) legal = False theMove = 0 for moveDes in pieceMoves: if self.allSqParams[moveDes][0] < self.allPieces[self.selectedImage][1][0] + 50 < \ self.allSqParams[moveDes][1]: if self.allSqParams[moveDes][2] < self.allPieces[self.selectedImage][1][1] + 50 < \ self.allSqParams[moveDes][3]: legal = True theMove = moveDes if legal == False: self.allPieces[self.selectedImage][1][0] = self.prevx self.allPieces[self.selectedImage][1][1] = self.prevy else: self.allPieces[self.selectedImage][1][0] = self.allSqParams[theMove][0] self.allPieces[self.selectedImage][1][1] = self.allSqParams[theMove][2] thisMove = Move(self.firstBoard, self.allPieces[self.selectedImage][2], theMove) newBoard = thisMove.createNewBoard() if not newBoard == False: self.firstBoard = newBoard newP = self.updateChessPieces() self.allPieces = newP self.currentPlayer = newBoard.currentPlayer thread = threading.Thread(target=self.miniMaxMove, args=[]) thread.start() except: pass self.prevy = 0 self.prevx = 0 self.selectedImage = None # HANDLE DRAWING TILES EACH FRAME for info in self.allTiles: pygame.draw.rect(self.gameDisplay, info[0], info[1]) # HANDLE DRAWING PIECES EACH FRAME for img in self.allPieces: self.gameDisplay.blit(img[0], img[1]) # GAME LOOP MANAGEMENT pygame.display.update() self.clock.tick(60)