Exemplo n.º 1
0
    def pong(self, agent, ctype, count, card):
        """ Pong callback. (Required API by Agent)
        This API will be called by Gameboard object when Gameboard detect you being able to pong.
        When you decide to pong, you should handle pong and call API:draw() from Gameboard to draw another card and return dropped card.
        If you don't want to pong, just return None or do nothing.
        * Argument
          agent - The Agent which dropped card.
          ctype - Cared type of dropped card.
          count - 2=Pong/3=Kong.
          card - Dropped card which you can pong/kong.
        * Return
          Dropped card if pong/kong; None to give up pong/kong."""
        if GameBoard.GoalState(self, card):  # Check goal state
            self.gb.win_agent = self
            self.win_card = card
            self.win += 1
            agent.close += 1
            self.dprint("\t[Test] Agent({0}) 碰胡 {1}!!".format(self.name, card))
            return
        #if self._isPrewin():
#    return
# Greedy algorithm: Always pong!
        if ctype == 1:
            return self._pong(self.wang_list, count, card)
        elif ctype == 2:
            return self._pong(self.tube_list, count, card)
        elif ctype == 3:
            return self._pong(self.bamb_list, count, card)
        elif ctype == 4:
            return self._pong(self.word_list, count, card)
        elif ctype == 5:
            return self._pong(self.wind_list, count, card)
Exemplo n.º 2
0
 def eat(self, agent, card, ctype, eat_list):
     """ Eat callback. (Required API by Agent)
     This API will be called by Gameboard when it detect you being able to eat card dropped from previous agent(上家).
     When you decide to eat, a dropped card from hand should be returned. Otherwise return None or do nothing.
     * Argument
       agent - Agent to dropped the card
       card - Dropped card
       ctype - Card type of dropped card
       eat_list - Tuple(card in hand to generate the sequence by dropped card, sequence formed by dropped card) list.
     * Return
       Dropped card if decide to eat; None to give up chance to eat."""
     if GameBoard.GoalState(self, card):  # Check goal state
         self.gb.win_agent = self
         self.win_card = card
         self.win += 1
         agent.lose += 1
         self.dprint("\t[Test] Agent({0}) 吃胡 {1}!".format(self.name, card))
         return
     if self._isPrewin():
         return
     # Greedy algorithm: Always eat from the first choice
     if ctype == 1:
         return self._eat(self.wang_list, eat_list[0][0], eat_list[0][1])
     elif ctype == 2:
         return self._eat(self.tube_list, eat_list[0][0], eat_list[0][1])
     elif ctype == 3:
         return self._eat(self.bamb_list, eat_list[0][0], eat_list[0][1])
