def handTypeToHands(handType):
	if handTypeIsPair(handType):
		cards = [Card(handType[0] + suit) for suit in Card.SuitNames]
		return [Hand.fromCards(*cards) for cards in itertools.combinations(cards, 2)]
	elif handTypeIsSuited(handType):
		return [Hand.fromCards(Card(handType[0]+s), Card(handType[1]+s)) for s in Card.SuitNames]
	else:
		cards1 = [handType[0] + suit for suit in Card.SuitNames]
		cards2 = [handType[1] + suit for suit in Card.SuitNames]
		return [
			Hand.fromCards(Card(a), Card(b)) for (a, b) in itertools.product(cards1, cards2) if a[1] != b[1]
			]
def handTypeToHands(handType):
    if handTypeIsPair(handType):
        cards = [Card(handType[0] + suit) for suit in Card.SuitNames]
        return [
            Hand.fromCards(*cards)
            for cards in itertools.combinations(cards, 2)
        ]
    elif handTypeIsSuited(handType):
        return [
            Hand.fromCards(Card(handType[0] + s), Card(handType[1] + s))
            for s in Card.SuitNames
        ]
    else:
        cards1 = [handType[0] + suit for suit in Card.SuitNames]
        cards2 = [handType[1] + suit for suit in Card.SuitNames]
        return [
            Hand.fromCards(Card(a), Card(b))
            for (a, b) in itertools.product(cards1, cards2) if a[1] != b[1]
        ]
