0

I want to build a straight function for a Texas hold'em program. I've created some test values and want the function to return the list of cards that satisfy the straight.

This is what I have so far:

import cards

c1=cards.Card(1,1)
c2=cards.Card(2,1)
c3=cards.Card(3,2)
c4=cards.Card(4,2)
c5=cards.Card(5,2)
c6=cards.Card(6,4)
c7=cards.Card(3,4)
c8=cards.Card(7,3)
H1=[c7,c3,c2,c6,c5,c4,c1]
H2=[c1,c2,c3,c2,c3,c3,c8]


def build_rank_D(H):
    dict1={}
    for item in H:
        A=item.get_rank()
        if A not in dict1:
            dict1[A]=[item]


        else:
            dict1[A].append(item)





 return dict1

def straight(H):
    sequence=set()
    for item in H:
        A=item.get_rank()
        sequence.add(A)

    list_seq=list(sequence)
    n=list_seq[0]
    new_list=[]
    if list_seq[1]==n+1 and list_seq[2]==n+2 and list_seq[3]==n+3 and list_seq[4]==n+4
        print("you have a straight")
        return H

    else:
        print("no straight found")
    return []


print(straight(H1))

straight(H2)

Right now the function prints the entire set of cards, not the cards that satisfy the straight, which is what I want.

This is a sample of the cards class program that I've imported:

import random    # required for shuffle method of Deck

class Card(object):
    ''' Suit and rank are ints, and index into suit_list and rank_list.
        Value is different from rank: for example face cards are equal in value (all 10)
    '''
    # Use these lists to map the ints of suit and rank to nice words.
    # The 'x' is a place holder so that index-2 maps to '2', etc.
    suit_list = ['x','c','d','h','s']
    rank_list = ['x', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10','J', 'Q', 'K']

    def __init__(self, rank=0, suit=0):
        ''' Rank and suit must be ints. This checks that they are in the correct range.
            Blank card has rank and suit set to 0.
        '''
        if type(suit) == int and type(rank) == int:
            # only good indicies work
            if suit in range(1,5) and rank in range(1,15):
                self.__suit = suit
                self.__rank = rank

            else:
                self.__suit = 0
                self.__rank = 0
        else:
            self.__suit = 0
            self.__rank = 0
    def get_rank(self):
        return self.__rank

    def get_suit(self):
        return self.__suit

2 Answers2

0

What this does:

sequence = set()
...
list_seq=list(sequence)

is produce a list of integers in random order (set is unordered), you will want to sort the list first before making the comparisons.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • I am still unable to print out only the cards that form a straight. My current function prints the entire list of cards. I am assuming I have to write a loop and iterate, but I'm not exactly sure. When I print out list_seq, the integers are in the correct order. – user3477408 Apr 07 '14 at 22:44
  • @user3477408: don't trust what you see when you print; integers are its own hash, which is why it will in many cases look like it's in order, but you'll get bitten when this accident of implementation doesn't hold. (e.g. `print list(set([1, 5, 11]))` produces `[1, 11, 5]`) – Lie Ryan Apr 07 '14 at 22:50
0

As @LieRyan stated, first you have to be sure your cards are ordered, and then you can continue finding the straight.

For that, you can add a __lt__(self, other) method to your Card class that will allow you to order a list of class. In this method you order according to the rank:

def __lt__(self, other):
    if self.__rank < other.__rank:
        return True
    else:
        return False

Then, to achieve the output you want I will recomend to add a __repr__(self) method to control how your Cards are printed:

def __repr__(self):
    return str(self.rank_list[self.__rank]) + str(self.suit_list[self.__suit]) 

Finally in your stright(H) you only need to order the Cards, get a list with unique ranks (I'm using a list mycards to control which cards are the unique one I'm considering), check your straight and print the 5 first cards from mycards:

def straight(H):
    H.sort()
    list_seq=[]
    mycards = []
    for item in H:
        A=item.get_rank()
        if A not in list_seq:
            list_seq.append(A)
            mycards.append(item)

    n=list_seq[0]
    new_list=[]
    if list_seq[1]==n+1 and list_seq[2]==n+2 and list_seq[3]==n+3 and list_seq[4]==n+4:
        print("you have a straight")
        return mycards[0:5]

    else:
        print("no straight found")
        return []

For straight(H1), in my case, I'm getting:

you have a straight
[Ac, 2c, 3s, 4d, 5d]

If you want to print whole H, just omit the list mycards and print H (which is already ordered)

Ruben Bermudez
  • 2,293
  • 14
  • 22