0

I'm creating a program that ranks poker hands. I want to sort each hand by number value of cards before ranking to make it easier but am having issues getting the sort to work. It sorts every letter in the self.deck instead of the individual strings.

from random import *
class Carddeck:

    def __init__(self, deck=[], hand=[]):
        self.deck=deck
        self.hand=hand
        numbers=['01','02','03','04','05','06','07','08','09','10','11','12']
        suits=['heart','spade','diamond','club']
        for each in numbers:
            for every in suits:
                deck.append(str(Card(each,every)))

    def __repr__(self):

        return str(self.deck)

    def shuffle(self):
        shuffle(self.deck)

        return self.deck

    def dealcards(self,n):
        if len(str(self.deck))>n:
            self.hand=str(self.deck[0:n])
            self.deck = self.deck[n:]
        elif len(self.deck)==n:
            self.hand=str(self.deck[0:n])
            deck=[]
            self.deck=deck
            numbers=['01','02','03','04','05','06','07','08','09','10','11','12']
            suits=['heart','spade','diamond','club']
            for each in numbers:
                for every in suits:
                    self.deck.append(Card(each,every))
        else:
            x=len(self.deck)
            self.hand=self.deck
            self.deck=[]

            numbers=[1,2,3,4,5,6,7,8,9,10,11,12]
            suits=['heart','spade','diamond','club']
            for each in numbers:
                for every in suits:
                    self.deck.append(Card(each,every))
            y=n-x
            self.hand+=str(self.deck[0:y])
            self.deck=self.deck[y-1-3:]


class Pokerhand(Carddeck):
    def newHand(self,hand):
        self.dealcards(5)

    def __repr__(self):
        return str(self.hand)



    def rank(self):
        self.hand=sorted(self.hand)
        return self.hand


>>> x=Carddeck()
>>> x.shuffle()
['01 diamond', '05 heart', '03 club', '05 spade', '09 club', '02 diamond', '03 diamond', '08 diamond', '01 heart', '07 diamond', '11 diamond', '05 club', '02 club', '07 club', '10 club', '01 club', '04 club', '04 heart', '12 club', '11 club', '03 heart', '02 heart', '09 diamond', '08 heart', '06 diamond', '12 spade', '04 diamond', '07 heart', '10 diamond', '12 heart', '09 heart', '08 spade', '07 spade', '03 spade', '02 spade', '08 club', '10 heart', '01 spade', '12 diamond', '11 heart', '06 heart', '06 club', '10 spade', '09 spade', '04 spade', '05 diamond', '06 spade', '11 spade']

>>> y=Pokerhand()
>>> y.newHand(x)
>>> y
['01 diamond', '05 heart', '03 club', '05 spade', '09 club']
>>> y.rank()
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", ',', ',', ',', ',', '0', '0', '0', '0', '0', '1', '3', '5', '5', '9', '[', ']', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'h', 'i', 'l', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'u']
wwii
  • 23,232
  • 7
  • 37
  • 77
Bailey
  • 1
  • 1
  • You have to tell `sorted()` how to compare your cards with the `key` argument or by implementing `__cmp__()` in `Card`. – Klaus D. Dec 02 '16 at 03:49
  • [Sorting HOWTO](https://docs.python.org/3/howto/sorting.html) ... You could implement an [```__lt__()``` method](https://docs.python.org/3/reference/datamodel.html#object.__lt__) - ```The sort routines are guaranteed to use __lt__() when making comparisons between two objects. So, it is easy to add a standard sort order to a class by defining an __lt__() method:```. – wwii Dec 02 '16 at 05:19
  • That's one reason not to use strings for cards. Just represent them as integers from 0 to 51, representing (2c, 2d, 2h, 2s, 3c, 3d...Ks, Ac, Ad, Ah, As). That way, you can just sort and compare them naturally as numbers. – Lee Daniel Crocker Dec 02 '16 at 22:24

2 Answers2

1

sorted sorts a list of strings. If you pass a string to sorted, it is converted to a list of single-character strings and only then sorted. The hand in your program is actually a string, not a list of strings: self.hand=str(self.deck[0:n]).

DYZ
  • 55,249
  • 10
  • 64
  • 93
0

What's going on is that in your PokerHand class, your dealcards method, you have:

if len(str(self.deck))>n:
    self.hand=str(self.deck[0:n])
    self.deck = self.deck[n:]

So you end up setting hand to the string representation of a list. It sorts that string representation and you get the result you are seeing, which sorts the individual characters lexicographically. It doesn't make sense to make your hand attribute a string.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172