def initEdges(self): """ For the game state, figures out the possible moves, and creates edges from it Also creates empty nodes for the next level down """ moveList = self.gameState.getPossibleMoves(self.board) if(self.model != None): movListWeights = self.model.getMovePreferenceList( self.gameState, moveList) print(movListWeights) self.edges = [Edge(self, x) for x in moveList] for i, edge in enumerate(self.edges): resultGameState = moveList[i].apply(self.gameState) edge.targetState = resultGameState edge.target = None # otherwise, will recursively create every possible game state from here # additions to MCTS tp save python-chess type board-state final_move = I2S(moveList[i].startLoc) + I2S(moveList[i].endLoc) final_move = chess.Move.from_uci(final_move) self.board.push(final_move) edge.boardState = copy.deepcopy(self.board) self.board.pop() self.policyWeights = [DEFAULT_POLICY_WEIGHT for x in self.edges]
def mcts_one_match(self): new_state = GameState.getInitialState() new_node = MCTS.Node(self.mcts, parent=None, gameState=new_state, move=None) examples = [] # Debug i = 0 board = chess.Board() while True: actual_state = new_state actual_node = new_node # Running the exploration step of MCTS. for s in range(self.mcts_simulations_per_state): self.mcts.search(actual_node) examples.append( [actual_node.move, self.mcts.P[actual_state], None]) best_move = actual_node.select_best_child().move # I am just keeping track of the board state using python-chess if (i % 20 == 0): print(board.fen()) i += 1 print(i, best_move) final_move = I2S(best_move.startLoc) + I2S(best_move.endLoc) final_move = chess.Move.from_uci(final_move) board.push(final_move) new_state = deepcopy(actual_state) new_state = best_move.apply(new_state) new_node = MCTS.Node(self.mcts, parent=actual_node, gameState=new_state, move=best_move) # Debug for end game if board.result() != "*": if self.winner is None: self.result = board.result() if self.result == '1-0': print("White winner.") elif self.result == '0-1': print("Black winner.") else: print("Draw.. Chicken dinner not served..") return board
def tensorToMove(tensor): """ Used on the output of the neural network to translate it to a (legal?) move """ # Thought: should we make an actual Move from this? Or is that unnecessary? retval = [None, None] tensorS = tensor[:66] tensorE = tensor[66:] startRaw = tensorS.argmax() endRaw = tensorE.argmax() if (startRaw > 63): retval[0] = "cast" else: retval[0] = I2S((startRaw / 8, startRaw % 8)) if (endRaw > 63): retval[1] = "cast" else: retval[1] = I2S((endRaw / 8, endRaw % 8)) return retval
def constructFromPgnHalfmove(cls, gameState, Piece, Rank, File, Endloc, Promotion): # remember: order is (file)(rank) (files are letters, ranks are numbers) endLoc = S2I(Endloc) if Piece == '': Piece = PieceType.PAWN else: matchingTypes = [ x for x in PIECELABELS.keys() if PIECELABELS[x][0] == Piece ] Piece = matchingTypes[0] if Promotion != '': matchingTypes = [ x for x in PIECELABELS.keys() if PIECELABELS[x][0] == Promotion ] Promotion = matchingTypes[0] # figure out what f*****g piece is moving possibleMovers = gameState.getPiecesOfColor(Piece, gameState.turn) candidates = [] for candidate in possibleMovers: mask = MoveSquares.makeMoveMask(Piece, gameState.turn, candidate, gameState) if mask[endLoc]: candidates.append(candidate) if len(candidates) > 1: # if piece ambiguity if File != '': candidates = [x for x in candidates if I2S(x)[0] == File] if Rank != '': candidates = [x for x in candidates if I2S(x)[1] == Rank] if len(candidates) != 1: # pdb.set_trace() #raise ValueError("Still have piece ambiguity") raise BadGameParseException( "Some sort of piece ambiguity or emptiness") startLoc = candidates[0] retval = Move(startLoc, endLoc, None, Promotion) return retval
def log_move(self, move): # change the state of python-chess board in parallel with our gameState. final_move = I2S(move.startLoc) + I2S(move.endLoc) final_move = chess.Move.from_uci(final_move) self.board.push(final_move)
def log_move(self, move): # Portion of code after the move is made. final_move = I2S(move.startLoc) + I2S(move.endLoc) final_move = chess.Move.from_uci(final_move) self.board.push(final_move)
def __repr__(self): if self.castle != None: retval = "<Move C:%s>" % self.castle.name else: retval = "<Move %s->%s>" % (I2S(self.startLoc), I2S(self.endLoc)) return retval