just need help with a card game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codie
    New Member
    • May 2007
    • 8

    just need help with a card game

    I got my code to work be the print out go's down on a angle is there anyway to stop this from happing. P.S sorry for taking up your time


    Code:
    class Card:
    
        suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
        rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
                    "8", "9", "10", "Jack", "Queen", "King"]
    
        def __init__ (self, suit=0, rank=1):
            if suit >= 0 and suit <= 3:
                self.suit = suit
            else:
                self.suit = 0
            if rank >= 1 and rank <= 13:
                self.rank = rank
            else:
                self.rank = 1
    
        def __str__(self):
            return self.rankList[self.rank] + " of " + self.suitList[self.suit]
    
        def __cmp__(self, other):
            if self.rank > other.rank:
                return 1
            if self.rank < other.rank:
                return -1
            if self.suit > other.suit:
                return 1
            if self.suit < other.suit:
                return -1
            return 0
    
    class Deck:
    
        def __init__(self):
            self.cards = [] 
            for suit in range(4):
                for rank in range(1,14):
                    self.cards.append(Card(suit, rank))
    
        def __str__(self):
            s = ""
            for i in range(len(self.cards)):        
                s = s + (" " * i) + str(self.cards[i]) + "\n"
            return s
    
        def printDeck(self):
            for card in self.cards:
                print card
    
       
    def main():
    
        deck1 = Deck()
        print "Original deck:"
        print deck1
        
        
    main()
    if some one can help me that will be gr8
  • codie
    New Member
    • May 2007
    • 8

    #2
    o sorry and i need to add spaces between them as well :-(

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      'Deck' method:[code=Python] def __str__(self):
      return '\n'.join([str(self.cards[i]) for i in range(len(self. cards))])[/code]

      Comment

      • codie
        New Member
        • May 2007
        • 8

        #4
        thank you so much i need that help

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by codie
          thank you so much i need that help
          You are welcome. I am pleased to help.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Actually, I like this better:[code=Python]....def __iter__(self):
            for card in self.cards:
            yield card

            def __str__(self):
            return '\n'.join([str(c) for c in self])[/code]

            Comment

            Working...