I am attached to a card game, and at the moment I am working on its beginning. What confuses me is sorting cards in a hand according to their rank, and then much more, how to reduce repetition. At the moment, I could create a for loop to organize the maps, and then have 52 different ones, if for every opportunity, but I was wondering if this is an easier way to do this and a lot of other repetitive tasks. Thanks: D I will put the code below:
from random import shuffle class deckOfCards: def __init__(self): self.rank = ['2','3','4','5','6','7','8','9','T','J','Q','K','A'] self.suit = ['C', 'S', 'H', 'D'] self.deck = [r+s for r in self.rank for s in self.suit] shuffle(self.deck) def setValue(self, deck): cnt = 1 self.value = {} for i in self.deck: self.value[i] = cnt cnt += 1 class Deal: def __init__(self, deck, position): self.hand = deck[position::4]
EDIT: I think this is very good for sorting cards, but I'm still confused on how to eliminate some repetitions. Thanks for the help: D
def sortHand(player): hand = player.hand for i in hand: for i in hand: index = player1.hand.index(i) if index != 12: if deck.value[i] > deck.value[hand[index+1]]: hand.insert(index+1, hand.pop(index))
source share