示例#1
0
    def playGame(self):

        # sets up first two rounds
        self.setUp()

        winner = None
        count = 0
        while (True):
            count += 1
            if count > 100:
                break
            for player in self.players:
                roll = random.randint(1, 6)
                roll2 = random.randint(1, 6)
                sum = roll + roll2

                for vertex in self.built_vertices:
                    for tile in vertex.tiles:
                        if tile.probability == sum:
                            for i in range(0, vertex.settlementType):
                                vertex.player.drawResourceCard(tile.type)
                while (True):
                    mcts = mcts(timeLimit=1000)

                    action = mcts.search(self)

                    if action is None:
                        break
                    else:
                        self.takeAction(action)

                if player.victoryPoints >= 10:
                    winner = player
                    break
        return winner
示例#2
0
def main():
    # generate_random_graph(50, 0.08)
    state = CliqueState()
    while not state.isTerminal():
        best_action = mcts.search(initialState=state)
        state = state.takeAction(best_action)
    draw_graph(G, state.clique_set)
    print(state)
示例#3
0
def mctree(mcts, state):
    curState = state
    numMoves = 0
    visited = []
    while (True):
        visited.append(curState)
        if curState.isTerminal():
            break
        action = mcts.search(initialState=curState)
        curState = curState.takeAction(action)
        mcts.add(curState)

        numMoves += 1

    return numMoves
示例#4
0
        for row in self.board:
            s += " ".join([{0: "#", 1: "X", -1: "O"}[x] for x in row]) + "\n"
        return s


class Action():
    def __init__(self, player, x, y):
        self.player = player
        self.x = x
        self.y = y

    def __str__(self):
        return str((self.x, self.y))

    def __repr__(self):
        return str(self)

    def __eq__(self, other):
        return self.__class__ == other.__class__ and self.x == other.x and \
    self.y == other.y and self.player == other.player

    def __hash__(self):
        return hash((self.x, self.y, self.player))


if __name__ == "__main__":
    from mcts import mcts
    initialState = NaughtsAndCrossesState()
    mcts = mcts(iterationLimit=10000)
    action = mcts.search(initialState=initialState)
示例#5
0
            elif counter > 10:
                raise RuntimeError("counter was too big")
            else:
                return False

    def getReward(self):
        return constant.prediction(self.draft, self.radiant)


if __name__ == '__main__':
    heropicks = {2: 1, 5: -1, 75: 1, 76: -1}
    draft = constant.createDraft(heropicks)
    radiant = []
    dire = []
    initialState = DraftState(draft, True)
    initialState.banned.append(3)
    print(initialState.banned)
    mcts = mcts(timeLimit=10000)
    print(initialState.draft)
    # note top5 is most likely not accounted for atm
    action, top5 = mcts.search(initialState)
    print(action)
    print("recomended hero: {0}".format(constant.getDisplayName(action)))
    initialState.takeActualAction(action)
    if initialState.getPossibleActions().__contains__(3):
        print("ban contained in possible actions")
    constant.getDisplayName(initialState.draft)
    print("top5")
    for hero in top5:
        print(constant.getDisplayName(hero))
示例#6
0
    def playGame(self):
        # sets up first two rounds
        winner = None
        count = 0
        while (True):
            count += 1
            if count > 100:
                break

            print(count)

            # goes through a round
            for player in self.players:
                roll = random.randint(1, 6)
                roll2 = random.randint(1, 6)
                sum = roll + roll2

                # gives resources to players
                self.distributeCards()

                # Finds the current player
                for player in self.players:
                    if (player.index == self.whoseTurn):
                        curPlayer = player
                        playerNum = player.index

                # if the current player is random player
                if (curPlayer.name == "Random"):
                    while (True):
                        print("Random making moves")
                        action = curPlayer.chooseAction(self)
                        if action is None:
                            break
                        else:
                            self.takeAction(action)

                        if (curPlayer.index == 1):
                            if (self.firstPlayerScore >= 10):
                                winner = curPlayer.name
                                break

                        elif (curPlayer.index == 2):
                            if (self.secondPlayerScore >= 10):
                                winner = curPlayer.name
                                break
                        elif (curPlayer.index == 3):
                            if (self.thirdPlayerScore >= 10):
                                winner = curPlayer.name
                                break

                # if current player is Monte Carlo player
                elif (curPlayer.name == "MonteCarlo"):
                    while (True):
                        print("MonteCarlo making moves")

                        mcts = mcts(timeLimit=1000)

                        action = mcts.search(self)

                        if action is None:
                            break
                        else:
                            self.takeAction(action)

                        if (curPlayer.index == 1):
                            if (self.firstPlayerScore >= 10):
                                winner = curPlayer.name
                                break

                        elif (curPlayer.index == 2):
                            if (self.secondPlayerScore >= 10):
                                winner = curPlayer.name
                                break
                        elif (curPlayer.index == 3):
                            if (self.thirdPlayerScore >= 10):
                                winner = curPlayer.name
                                break
                else:
                    break
        return winner
示例#7
0
def mctsMove(game, depth):
    initialState = ChessMCTS(game)
    move = mcts.search(initialState=initialState).move
    #print(move.move)
    return move