Exemplo n.º 1
0
    def autoProcessAddCard(self):
        player = self.table.addCardProcessor.getPlayer()
        nowTile = self.table.addCardProcessor.getTile()

        if (self.table.getTableConfig(MTDefine.ROBOT_LEVEL, MTDefine.ROBOT_SMART) == MTDefine.ROBOT_FOOLISH) and \
            player.isRobot():
            # AI是傻瓜级别,抓什么打什么
            self.table.dropTile(player.curSeatId, nowTile)
            return
        
        if self.table.addCardProcessor.getState() & MTableState.TABLE_STATE_HU:
            self.table.gameWin(player.curSeatId, nowTile)
            return
        
        exInfo = self.table.addCardProcessor.extendInfo
        if self.table.addCardProcessor.getState() & MTableState.TABLE_STATE_TING:
            _pattern, newState = exInfo.getFirstPattern(MTableState.TABLE_STATE_TING)
            tingInfo = exInfo.getChoosedInfo(newState)
            ftlog.debug('MajiangTable.autoProcessAddCard tingInfo:', tingInfo)
            if 'ting' in tingInfo:
                tingInfo = tingInfo['ting'][0]
            ftlog.debug('MajiangTable.autoProcessAddCard tingInfo:', tingInfo)
            # 选择打掉的牌,用剩下的牌听,自动选择可胡牌数最多的解,默认不扣任何牌
            self.table.tingAfterDropCard(player.curSeatId, tingInfo['dropTile'], [], self.table.addCardProcessor.extendInfo)
            return
        
        if self.table.addCardProcessor.getState() & MTableState.TABLE_STATE_GANG:
            _pattern, newState = exInfo.getFirstPattern(MTableState.TABLE_STATE_GANG)
            ftlog.debug('MajiangTable.autoProcessAddCard getFirstGangPattern pattern:', _pattern
                , ' newState:', newState)
            
            exInfo.updateState(newState, _pattern)
            gangInfo = exInfo.getChoosedInfo(newState)

            ftlog.debug( 'MajiangTable.autoProcessAddCard gangInfo:', gangInfo )
            style = gangInfo['style']
            pattern = gangInfo['pattern']
            special_tile = self.getPiguTile()
            self.table.gangTile(player.curSeatId, nowTile, pattern, style, MTableState.TABLE_STATE_GANG, special_tile)
            return

        ftlog.debug( player.copyTiles() )
        ftlog.debug( player.isTing() )
        minTile, minValue = MTileValue.getBestDropTile(player.copyTiles()
            , self.table.tableTileMgr.getTiles()
            , self.table.playMode
            , nowTile
            , player.isTing()
            , self.table.tableTileMgr.getMagicTiles(player.isTing())
            , self.table.tingRule
      	    ,player.isWon())
	# modify by youjun 05.04 
        ftlog.debug('autoProcessAddCard.getBestDropTile minTile:', minTile, ' minValue:', minValue)
        
        # 最后,出价值最小的牌
        self.table.dropTile(player.curSeatId, minTile)
