def expand(self):
        if (len(self.c) > 0):
            print("Ae bozebo")
            return  # already expanded
        status = TicTacToeStatic.Status(self.s)
        if (status != None):
            return

        moves = TicTacToeStatic.available_moves(self.s)
        NTWG = TicTacToeStatic.getNTW()
        PM = PrioritizeMoves(NTWG, len(self.s))
        PM_moves = PM.PrioritizeMoves(self.s, self.x, moves)

        if (moves != PM_moves):
            #print(moves,PM_moves)
            moves = PM_moves
        if (self.cnt <= 1000):
            moves = TicTacToeStatic.removecopies(self.s, moves)

        # print('--------------------')
        # ss= self.s.copy()
        # for move in moves:
        #     ss[move[0]][move[1]]=9
        # print(ss)
        #print(self.x, " [[[[[")
        #input()
        turn = self.x
        for move in moves:
            new_s = self.s.copy()
            r = move[0]
            c = move[1]
            new_s[r, c] = turn
            next = Node(new_s, self.cnt + 1, turn * -1, self)
            self.c.append(next)
示例#2
0
    def rollout(self):
        rand_play = RandomBot()
        cur_s = self.s
        turn = self.x

        while (TicTacToeStatic.Status(cur_s) == None):
            move = rand_play.make_a_move(cur_s)
            r = move // len(self.s)
            c = move % len(self.s)
            new_s = cur_s.copy()
            new_s[r, c] = turn
            cur_s = new_s
            turn *= -1

        e = TicTacToeStatic.Status(cur_s)

        return e
示例#3
0
    def expand(self):
        status = TicTacToeStatic.Status(self.s)
        if (status != None):
            return
        moves = TicTacToeStatic.available_moves(self.s)
        #if(self.cnt<=3):
        #    moves = TicTacToeStatic.removecopies(self.s,moves)

        turn = self.x
        for move in moves:
            new_s = self.s.copy()
            r = move[0]
            c = move[1]
            new_s[r][c] = turn
            next = Node(new_s, self.cnt + 1, turn * -1, None, (r, c))
            self.children.append(next)
        shuffle(self.children)
    def make_a_move(self, s, n_iter=100):
        s = np.array(s)
        length = len(s)
        cnt = 0
        for i in range(length):
            for j in range(length):
                if (s[i][j] != 0):
                    cnt += 1

        if (cnt == 0):
            return (len(s) // 2 - 1) * len(s) + (len(s) // 2 - 1)

        moves = TicTacToeStatic.available_moves(s)
        NTWG = TicTacToeStatic.getNTW()
        PM = PrioritizeMoves(NTWG, len(s))
        PM_moves = PM.PrioritizeMoves(s, self.x, moves)

        if (moves != PM_moves):
            #print(moves,PM_moves)
            moves = PM_moves
            #print(PM_moves," <---------------PM_moves")
            if (len(moves) == 1):
                print("!!!")
                a = moves[0]
                return a[0] * len(s) + a[1]

        root = Node(s, cnt, self.x)
        root.build_tree(n_iter)

        most = -1
        for child in root.c:
            if (child.N > most):
                most = child.N
                C = child

        r, c = 0, 0
        length = len(s)
        for i in range(length):
            for j in range(length):
                if (root.s[i, j] != C.s[i, j]):
                    r, c = i, j
        return r * len(s) + c
    def rollout(self):
        rand_play = RandomBot()
        cur_s = self.s
        turn = self.x
        lst = None
        cnt_now = self.cnt
        while (TicTacToeStatic.Status(cur_s, lst, cnt_now) is None):
            move = rand_play.make_a_move(cur_s)
            r = move // len(self.s)
            c = move % len(self.s)
            lst = r, c
            cnt_now += 1
            new_s = cur_s.copy()
            new_s[r, c] = turn
            cur_s = new_s
            turn *= -1

        e = TicTacToeStatic.Status(cur_s, lst, cnt_now)

        return e
示例#6
0
    def compute_score(self):
        status = TicTacToeStatic.Status(self.s)
        if (status != None):
            self.score = status
            return

        score1 = -1
        score2 = 1
        for child in self.children:
            if (self.x == 1):
                if (child.score > score1):
                    score1 = child.score
                if (child.score == 1):
                    break
            if (self.x == -1):
                if (child.score < score2):
                    score2 = child.score
                if (child.score == -1):
                    break

        if (self.x == 1):
            self.score = score1
        elif (self.x == -1):
            self.score = score2
    def make_a_move(self, s, n_iter=100):
        print("Daviwyet =================")
        s = np.array(s)
        length = len(s)
        cnt = 0
        for i in range(length):
            for j in range(length):
                if (s[i][j] != 0):
                    cnt += 1

        if (cnt == 0):
            return (len(s) // 2 - 1) * len(s) + (len(s) // 2 - 1)

        if (self.now == None):
            moves = TicTacToeStatic.available_moves(s)
            NTWG = TicTacToeStatic.getNTW()
            PM = PrioritizeMoves(NTWG, len(s))
            PM_moves = PM.PrioritizeMoves(s, self.x, moves)

            if (moves != PM_moves):
                #print(moves,PM_moves)
                moves = PM_moves
                #print(PM_moves," <---------------PM_moves")
                if (len(moves) == 1):
                    print("!!!")
                    a = moves[0]
                    return a[0] * len(s) + a[1]

            self.now = Node(s, cnt, self.x)
        else:
            print(s)
            print(self.now.s)
            a = 0
            b = 0
            A = 0
            B = 0
            for i in range(len(s)):
                for j in range(len(s)):
                    if (s[i][j] != self.now.s[i][j]):
                        if (self.now.s[i][j] == 0):
                            if (s[i][j] == self.x):
                                a, b = i, j
                            if (s[i][j] != self.x):
                                A, B = i, j

            self.now.s[a][b] = self.x

            NEXT = None
            flag = 0
            for child in self.now.c:
                if (np.array_equal(child.s, self.now.s)):
                    NEXT = child
                    #print("!!__!!")
                    break

            if (NEXT == None):
                self.now = Node(s, cnt, self.x)
                flag = 1
            else:
                self.now = NEXT
                print("!!!!!!=========!!!!!!!!!")

                print(self.now.s)
                print(a, b, A, B, self.x)

                self.now.s[A][B] = self.x * -1

                print(self.now.s)

                NEXT2 = None
                for child in self.now.c:
                    if (np.array_equal(child.s, self.now.s)):
                        NEXT2 = child
                        #print("!!_=_!!")
                        break

                if (NEXT2 != None):
                    self.now = NEXT2
                    print("!!!!!!!!!!!!!!!")
                else:
                    self.now = Node(s, cnt, self.x)
        print("bildavs")
        self.now.build_tree(n_iter)
        print("dabilda")

        most = -1
        for child in self.now.c:
            if (child.N > most):
                most = child.N
                C = child

        r, c = 0, 0
        length = len(s)
        for i in range(length):
            for j in range(length):
                if (self.now.s[i, j] != C.s[i, j]):
                    r, c = i, j
        return r * len(s) + c