Exemplo n.º 1
0
    def __init__(self):
        # 表示するWindow名
        self.window_name = "Ostle"
        # ディスプレイ設定
        self.display_updater = DisplayUpdater(self.window_name)
        # コールバックの設定
        self.mouse_observer = MouseObserver(self.window_name)

        self.board_manager = BoardManager()
Exemplo n.º 2
0
 def Exists(self):
     if self._type == TYPE_DIR:
         return True
     board = BoardManager.GetBoardByIndex(self._index + 1)
     if board is None:
         return False
     return True
Exemplo n.º 3
0
def Init():
    Config.LoadConfig()
    BCache.Init()
    BoardManager.Init()
    UCache.Init()
    Utmp.Init()
    return
Exemplo n.º 4
0
 def Init(self):
     Config.LoadConfig()
     BCache.Init()
     BoardManager.Init()
     UCache.Init()
     commondata.CommonData.Init()
     self.fast_indexer_state = fast_indexer.State()
     self.fast_indexer = fast_indexer.FastIndexer(self.fast_indexer_state)
     self.fast_indexer.daemon = True
     self.fast_indexer.start()
Exemplo n.º 5
0
 def GetInfo(self, index, user):
     rfb = {}
     rfb['index'] = index
     rfb['father'] = self._father
     if self.IsBoard():
         rfb['type'] = 'board'
         board = BoardManager.GetBoardByIndex(self._index + 1)
         if board != None:
             rfb['binfo'] = board.GetInfoWithUser(user)
     else:
         rfb['type'] = 'dir'
         rfb['name'] = self._name
     return rfb
    def __init__(self, queens, initialPopulation, iterations, mutationProbability, numberOfIndividues):
 
        print "Parametros:"
        print "Numero de reinas         ", queens
        print "Poblacion inicial        ", initialPopulation
        print "Iteraciones              ", iterations
        print "Probabilidad de mutacion ", mutationProbability
        print "Torneo de                ", numberOfIndividues
        
        print "\nSoluciones\n "
        global initialTime
        initialTime = clock()
        
        self.__queens = queens
        self.__initialPopulation = initialPopulation
        self.__iterations = iterations
        self.__mutationProbability = mutationProbability
        self.__numberOfIndividues = numberOfIndividues
        self.__boardManager = BoardManager(self.__queens, self.__initialPopulation)
        self.__operator = EvolutionaryOperators()
class EvolutionaryAlgorithm:

    def __init__(self, queens, initialPopulation, iterations, mutationProbability, numberOfIndividues):
 
        print "Parametros:"
        print "Numero de reinas         ", queens
        print "Poblacion inicial        ", initialPopulation
        print "Iteraciones              ", iterations
        print "Probabilidad de mutacion ", mutationProbability
        print "Torneo de                ", numberOfIndividues
        
        print "\nSoluciones\n "
        global initialTime
        initialTime = clock()
        
        self.__queens = queens
        self.__initialPopulation = initialPopulation
        self.__iterations = iterations
        self.__mutationProbability = mutationProbability
        self.__numberOfIndividues = numberOfIndividues
        self.__boardManager = BoardManager(self.__queens, self.__initialPopulation)
        self.__operator = EvolutionaryOperators()
        
    def runAlgorithm(self):
        for i in range(self.__iterations):
            fathers = self.__boardManager.getFathers(self.__numberOfIndividues)
            sons = self.__operator.geneticCross(fathers[0],fathers[1])
            for son in sons :
                self.__boardManager.insertBoard(son)
            if self.__operator.isThereMutation(self.__mutationProbability):
                board = self.__boardManager.extractBoard(random.randint(0,len(self.__boardManager.getBoards())-1)) 
                self.__boardManager.insertBoard(self.__operator.geneticMutation(board))
        
    
    def getSolutions(self):
        return self.__boardManager.getSolutions()
        