Exemplo n.º 2
0
    def getBestDropTile(self,
                        tiles_player_hand,
                        tiles_left,
                        tile,
                        isTing,
                        magicTiles,
                        absenceColor,
                        tingRule=None):
        """
        手牌的价值,根据玩家自己的手牌和已经出的牌,计算手牌价值
        参数:
            tiles_player_hand - 用户的手牌
            tiles_droped - 牌桌上已经打出的牌和玩家手里已经成型的牌,这部分牌不再参与计算牌的可能性
        计算方法:
        1)没有的手牌,权值为0
        2)有的手牌,初始权值为4 * count + 1 * left
        3)左右相邻的手牌,增加权重 3 * count
        4)左右隔一张的手牌,增加权重 2 * count
        """
        ftlog.debug(
            'MDropCardStrategyJipinghu.getBestDropTile tiles_player_hand:',
            tiles_player_hand, ' tiles_left:', tiles_left, ' tile:', tile,
            ' isTing:', isTing, ' magicTiles:', magicTiles, ' absenceColor:',
            absenceColor, ' tingRule:', tingRule)

        if isTing:
            # 听牌后,直接打出摸到的牌
            return tile, 0

        doQing, qingColor = self.doQingYiSe(tiles_player_hand, tiles_left)
        tiles_value_Arr, tiles_player_Arr = MTileValue.getHandTilesValue(
            tiles_player_hand, tiles_left)
        # 放大癞子牌的作用
        for mTile in magicTiles:
            tiles_value_Arr[mTile] = tiles_value_Arr[mTile] * 100

        # 如果有一门花色大于9张牌,放大该门花色牌的价值,便于去做清一色番型
        if doQing:
            for tile in MTile.traverseTile(qingColor):
                tiles_value_Arr[tile] += 10

        # 减小缺牌的作用
        for tile in range(len(tiles_value_Arr)):
            if MTile.getColor(tile) == absenceColor:
                tiles_value_Arr[tile] -= 100
                continue

            if doQing and MTile.getColor(tile) != qingColor:
                tiles_value_Arr[tile] -= 10
                continue

        # [{'dropTile': 11, 'winNodes': [{'winTile': 1, 'winTileCount': 3, 'pattern': [[6, 6], [5, 6, 7], [4, 5, 6], [1, 2, 3]]}, {'winTile': 2, 'winTileCount': 2, 'pattern': [[6, 6, 6], [5, 6, 7], [3, 4, 5], [2, 2]]}, {'winTile': 4, 'winTileCount': 3, 'pattern': [[6, 6], [5, 6, 7], [4, 5, 6], [2, 3, 4]]}, {'winTile': 5, 'winTileCount': 2, 'pattern': [[6, 6, 6], [5, 6, 7], [5, 5], [2, 3, 4]]}, {'winTile': 7, 'winTileCount': 1, 'pattern': [[6, 6], [5, 6, 7], [5, 6, 7], [2, 3, 4]]}, {'winTile': 8, 'winTileCount': 1, 'pattern': [[6, 7, 8], [6, 6, 6], [5, 5], [2, 3, 4]]}]}]
        if tingRule:
            canTing, tingResults = tingRule.canTing(tiles_player_hand,
                                                    tiles_left, tile,
                                                    magicTiles)
            ftlog.debug(canTing)
            ftlog.debug(tingResults)

            if canTing:
                totalValue = 0
                for tingResult in tingResults:
                    dropTile = tingResult['dropTile']
                    winNodes = tingResult['winNodes']
                    for winNode in winNodes:
                        totalValue += winNode['winTileCount'] * winNode[
                            'winFan']

                for tingResult in tingResults:
                    dropTile = tingResult['dropTile']
                    winNodes = tingResult['winNodes']
                    dropValue = 0
                    for winNode in winNodes:
                        dropValue += winNode['winTileCount'] * winNode['winFan']
                    tiles_value_Arr[dropTile] = (totalValue - dropValue)

                    if doQing and (MTile.getColor(dropTile) == qingColor) and (
                            tiles_value_Arr[dropTile] != -4):
                        # 优先打出非清一色颜色的上听牌
                        tiles_value_Arr[dropTile] += (totalValue /
                                                      len(tingResults))

                ftlog.debug(
                    'MDropCardStrategyJipinghu.getBestDropTile adjust tileValue by ting:',
                    tiles_value_Arr, ' tingResults:', tingResults,
                    ' totalValue:', totalValue)

        minTile = 0
        minValue = 0
        for index in range(MTile.TILE_MAX_VALUE):
            if tiles_player_Arr[index] > 0:
                if minTile == 0:
                    minTile = index
                    minValue = tiles_value_Arr[index]
                    continue

                if minValue > tiles_value_Arr[index]:
                    minValue = tiles_value_Arr[index]
                    minTile = index

        ftlog.debug('MDropCardStrategyJipinghu.getBestDropTile minTile:',
                    minTile, ' tileValue:', tiles_value_Arr[minTile])
        return minTile, tiles_value_Arr[minTile]
    def getBestDropTile(self,
                        tiles_player_hand,
                        tiles_left,
                        tile,
                        isTing,
                        magicTiles,
                        extend={}):
        """
        手牌的价值,根据玩家自己的手牌和已经出的牌,计算手牌价值
        参数:
            tiles_player_hand - 用户的手牌
            tiles_droped - 牌桌上已经打出的牌和玩家手里已经成型的牌,这部分牌不再参与计算牌的可能性
        计算方法:
        1)没有的手牌,权值为0
        2)有的手牌,初始权值为4 * count + 1 * left
        3)左右相邻的手牌,增加权重 3 * count
        4)左右隔一张的手牌,增加权重 2 * count
        """
        ftlog.debug(
            'MDropCardStrategyXuezhan.getBestDropTile tiles_player_hand:',
            tiles_player_hand, ' tiles_left:', tiles_left, ' tile:', tile,
            ' isTing:', isTing, ' magicTiles:', magicTiles, ' extend:', extend)

        if isTing:
            # 听牌后,直接打出摸到的牌
            return tile, 0

        tiles_value_Arr, tiles_player_Arr = MTileValue.getHandTilesValue(
            tiles_player_hand, tiles_left)

        # 如果有缺牌,打出缺牌
        seatId = extend.get('seatId', 0)
        abColors = extend.get('absenceColor', [])
        absenceColor = MTile.TILE_FENG
        if len(abColors) > 0:
            absenceColor = abColors[seatId]

        abMinTile = 0
        abMinValue = 0
        for tile in MTile.traverseTile(absenceColor):
            if tiles_player_Arr[tile] > 0:
                if abMinTile == 0:
                    abMinTile = tile
                    abMinValue = tiles_value_Arr[tile]
                    continue

                if abMinValue > tiles_value_Arr[tile]:
                    abMinValue = tiles_value_Arr[tile]
                    abMinTile = tile
        if abMinTile:
            ftlog.debug('has Absence, drop the min absence tile:', abMinTile,
                        ' abMinValue:', abMinValue)
            return abMinTile, abMinValue

        seatId = extend.get('seatId', None)
        abColors = extend.get('absenceColor', None)
        doQing, qingColor = self.doQingYiSe(tiles_player_hand, tiles_left,
                                            seatId, abColors)
        # 放大癞子牌的作用
        for mTile in magicTiles:
            tiles_value_Arr[mTile] = tiles_value_Arr[mTile] * 100

        # 如果有一门花色大于9张牌,放大该门花色牌的价值,便于去做清一色番型
        if doQing:
            colors = [MTile.TILE_WAN, MTile.TILE_TONG, MTile.TILE_TIAO]
            colors.remove(absenceColor)
            colors.remove(qingColor)
            dMinTiles = []
            dMinValue = 0
            for tile in MTile.traverseTile(colors[0]):
                if tiles_player_Arr[tile] > 0:
                    if len(dMinTiles) == 0:
                        dMinValue = tiles_value_Arr[tile]
                        dMinTiles.append(tile)
                        continue

                    if dMinValue > tiles_value_Arr[tile]:
                        dMinValue = tiles_value_Arr[tile]
                        dMinTiles = [tile]
                    elif dMinValue == tiles_value_Arr[tile]:
                        dMinTiles.append(tile)

            if len(dMinTiles) > 0:
                ftlog.debug('doQingYiSe, qingColor:', qingColor, ' dMinTiles:',
                            dMinTiles, ' dMinValue:', dMinValue)
                return self.chooseBestDropTile(dMinTiles, tiles_left,
                                               qingColor), dMinValue

        tingRule = extend.get('tingRule', None)
        # [{'dropTile': 11, 'winNodes': [{'winTile': 1, 'winTileCount': 3, 'pattern': [[6, 6], [5, 6, 7], [4, 5, 6], [1, 2, 3]]}, {'winTile': 2, 'winTileCount': 2, 'pattern': [[6, 6, 6], [5, 6, 7], [3, 4, 5], [2, 2]]}, {'winTile': 4, 'winTileCount': 3, 'pattern': [[6, 6], [5, 6, 7], [4, 5, 6], [2, 3, 4]]}, {'winTile': 5, 'winTileCount': 2, 'pattern': [[6, 6, 6], [5, 6, 7], [5, 5], [2, 3, 4]]}, {'winTile': 7, 'winTileCount': 1, 'pattern': [[6, 6], [5, 6, 7], [5, 6, 7], [2, 3, 4]]}, {'winTile': 8, 'winTileCount': 1, 'pattern': [[6, 7, 8], [6, 6, 6], [5, 5], [2, 3, 4]]}]}]
        if tingRule:
            canTing, tingResults = tingRule.canTing(tiles_player_hand,
                                                    tiles_left, tile,
                                                    magicTiles)
            ftlog.debug(canTing)
            ftlog.debug(tingResults)

            if canTing:
                totalValue = 0
                for tingResult in tingResults:
                    # 对dropTile的价值重新考虑
                    dropTile = tingResult['dropTile']
                    tiles_value_Arr[dropTile] = 0

                    winNodes = tingResult['winNodes']
                    for winNode in winNodes:
                        totalValue += winNode['winTileCount'] * winNode[
                            'winFan']

                for tingResult in tingResults:
                    dropTile = tingResult['dropTile']
                    winNodes = tingResult['winNodes']
                    dropValue = 0
                    for winNode in winNodes:
                        dropValue += winNode['winTileCount'] * winNode['winFan']
                    tiles_value_Arr[dropTile] += (-dropValue)

                    if doQing and (MTile.getColor(dropTile) == qingColor):
                        # 优先打出非清一色颜色的上听牌
                        tiles_value_Arr[dropTile] += (totalValue /
                                                      len(tingResults))

                    ftlog.debug(
                        'MDropCardStrategyXuezhan.getBestDropTile dropTile:',
                        dropTile, ' dropTileValue:', tiles_value_Arr[dropTile])

                ftlog.debug(
                    'MDropCardStrategyXuezhan.getBestDropTile adjust tileValue by ting:',
                    tiles_value_Arr, ' tingResults:', tingResults,
                    ' totalValue:', totalValue)

        minTiles = []
        minValue = 0
        for index in range(MTile.TILE_MAX_VALUE):
            if tiles_player_Arr[index] > 0:
                if (len(minTiles) == 0) or (minValue > tiles_value_Arr[index]):
                    minValue = tiles_value_Arr[index]
                    minTiles = [index]
                elif minValue == tiles_value_Arr[index]:
                    minTiles.append(index)

        ftlog.debug('MDropCardStrategyJipinghu.getBestDropTile minTiles:',
                    minTiles, ' tileValue:', minValue)
        return self.chooseBestDropTile(minTiles, tiles_left,
                                       qingColor), minValue
