1. Game introduction
When four famous brand players play cards, the computer will randomly distribute 52 cards (not big or small) to four players, and display the cards of each player on the screen.
2. Procedure design steps
Three classes are designed in the licensing program: Card class, Hand class and Poke class. Class Card
The Card class represents a Card, in which the FaceNum field refers to the number 1-13 of the Card, the Suit field refers to the decor, "Mei" refers to plum blossom, "Fang" refers to diamonds, "red" refers to hearts, "black" refers to spades. Class Hand
The Hand class represents a Hand (a card held by a player), which can be considered as a card held by a player. The cards list variable stores the cards held by the player. You can add cards, empty the cards in your Hand, and give one card to another player.
The Poke class represents a deck of cards. We can think of a deck of cards as a player with 52 cards, so we inherit the Hand class. Because the cards list variable needs to store 52 cards, and needs to be dealt and shuffled, the following methods are added. main program
The main program is relatively simple. Because there are four players, the players list is generated to store the initialized four players. The object instance poke1 of a deck of cards is generated. A deck of 52 cards is generated by calling the populate() method. The shuffle order is disrupted by calling the huffle() method. The deal(players, 13) method is called to issue 13 cards to each player respectively. Finally, all the cards of the four players are shown.
3. Some code Notes
1. Define the face of a playing card: Card number: 1-13 Plum for plum, square for diamonds, red for hearts, black for spades
2. Define turnover method
3. Define the serial number of the card
4. Generate a deck of cards
6. Give players 13 cards by default
4. Reference code
""" A playing card""" RANKS=["A","2","3","4","5","6","7","8","9","10","J","Q","K"] SUITS=["Plum blossom","square","red","black"] def __init__(self,rank,suit,face_up=True): self.rank=rank self.suit=suit self.is_face_up=face_up def __str__(self): if self.is_face_up: rep=self.suit+self.rank else: rep="XX" return rep def flip(self): self.is_face_up=not self.is_face_up def pic_order(self): if self.rank=="A": FaceNum=1 elif self.rank=="J": FaceNum=11 elif self.rank=="Q": FaceNum=12 elif self.rank=="K": FaceNum=13 else: FaceNum=int(self.rank) if self.suit=="Plum blossom": Suit=1 elif self.suit=="square": Suit=2 elif self.suit == "red": Suit = 3 else: Suit=4 return (Suit-1)*13+FaceNum class Hand(): """A hand of playing cards.""" def __init__(self): self.cards= def __str__(self): if self.cards: rep="" for card in self.cards: rep+=str(card)+"\t" else: rep="No cards" return rep def clear(self): self.cards= def add(self,card): self.cards.append(card) def give(self,card,other_hand): self.cards.remove(card) other_hand.add(card) class Poke(Hand): """A deck of playing cards""" def populate(self): for suit in Card.SUITS: for rank in Card.RANKS: self.add(Card(rank,suit)) def shuffle(self): import random random.shuffle(self.cards) def deal(self,hands,per_hand=13): for rounds in range(per_hand): for hand in hands: top_card=self.cards self.cards.remove(top_card) hand.add(top_card) if __name__=="__main__": print("This is a module with classes for playing cards.") players=[Hand(),Hand(),Hand(),Hand()] poke1=Poke() poke1.populate() poke1.shuffle() poke1.deal(players,13) n=1 for hand in players: print("card-game competitor",n,end=":") print(hand) n=n+1 input("\nPress the enter key to exit.")