Card Game Mini Project in Python
destination source:https://www.programming-techniques.com/?p=2366
This is the second post in a series of mini-projects for Python where you can learn programming knowledge related to python and implement them as a project.

To begin the implementation in Python, make sure you have the following tutorials covered:-
Source code for the mini-project of card game using python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import random import sys class Card: suits = ['\u2666', '\u2665', '\u2663', '\u2660'] # ["Clubs", "Diamonds", "Hearts", "Spades"] ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] def __init__(self, suit=0, rank=0): """Default constructor """ self.suit = suit self.rank = rank def __str__(self): """Returns a human-readable string representation """ return '%s %s' % (Card.suits[self.suit], Card.ranks[self.rank]) # return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit]) def __lt__(self, other): """Overriding < operator """ t1 = self.rank, self.suit t2 = other.rank, other.suit return t1 < t2 class Deck: def __init__(self): """Initializes the Deck with 52 cards.""" self.cards = [] for suit in range(4): for rank in range(13): card = Card(suit, rank) self.cards.append(card) self.shuffle() def __str__(self): """Returns a string representation of the deck.""" res = [] for card in self.cards: res.append(str(card)) return ', '.join(res) def __len__(self): """Overriding len operator""" return len(self.cards) def add_card(self, card): """Adds a card to the deck.""" self.cards.append(card) def pop_card(self, i=-1): """Removes and returns a card from the deck. i: index of the card to pop; by default, pops the last card. """ return self.cards.pop(i) def shuffle(self): """Shuffles the cards in this deck.""" random.shuffle(self.cards) def sort(self): """Sorts the cards in ascending order.""" self.cards.sort() def wincard(self, cards): """Get the highest winner card from list""" winner = cards[0] for card in cards: if winner < card: winner = card return winner class Hand(Deck): """Represents a hand of playing cards.""" def __init__(self, label=''): self.cards = [] self.label = label self.wincount = 0 def getlabel(self): """ Store players name """ return self.label def roundwinner(self): """ increasing the win count for player """ self.wincount += 1 def getwincount(self): """ get the winner count finally """ return self.wincount def __str__(self): return "Card for " + self.label + " is " + Deck.__str__(self) def play(argv): deck = Deck() # initialize deck hands = [] for i in range(1, 5): player = 'Player %d' % i # default player name if len(argv) > i: player = argv[i] # get player name from command line parameter hands.append(Hand(player)) # add player while len(deck) > 0: for hand in hands: hand.add_card(deck.pop_card()) # remove card from deck and add to hand print(hands[0]) # print first player card input("Lets start playing. Press any key to continue : ") # wait for keypress for i in range(1, 14): cards = [] # collect card in a round floors = [] # get string representation for display in each round for hand in hands: card = hand.pop_card() cards.append(card) # collect individual card floors.append(hand.getlabel() + " : " + str(card)) # add string format for individual card winner_card = deck.wincard(cards) # check for winner card winner_hand = hands[cards.index(winner_card)] # find the winner hand from winner card winner_hand.roundwinner() # add score for winner hand print("Round", i, ":-", ", ".join(floors), ", Winner :- ", winner_hand.getlabel(), ":", winner_card) input() # wait for keypress to go for next round for hand in hands: # display the individual hand score after the 13 rounds of play print("Score for", hand.getlabel(), "is", hand.getwincount()) def main(argv=[]): answer = "Y" while answer.upper() == "Y": play(argv) answer = input("Play Again (Y/N)? : ") print("Bye Bye") if __name__ == '__main__': main(sys.argv) |
The output of the above code is:-
You can find the complete code in Github link.

Related Article
destination source:https://www.programming-techniques.com/?p=2366