Exemplo n.º 4
0
    def getBestDropTile(self,
                        tiles_player_hand,
                        tiles_left,
                        tile,
                        isTing,
                        magicTiles,
                        extend={}):
        """
        手牌的价值,根据玩家自己的手牌和已经出的牌,计算手牌价值
        参数:
            tiles_player_hand - 用户的手牌
            tiles_droped - 牌桌上已经打出的牌和玩家手里已经成型的牌,这部分牌不再参与计算牌的可能性
        计算方法:
        1)没有的手牌,权值为0
        2)有的手牌,初始权值为4 * count + 1 * left
        3)左右相邻的手牌,增加权重 3 * count
        4)左右隔一张的手牌,增加权重 2 * count
        
        该套策略可用于推倒胡
        """
        if isTing:
            # 听牌后,直接打出摸到的牌
            ftlog.debug('alReady ting, drop tile directly:', tile)
            return tile, 0

        tiles_value_Arr, tiles_player_Arr = MTileValue.getHandTilesValue(
            tiles_player_hand, tiles_left)

        # 放大癞子牌的作用
        for mTile in magicTiles:
            tiles_value_Arr[mTile] = tiles_value_Arr[mTile] * 100

        tingRule = extend.get('tingRule', None)
        # [{'dropTile': 11, 'winNodes': [{'winTile': 1, 'winTileCount': 3, 'pattern': [[6, 6], [5, 6, 7], [4, 5, 6], [1, 2, 3]]}, {'winTile': 2, 'winTileCount': 2, 'pattern': [[6, 6, 6], [5, 6, 7], [3, 4, 5], [2, 2]]}, {'winTile': 4, 'winTileCount': 3, 'pattern': [[6, 6], [5, 6, 7], [4, 5, 6], [2, 3, 4]]}, {'winTile': 5, 'winTileCount': 2, 'pattern': [[6, 6, 6], [5, 6, 7], [5, 5], [2, 3, 4]]}, {'winTile': 7, 'winTileCount': 1, 'pattern': [[6, 6], [5, 6, 7], [5, 6, 7], [2, 3, 4]]}, {'winTile': 8, 'winTileCount': 1, 'pattern': [[6, 7, 8], [6, 6, 6], [5, 5], [2, 3, 4]]}]}]
        if tingRule:
            canTing, tingResults = tingRule.canTing(tiles_player_hand,
                                                    tiles_left, tile,
                                                    magicTiles)
            ftlog.debug(canTing)
            ftlog.debug(tingResults)

            if canTing:
                totalValue = 0

                for tingResult in tingResults:
                    dropTile = tingResult['dropTile']
                    tiles_value_Arr[dropTile] = 0

                    winNodes = tingResult['winNodes']
                    for winNode in winNodes:
                        totalValue += winNode['winTileCount'] * winNode[
                            'winFan']

                for tingResult in tingResults:
                    dropTile = tingResult['dropTile']
                    winNodes = tingResult['winNodes']
                    dropValue = 0
                    for winNode in winNodes:
                        dropValue += winNode['winTileCount'] * winNode['winFan']
                    tiles_value_Arr[dropTile] += (-dropValue)

        minTile = 0
        minValue = 0
        for index in range(MTile.TILE_MAX_VALUE):
            if tiles_player_Arr[index] > 0:
                if minTile == 0:
                    minTile = index
                    minValue = tiles_value_Arr[index]
                    continue

                if minValue >= tiles_value_Arr[index]:
                    # 优先打高数值的牌,风
                    minValue = tiles_value_Arr[index]
                    minTile = index

        ftlog.debug('MDropCardStrategy.getBestDropTile ', ' tile:', tile,
                    ' magicTiles:', magicTiles, ' isTing:', isTing,
                    ' tiles_player_hand:', tiles_player_hand, ' tileValues:',
                    tiles_value_Arr, ' tiles_left:', tiles_left, ' minTile:',
                    minTile, ' minTileValue:', minValue)

        return minTile, tiles_value_Arr[minTile]