CARD GAME | PYTHON

In this article,  we quickly run into how to create a card game using python.

Introduction:

We all know that card game is all about ranks and suits.

A standard 52 deck of cards consists of four types of cards .they are spades, hearts, diamonds, and clubs. Each suit includes three face cards, King, Queen, and Jack. And each rank means the individual cards from high to low.

So let’s find out the steps involved in playing the card game in python.

Outline:

 We can directly run the program and get the output. We don’t need to take any input from the user.

Project Prerequisites:

We don’t need to install any packages to run this code. We need to import the required modules. They are random and sys modules.

A random module is used for generating random numbers.

 A sys module lets us access system-specific parameters and functions.

Code Implementation :

Firstly we have to import the modules from the Tkinter package.

Tkinter package is pre-installed within the python.

so let’s import these 2 modules.

import random
import sys

Here in the below code, we will create some classes and functions.

Firstly we have to create a class name called card and define its properties.

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

Now, we have to create another class called deck for initializing the deck of the 52 cards.

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

Here this class represents playing cards.

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)

The main part of the code. in this part, we initialize the deck and number of players, and finally our last code to check the winner card.

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)

Source Code:

Here is the complete source code for the project.

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)

Output:

Run the python script in your command prompt.

See also  Best Python automation project with source code

After running the above-given command, you will be getting the prompt for choosing the card.

Here’s the screenshot of the output.

Important Note:

Note that you can still improve this code using some GUI packages. It is up to you to play with the code and make changes. We will implement this project in GUI in the upcoming projects.

Happy Coding.

🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.