Exemplo n.º 3
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)
        * Return:
          Dropped card."""
        card = self.gb.drawCard()
        if GameBoard.GoalState(self, card):  # Check goal state
            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:
            self.wang_list.append(card)
            self.wang_list.sort()
            self.card_count += 1
        elif ctype == 2:
            self.tube_list.append(card)
            self.tube_list.sort()
            self.card_count += 1
        elif ctype == 3:
            self.bamb_list.append(card)
            self.bamb_list.sort()
            self.card_count += 1
        elif ctype == 4:
            self.word_list.append(card)
            self.word_list.sort()
            self.card_count += 1
        elif ctype == 5:
            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()
            #print("\t[Test] {0} drop {1}...".format(self.name, dcard))
            #self.gb.disCard(self, dcard)
        return dcard
Exemplo n.º 4
0
    def pong(self, agent, ctype, count, card):
        if GameBoard.GoalState(self, card):  # Check goal state
            self.gb.win_agent = self
            self.win_card = card
            self.win += 1
            agent.close += 1
            print("\t[Test] Agent({0}) 碰胡 {1}!!".format(self.name, card))
            return

        # Greedy algorithm: Always pong!
        if ctype == 1:
            return self._pong(self.wang_list, count, card)
        elif ctype == 2:
            return self._pong(self.tube_list, count, card)
        elif ctype == 3:
            return self._pong(self.bamb_list, count, card)
        elif ctype == 4:
            return self._pong(self.word_list, count, card)
        elif ctype == 5:
            return self._pong(self.wind_list, count, card)
Exemplo n.º 5
0
 def eat(self, agent, card, ctype, eat_list):
     if GameBoard.GoalState(self, card):  # Check goal state
         self.gb.win_agent = self
         self.win_card = card
         self.win += 1
         agent.lose += 1
         print("\t[Test] Agent({0}) 吃胡 {1}!".format(self.name, card))
         return
     # Greedy algorithm: Always eat from the first choice
     print('\t[Info] {0}'.format(self))
     print('\t[Info] Eat {0} has option(s):'.format(card))
     if ctype == 1:
         #if len(eat_list)==1: return self._eat(card, self.wang_list, eat_list[0][0], eat_list[0][1])
         for i in range(len(eat_list)):
             print("\t{0}:{1} ".format(i + 1, toCListStr(eat_list[i][1])))
         choice_opt = input('\t: ')
         if len(choice_opt) > 0:
             return self._eat(card, self.wang_list,
                              eat_list[int(choice_opt) - 1][0],
                              eat_list[int(choice_opt) - 1][1])
     elif ctype == 2:
         #if len(eat_list)==1: return self._eat(card, self.tube_list, eat_list[0][0], eat_list[0][1])
         for i in range(len(eat_list)):
             print("\t{0}:{1} ".format(i + 1, toCListStr(eat_list[i][1])))
         choice_opt = input('\t: ')
         if choice_opt == 'n' or choice_opt == 'N':
             return
         if len(choice_opt) > 0:
             return self._eat(card, self.tube_list,
                              eat_list[int(choice_opt) - 1][0],
                              eat_list[int(choice_opt) - 1][1])
     elif ctype == 3:
         #if len(eat_list)==1: return self._eat(card, self.bamb_list, eat_list[0][0], eat_list[0][1])
         for i in range(len(eat_list)):
             print("\t{0}:{1} ".format(i + 1, toCListStr(eat_list[i][1])))
         choice_opt = input('\tChoose 1-{0}: '.format(len(eat_list)))
         if len(choice_opt) > 0:
             return self._eat(card, self.bamb_list,
                              eat_list[int(choice_opt) - 1][0],
                              eat_list[int(choice_opt) - 1][1])
Exemplo n.º 6
0
    def draw(self, keep=False):
        card = self.gb.drawCard()
        if GameBoard.GoalState(self, card):  # Check goal state
            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 = None
        if not keep:
            dcard = self.drop()
            print("\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 != 2:
            self.wrong = True
        return dcard
Exemplo n.º 7
0
    def idraw(self):
        card = self.gb.drawCard()
        dctype = GameBoard.CardType(card)
        if GameBoard.GoalState(self, card):  # Check goal state
            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

        if dctype > 5:
            self.flow_list.append(card)
            self.flow_list.sort()
            return self.idraw()
        print("\t[Info] {0}: You draw {1}...".format(self, card))

        # 確認槓牌
        if dctype == 1:
            if self.wang_list.count(card) == 3:  # 確認槓牌
                kong_opt = input('\t[Info] Kong {0} (y/n): '.format(card))
                if len(kong_opt) > 0 and (kong_opt == 'y' or kong_opt == 'Y'):
                    self._kong(dctype, card)
                    return self.idraw()
        elif dctype == 2:
            if self.tube_list.count(card) == 3:  # 確認槓牌
                kong_opt = input('\t[Info] Kong {0} (y/n): '.format(card))
                if len(kong_opt) > 0 and (kong_opt == 'y' or kong_opt == 'Y'):
                    self._kong(dctype, card)
                    return self.idraw()
        elif dctype == 3:
            if self.bamb_list.count(card) == 3:  # 確認槓牌
                kong_opt = input('\t[Info] Kong {0} (y/n): '.format(card))
                if len(kong_opt) > 0 and (kong_opt == 'y' or kong_opt == 'Y'):
                    self._kong(dctype, card)
                    return self.idraw()
        elif dctype == 4:
            if self.word_list.count(card) == 3:  # 確認槓牌
                kong_opt = input('\t[Info] Kong {0} (y/n): '.format(card))
                if len(kong_opt) > 0 and (kong_opt == 'y' or kong_opt == 'Y'):
                    self._kong(dctype, card)
                    return self.idraw()
        elif dctype == 5:
            if self.wind_list.count(card) == 3:  # 確認槓牌
                kong_opt = input('\t[Info] Kong {0} (y/n): '.format(card))
                if len(kong_opt) > 0 and (kong_opt == 'y' or kong_opt == 'Y'):
                    self._kong(dctype, card)
                    return self.idraw()

        # choose drop card
        dcard = self._chooseDropCard()

        if dcard:
            if dctype == 1:
                self.wang_list.append(card)
                self.wang_list.sort()
                self.card_count += 1
            elif dctype == 2:
                self.tube_list.append(card)
                self.tube_list.sort()
                self.card_count += 1
            elif dctype == 3:
                self.bamb_list.append(card)
                self.bamb_list.sort()
                self.card_count += 1
            elif dctype == 4:
                self.word_list.append(card)
                self.word_list.sort()
                self.card_count += 1
            elif dctype == 5:
                self.wind_list.append(card)
                self.wind_list.sort()
                self.card_count += 1
            print("\t[Test] {0} drop card={1}...".format(self.name, dcard))
            #self.gb.disCard(self, dcard)
            return dcard
        else:
            print("\t[Test] {0} drop draw card={1}...".format(self.name, card))
            #self.gb.disCard(self, card)
            return card