Beispiel #3
0
    def fromString(klass, string):
        """creates a hand range from a string containg hand patterns
		@param string: (str) 
		@return: L{HandRange}
		
		"""
        handRange = klass()
        # clean string
        p = string.replace(' ', '').replace('\t', '')
        p = p.split(',')
        for s in p:
            if not s: continue

            if s == 'random':
                deck = Deck()
                for cards in itertools.combinations(deck.cards, 2):
                    hand = Hand.fromCards(*cards)
                    handRange._hands.add(hand)
                break

            elif s == 'suited':
                p.extend([
                    'A2s+',
                    'K2s+',
                    'Q2s+',
                    'J2s+',
                    'T2s+',
                    '92s+',
                    '82s+',
                    '72s+',
                    '62s+',
                    '52s+',
                    '42s+',
                    '32s+',
                ])
                continue

            elif s == 'offsuit':
                p.extend([
                    'A2o+',
                    'K2o+',
                    'Q2o+',
                    'J2o+',
                    'T2o+',
                    '92o+',
                    '82o+',
                    '72o+',
                    '62o+',
                    '52o+',
                    '42o+',
                    '32o+',
                ])
                continue

            elif s == 'broadways':
                p.extend([
                    'TT+',
                    'AT+',
                    'KT+',
                    'QT+',
                    'JT+',
                ])
                continue
            elif s == 'pairs':
                p.append('22+')
                continue

            # substring is a hand --> 'Kh7d'
            #
            result = klass.PatHand.match(s)
            if result is not None:
                card1, card2 = Card(result.group('card1')), Card(
                    result.group('card2'))
                hand = Hand.fromCards(card1, card2)
                handRange._hands.add(hand)
                continue

            # substring is a handTypePair --> 'TT' or 'TT+'
            #
            result = klass.PatHandTypePair.match(s)
            if result is not None:
                rank = result.group('rank')
                hands = handTypeToHands(rank + rank)
                for hand in hands:
                    handRange._hands.add(hand)

                # expand pattern if necessary
                qualifier = result.group('qualifier')
                if qualifier:
                    iRank = Card.RankNames.index(rank)
                    for otherRank in Card.RankNames[iRank + 1:]:
                        p.append(otherRank + otherRank)
                continue

            # substring is a handTypeSuit --> 'KTs', 'KTs+', 'KTo' or 'KT'
            #
            #NOTE: PokerTracker handles 'KT' but not 'KT+', we do
            result = klass.PatHandTypeSuit.match(s)
            if result is not None:
                rank1 = result.group('rank1')
                rank2 = result.group('rank2')
                rank1, rank2 = klass._sortedCardRanks(rank1,
                                                      rank2,
                                                      revert=True)
                suit = result.group('suit')
                qualifier = result.group('qualifier')

                # got a pair - assume typo - PokerStove does so as well (?)
                if rank1 == rank2:
                    if qualifier:
                        p.append('%s%s%s' % (rank1, rank1, qualifier))
                    else:
                        p.append('%s%s' % (rank1, rank1))
                    continue

                if suit:
                    hands = handTypeToHands(rank1 + rank2 + suit)
                else:
                    hands = handTypeToHands(rank1 + rank2 +
                                            's') + handTypeToHands(rank1 +
                                                                   rank2 + 'o')
                for hand in hands:
                    handRange._hands.add(hand)

                # expand pattern if necessary
                if qualifier:
                    iRank1 = Card.RankNames.index(rank1)
                    iRank2 = Card.RankNames.index(rank2)
                    for otherRank in Card.RankNames[iRank2 + 1:iRank1]:
                        if otherRank == rank1: continue
                        if suit:
                            for hand in handTypeToHands(rank1 + otherRank +
                                                        suit):
                                handRange._hands.add(hand)

                        else:
                            for hand in handTypeToHands(rank1 + otherRank +
                                                        's'):
                                handRange._hands.add(hand)
                            for hand in handTypeToHands(rank1 + otherRank +
                                                        'o'):
                                handRange._hands.add(hand)
                continue

            # substring is a handTypePairRange --> '22-TT'
            #
            result = klass.PatHandTypePairRange.match(s)
            if result is not None:
                rank1 = result.group('rank1')
                rank2 = result.group('rank2')
                rank1, rank2 = klass._sortedCardRanks(rank1,
                                                      rank2,
                                                      revert=False)
                iRank1 = Card.RankNames.index(rank1)
                iRank2 = Card.RankNames.index(rank2)

                # expand pattern
                ranks = Card.RankNames[iRank1:iRank2 + 1]
                for rank in ranks:
                    for hand in handTypeToHands(rank + rank):
                        handRange._hands.add(hand)
                continue

            # substring is a handTypeSuiteRange --> 'K7s-KTs', 'KT-K7', 'KTo-K7', ...
            #
            result = klass.PatHandTypeSuitRange.match(s)
            if result is not None:
                rank1 = result.group('rank1')
                rank2 = result.group('rank2')
                rank1, rank2 = klass._sortedCardRanks(rank1,
                                                      rank2,
                                                      revert=True)
                rank3 = result.group('rank3')
                rank4 = result.group('rank4')
                rank3, rank4 = klass._sortedCardRanks(rank3,
                                                      rank4,
                                                      revert=True)
                # sort rank2 and 4 once again so we get ascending ranks for expanding
                rank2, rank4 = klass._sortedCardRanks(rank2,
                                                      rank4,
                                                      revert=False)
                if rank1 != rank3:
                    raise klass.ParseError('invalid range: %s' % s)
                # determine suit
                suit1 = result.group('suit1')
                suit2 = result.group('suit2')
                if suit1:
                    suit = suit1
                elif suit2:
                    suit = suit2
                else:
                    suit = None

                # expand pattern
                iRank2 = Card.RankNames.index(rank2)
                iRank4 = Card.RankNames.index(rank4)
                ranks = Card.RankNames[iRank2:iRank4 + 1]
                for rank in ranks:
                    if suit:
                        for hand in handTypeToHands(rank1 + rank + suit):
                            handRange._hands.add(hand)

                    else:
                        for hand in handTypeToHands(rank1 + rank + 's'):
                            handRange._hands.add(hand)
                        for hand in handTypeToHands(rank1 + rank + 'o'):
                            handRange._hands.add(hand)
                continue

            #
            raise klass.ParseError('invalid hand pattern: %s' % s)

        # finally
        return handRange
	def fromString(klass, string):
		"""creates a hand range from a string containg hand patterns
		@param string: (str) 
		@return: L{HandRange}
		
		"""
		handRange = klass()
		# clean string
		p = string.replace(' ', '').replace('\t', '')
		p = p.split(',')
		for s in p:
			if not s: continue
					
			if s == 'random':
				deck = Deck()
				for cards in itertools.combinations(deck.cards, 2):
					hand = Hand.fromCards(*cards)
					handRange._hands.add(hand)
				break
						
			elif s == 'suited':
				p.extend([
						'A2s+',
						'K2s+',
						'Q2s+',
						'J2s+',
						'T2s+',
						'92s+',
						'82s+',
						'72s+',
						'62s+',
						'52s+',
						'42s+',
						'32s+',
						])
				continue
				
			elif s == 'offsuit':
				p.extend([
						'A2o+',
						'K2o+',
						'Q2o+',
						'J2o+',
						'T2o+',
						'92o+',
						'82o+',
						'72o+',
						'62o+',
						'52o+',
						'42o+',
						'32o+',
						])
				continue
			
			elif s == 'broadways':
				p.extend([
						'TT+',
						'AT+',
						'KT+',
						'QT+',
						'JT+',
						])
				continue
			elif s == 'pairs':
				p.append('22+')
				continue	
						
			# substring is a hand --> 'Kh7d'
			#
			result = klass.PatHand.match(s)
			if result is not None:
				card1, card2 = Card(result.group('card1')), Card(result.group('card2'))
				hand = Hand.fromCards(card1, card2)
				handRange._hands.add(hand)
				continue
				
			# substring is a handTypePair --> 'TT' or 'TT+'
			#
			result = klass.PatHandTypePair.match(s)
			if result is not None:
				rank = result.group('rank')
				hands =  handTypeToHands(rank+rank)
				for hand in hands:
					handRange._hands.add(hand)
							
				# expand pattern if necessary
				qualifier = result.group('qualifier')
				if qualifier:
					iRank = Card.RankNames.index(rank)
					for otherRank in Card.RankNames[iRank +1:]:
						p.append(otherRank + otherRank)
				continue
								
			# substring is a handTypeSuit --> 'KTs', 'KTs+', 'KTo' or 'KT'
			#
			#NOTE: PokerTracker handles 'KT' but not 'KT+', we do
			result = klass.PatHandTypeSuit.match(s)
			if result is not None:
				rank1 = result.group('rank1')
				rank2 = result.group('rank2')
				rank1, rank2 = klass._sortedCardRanks(rank1, rank2, revert=True)
				suit = result.group('suit')
				qualifier = result.group('qualifier')
										
				# got a pair - assume typo - PokerStove does so as well (?)
				if rank1 == rank2:
					if qualifier:
						p.append('%s%s%s' % (rank1, rank1, qualifier))
					else:
						p.append('%s%s' % (rank1, rank1))
					continue
							
				if suit:
					hands = handTypeToHands(rank1+rank2+suit)
				else:
					hands = handTypeToHands(rank1+rank2+'s') + handTypeToHands(rank1+rank2+'o')
				for hand in hands:
					handRange._hands.add(hand)
							
				# expand pattern if necessary
				if qualifier:
					iRank1 = Card.RankNames.index(rank1)
					iRank2 = Card.RankNames.index(rank2)
					for otherRank in Card.RankNames[iRank2 +1:iRank1]:
						if otherRank == rank1: continue
						if suit:
							for hand in handTypeToHands(rank1 + otherRank + suit):
									handRange._hands.add(hand)
								
						else:
							for hand in handTypeToHands(rank1 + otherRank + 's'):
								handRange._hands.add(hand)
							for hand in handTypeToHands(rank1 + otherRank + 'o'):
								handRange._hands.add(hand)
				continue
				
			# substring is a handTypePairRange --> '22-TT'
			#
			result = klass.PatHandTypePairRange.match(s)
			if result is not None:
				rank1 = result.group('rank1')
				rank2 = result.group('rank2')
				rank1, rank2 = klass._sortedCardRanks(rank1, rank2, revert=False)
				iRank1 = Card.RankNames.index(rank1)
				iRank2 = Card.RankNames.index(rank2)
														
				# expand pattern
				ranks = Card.RankNames[iRank1:iRank2+1]
				for rank in ranks:
					for hand in handTypeToHands(rank + rank):
						handRange._hands.add(hand)
				continue
							
			# substring is a handTypeSuiteRange --> 'K7s-KTs', 'KT-K7', 'KTo-K7', ...
			#
			result = klass.PatHandTypeSuitRange.match(s)
			if result is not None:
				rank1 = result.group('rank1')
				rank2 = result.group('rank2')
				rank1, rank2 = klass._sortedCardRanks(rank1, rank2, revert=True)
				rank3 = result.group('rank3')
				rank4 = result.group('rank4')
				rank3, rank4 = klass._sortedCardRanks(rank3, rank4, revert=True)
				# sort rank2 and 4 once again so we get ascending ranks for expanding
				rank2, rank4 = klass._sortedCardRanks(rank2, rank4, revert=False)
				if rank1 != rank3:
					raise klass.ParseError('invalid range: %s' % s)
				# determine suit
				suit1 = result.group('suit1')
				suit2 = result.group('suit2')
				if suit1:
					suit = suit1
				elif suit2:
					suit = suit2
				else:
					suit = None
							
				# expand pattern
				iRank2 = Card.RankNames.index(rank2)
				iRank4 = Card.RankNames.index(rank4)
				ranks = Card.RankNames[iRank2:iRank4+1]
				for rank in ranks:
					if suit:
						for hand in handTypeToHands(rank1 + rank + suit):
							handRange._hands.add(hand)
						
					else:
						for hand in handTypeToHands(rank1 + rank + 's'):
							handRange._hands.add(hand)
						for hand in handTypeToHands(rank1 + rank + 'o'):
							handRange._hands.add(hand)
				continue
			
			#
			raise klass.ParseError('invalid hand pattern: %s' % s)	
		
		# finally		
		return handRange