Exemplo n.º 8
0
 def LoadFavBoards(self):
     path = User.User.OwnFile(self._userid, "favboard")
     self._current = -1
     fd = open(path, "rb")
     if fd != None:
         magic = Util.ReadInt(fd)
         if magic != 0x8080:
             self._count = magic
             index = 0
             while index < self._count:
                 bindex = Util.ReadInt(fd)
                 self._favboards[index] = FavBoard(bindex)
                 index = index + 1
         else:
             self._count = Util.ReadInt(fd)
             index = 0
             while index < self._count:
                 flag = Util.ReadInt(fd)
                 title = ''
                 if flag == -1:
                     length = Util.ReadChar(fd)
                     title = Util.gbkDec(Util.CString(fd.read(length)))
                 father = Util.ReadInt(fd)
                 self._favboards[index] = FavBoard(flag, title, father)
                 index = index + 1
         fd.close()
     if self._count <= 0:
         fd = open(Config.BBS_ROOT + "etc/initial_favboard", "r")
         if fd == None:
             self._count = 1
             self._favboards[0] = FavBoard(0)
         else:
             self._count = 1
             self._favboards[0] = FavBoard(0)
             while True:
                 board = Util.ReadString(fd)
                 if board == '':
                     break
                 bobj = BoardManager.GetBoard(board)
                 if bobj != None:
                     self._favboards[self._count] = FavBoard(bobj.index - 1)
             fd.close()
     else:
         count = self._count
         index = 0
         while index < self._count:
             fboard = self._favboards[index]
             if fboard.IsDir():
                 index = index + 1
                 continue
             bindex = fboard._index
             board = BoardManager.GetBoardByIndex(bindex + 1)
             user = UserManager.LoadUser(self._userid)
             if ((bindex >= 0) and (bindex <= BCache.GetBoardCount())
                     and (user != None)
                     and (board != None)
                     and (board.CheckSeePerm(user))):
                 index = index + 1
                 continue
             self.DelFavBoard(index)
             index = index + 1
         if count != self._count:
             self.SaveFavBoards()
Exemplo n.º 9
0
class GameManager:
    def __init__(self):
        # 表示するWindow名
        self.window_name = "Ostle"
        # ディスプレイ設定
        self.display_updater = DisplayUpdater(self.window_name)
        # コールバックの設定
        self.mouse_observer = MouseObserver(self.window_name)

        self.board_manager = BoardManager()

    def run(self):

        self.display_updater.update_board(self.board_manager.state)

        player = 1

        while True:
            self.__select_piece_phase(player)
            self.display_updater.update_board(self.board_manager.state)
            winner = self.judge_winner()
            if winner > 0:
                print("player", winner, "is win!!!")
                break

            if player == 1:
                player = 2
            else:
                player = 1

        cv2.destroyAllWindows()
        print("Finished")

    def __select_piece_phase(self, player):

        while True:
            cv2.waitKey(60)
            coord = self.mouse_observer.getCoord()

            if coord[0] is not None:
                pos = (int(coord[1] / 36), int(coord[0] / 36))
                self.display_updater.draw_mark(pos)

            # 左クリックがあったら表示
            if self.mouse_observer.getEvent() == cv2.EVENT_LBUTTONDOWN:
                if self.board_manager.is_own_piece(pos, player) is True:
                    if self.__select_direction_phase(pos) is True:
                        return

    def __select_direction_phase(self, mark_pos):

        # 今のマスから移動可能な方向を調べる
        move_dict = self.board_manager.ask_move_direction(mark_pos)
        if move_dict is not None:
            self.display_updater.draw_direction(move_dict)
        else:
            print("そのコマは移動できません")
            return

        while True:
            cv2.waitKey(60)

            coord = self.mouse_observer.getCoord()
            if coord[0] is not None:
                direct_pos = (int(coord[1] / 36), int(coord[0] / 36))
                direct = [k for k, v in move_dict.items() if v == direct_pos]
                self.display_updater.emphasis_direction_img(move_dict, direct)

            # 左クリックがあったらコマを移動
            if self.mouse_observer.getEvent(
            ) == cv2.EVENT_LBUTTONDOWN and len(direct) != 0:
                if self.board_manager.is_hole(mark_pos) is True:
                    self.board_manager.move_hole(mark_pos, direct[0])
                else:
                    self.board_manager.move_piece(mark_pos, direct[0])
                return True

            # 右クリックがあったら終了
            elif self.mouse_observer.getEvent() == cv2.EVENT_RBUTTONDOWN:
                print("push right click : __select_direction_phase")
                return False

    def judge_winner(self):
        winner = 0
        player1_count = self.board_manager.ask_piece_count(1)
        player2_count = self.board_manager.ask_piece_count(2)

        if player1_count <= 3:
            winner = 2
        if player2_count <= 3:
            winner = 1
        return winner