Exemplo n.º 1
0
 def __str__(self):
     self_str = "{0}({1}/{2}/{3}): [".format(self.name, self.win_by_draw,
                                             self.win, self.lose)
     for card in self.wang_list:
         self_str += "{0} ".format(card)
     for card in self.tube_list:
         self_str += "{0} ".format(card)
     for card in self.bamb_list:
         self_str += "{0} ".format(card)
     for card in self.word_list:
         self_str += "{0} ".format(card)
     for card in self.wind_list:
         self_str += "{0} ".format(card)
     self_str = "{0}]".format(self_str)
     if len(self.flow_list) > 0:
         self_str += " / [ "
         for card in self.flow_list:
             self_str += "{0} ".format(card)
         self_str += "]"
     else:
         self_str += " / []"
     if len(self.pong_list) > 0:
         self_str += " / [ "
         for card in self.pong_list:
             self_str += "{0} ".format(card)
         self_str += "]"
     else:
         self_str += " / []"
     if self.win_card:
         self_str += " -> {0}".format(self.win_card)
     else:
         prewin_tiles = GameBoard.PreWinTiles(self)
         if len(prewin_tiles) > 0:
             self_str += " / 聽 {0}".format(toCListStr(prewin_tiles))
     return self_str
Exemplo n.º 2
0
    def draw(self, keep=False):
        card = self.gb.drawCard()
        #print "\tGeniusAgent draw: {0}".format(card)
        prewin_tiles = GameBoard.PreWinTiles(self)
        if card in prewin_tiles:
            self.gb.win_agent = self
            self.win_card = card
            self.win_by_draw += 1
            #print("\t[Test] Agent({0}) 自摸 {1}!".format(self.name, card))
            return

        #print("\t[Test] {0} draw {1}...".format(self.name, card))
        ctype = GameBoard.CardType(card)
        if ctype == 1:
            if self.wang_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.wang_list.append(card)
            self.wang_list.sort()
            self.card_count += 1
        elif ctype == 2:
            if self.tube_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.tube_list.append(card)
            self.tube_list.sort()
            self.card_count += 1
        elif ctype == 3:
            if self.bamb_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.bamb_list.append(card)
            self.bamb_list.sort()
            self.card_count += 1
        elif ctype == 4:
            if self.word_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.word_list.append(card)
            self.word_list.sort()
            self.card_count += 1
        elif ctype == 5:
            if self.wind_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.wind_list.append(card)
            self.wind_list.sort()
            self.card_count += 1
        else:
            self.flow_list.append(card)
            self.flow_list.sort()
            return self.draw()

        dcard = self.drop()
        return dcard
Exemplo n.º 3
0
 def _isPrewin(self):
     prewin_tiles = GameBoard.PreWinTiles(self)
     if len(prewin_tiles) > 0:
         for tile in prewin_tiles:
             ctype = GameBoard.CardType(tile)
             if ctype == 1:
                 if tile in self.gb.wang_list:
                     return True
             elif ctype == 2:
                 if tile in self.gb.tube_list:
                     return True
             elif ctype == 3:
                 if tile in self.gb.bamb_list:
                     return True
             elif ctype == 4:
                 if tile in self.gb.word_list:
                     return True
             elif ctype == 5:
                 if tile in self.gb.wind_list:
                     return True
Exemplo n.º 4
0
 def _isPrewin(self):
     """ Check if entering pre-win(ready hand) status."""
     prewin_tiles = GameBoard.PreWinTiles(self)
     if len(prewin_tiles) > 0:
         for tile in prewin_tiles:
             ctype = GameBoard.CardType(tile)
             if ctype == 1:
                 if tile in self.gb.wang_list:
                     return True
             elif ctype == 2:
                 if tile in self.gb.tube_list:
                     return True
             elif ctype == 3:
                 if tile in self.gb.bamb_list:
                     return True
             elif ctype == 4:
                 if tile in self.gb.word_list:
                     return True
             elif ctype == 5:
                 if tile in self.gb.wind_list:
                     return True
Exemplo n.º 5
0
    def draw(self, keep=False):
        """ Draw card. (Required API by Agent)
        Gameboard will call this API to informat agent to draw card. Here should be responsible for:
        1. Call API:drawCard() from GameBoard object to draw card.
        2. Add drawn card into hand.
        3. Return card as dropped card. (In general, you will call API:drop() for dropped card)

        Different from the extended class, SmartAgent has its' own algorithm in drawing card:
        1. 使用 self.gb.drawCard() 從牌桌抽一張牌.
        2. 檢查是否滿足 Goal State (自摸).
           2.1 如果已經聽牌, 則打出抽到的牌.
        3. 檢查是否可以槓
           3.1 如果槓, 可以再抽一張.
        4. 選擇要放棄的牌.
        * Return:
          Dropped card."""
        card = self.gb.drawCard()
        prewin_tiles = GameBoard.PreWinTiles(self)
        if card in prewin_tiles:
            #if GameBoard.GoalState(self, card): # Check goal state
            self.gb.win_agent = self
            self.win_card = card
            self.win_by_draw += 1
            self.dprint("\t[Test] Agent({0}) 自摸 {1}!".format(self.name, card))
            return
        elif len(prewin_tiles) > 0:
            self.pwin_flag = True
            for tile in prewin_tiles:
                ctype = GameBoard.CardType(tile)
                if ctype == 1:
                    if tile in self.gb.wang_list:
                        return card
                elif ctype == 2:
                    if tile in self.gb.tube_list:
                        return card
                elif ctype == 3:
                    if tile in self.gb.bamb_list:
                        return card
                elif ctype == 4:
                    if tile in self.gb.word_list:
                        return card
                elif ctype == 5:
                    if tile in self.gb.wind_list:
                        return card

        self.dprint("\t[Test] {0} draw {1}...".format(self.name, card))
        ctype = GameBoard.CardType(card)
        if ctype == 1:
            if self.wang_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.wang_list.append(card)
            self.wang_list.sort()
            self.card_count += 1
        elif ctype == 2:
            if self.tube_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.tube_list.append(card)
            self.tube_list.sort()
            self.card_count += 1
        elif ctype == 3:
            if self.bamb_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.bamb_list.append(card)
            self.bamb_list.sort()
            self.card_count += 1
        elif ctype == 4:
            if self.word_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.word_list.append(card)
            self.word_list.sort()
            self.card_count += 1
        elif ctype == 5:
            if self.wind_list.count(card) == 3:  # 確認槓牌
                self._kong(ctype, card)
                return self.draw()
            self.wind_list.append(card)
            self.wind_list.sort()
            self.card_count += 1
        else:
            self.flow_list.append(card)
            self.flow_list.sort()
            return self.draw()

        dcard = None
        if not keep:
            dcard = self.drop()
            self.dprint("\t[Test] {0} drop {1}...".format(self.name, dcard))
            #self.gb.disCard(self, dcard)
        if (len(self.word_list) % 3 + len(self.wind_list) % 3 +
                len(self.tube_list) % 3 + len(self.wang_list) % 3 +
                len(self.bamb_list) % 3) == 0:
            self.wrong = True
        return dcard