avatarBetter Everything

Summary

The provided content outlines the process of creating a Solitaire game in Python, detailing the steps to generate a deck of cards, shuffle it, and draw cards.

Abstract

The article is the first in a series that guides readers through the development of a Solitaire game using Python. It begins by explaining how to create a standard deck of 52 playing cards, representing the four suits (Clubs, Diamonds, Spades, and Hearts) and the cards from Ace to King. The deck is then shuffled using Python's random.shuffle function to randomize the order of the cards. The author also demonstrates how to draw a card from the deck using the pop method, which removes and returns the last card in the list, effectively simulating the drawing action in a card game. The article emphasizes the practicality of this project for programming skill enhancement and invites readers to follow the series for further instruction. Additionally, the author encourages readers to support them and other writers by joining Medium.

Opinions

  • The author positions the Solitaire game project as an excellent exercise for practicing programming in Python.
  • The use of the random.shuffle function is presented as an easy and effective way to randomize the deck order.
  • Drawing cards with the pop method is suggested as a straightforward technique that also automatically updates the deck size.
  • The article implies that the process of building the game will be incremental, with more instructions to follow in subsequent parts of the series.
  • The author expresses a desire for reader engagement and support through Medium membership, indicating the value they place on reader followship and the platform's contribution model.

Making the Solitaire game in Python #1 — Drawing cards from a shuffled deck

In this series I will show you step-by-step how I made the card game Solitaire in Python. A great project to practice programming skills.

Solitaire is a card game played with 1 deck of 52 cards. Image by catalyststuff on Freepik

In the first part of this series, we will start with creating a deck of 52 playing cards. The deck should be able to be shuffled and we must be able to draw cards from them.

Creating a deck of 52 cards

We will write a function that loops over the four suits: Clubs, Diamonds, Spades and Hearts. For each suit we loop over the cards from Ace to King. And we append a tuple containing a combination of suit and card to an empty list called deck. At the end of the function we return the list deck, which by then is filled with 52 unique cards.

def create_deck():
    suits = ['Clubs','Diamonds','Spades','Hearts']
    cards = ['Ace','2', '3', '4', '5', '6', '7', '8', '9', '10',
             'Jack', 'Queen', 'King']
    deck = []
    for suit in suits:
        for card in cards:
            deck.append((suit,card))
    return deck

Here is how to test the create_deck function:

deck = create_deck()
print(deck)

Here is the result:

[(‘Clubs’, ‘Ace’), (‘Clubs’, ‘2’), (‘Clubs’, ‘3’), (‘Clubs’, ‘4’), (‘Clubs’, ‘5’), (‘Clubs’, ‘6’), (‘Clubs’, ‘7’), (‘Clubs’, ‘8’), (‘Clubs’, ‘9’), (‘Clubs’, ‘10’), (‘Clubs’, ‘Jack’), (‘Clubs’, ‘Queen’), (‘Clubs’, ‘King’), (‘Diamonds’, ‘Ace’), (‘Diamonds’, ‘2’), (‘Diamonds’, ‘3’), (‘Diamonds’, ‘4’), (‘Diamonds’, ‘5’), (‘Diamonds’, ‘6’), (‘Diamonds’, ‘7’), (‘Diamonds’, ‘8’), (‘Diamonds’, ‘9’), (‘Diamonds’, ‘10’), (‘Diamonds’, ‘Jack’), (‘Diamonds’, ‘Queen’), (‘Diamonds’, ‘King’), (‘Spades’, ‘Ace’), (‘Spades’, ‘2’), (‘Spades’, ‘3’), (‘Spades’, ‘4’), (‘Spades’, ‘5’), (‘Spades’, ‘6’), (‘Spades’, ‘7’), (‘Spades’, ‘8’), (‘Spades’, ‘9’), (‘Spades’, ‘10’), (‘Spades’, ‘Jack’), (‘Spades’, ‘Queen’), (‘Spades’, ‘King’), (‘Hearts’, ‘Ace’), (‘Hearts’, ‘2’), (‘Hearts’, ‘3’), (‘Hearts’, ‘4’), (‘Hearts’, ‘5’), (‘Hearts’, ‘6’), (‘Hearts’, ‘7’), (‘Hearts’, ‘8’), (‘Hearts’, ‘9’), (‘Hearts’, ‘10’), (‘Hearts’, ‘Jack’), (‘Hearts’, ‘Queen’), (‘Hearts’, ‘King’)]

As you can see, all the cards are there, but they are all in order. So now we will look into a way to shuffle the deck for us.

Shuffling decks

When you shuffle a deck you basically put the cards in a random order. In our case we have to put the card-tuples in our list deck in a random order. This can be easily done with the shuffle function from the standard package: random. All we have to do is pass the deck list in the function call.

from random import shuffle

def create_deck():
    suits = ['Clubs','Diamonds','Spades','Hearts']
    cards = ['Ace','2', '3', '4', '5', '6', '7', '8', '9', '10',
               'Jack', 'Queen', 'King']
    deck = []
    for suit in suits:
        for card in cards:
            deck.append((suit,card))
    return deck

deck = create_deck()
shuffle(deck)
print(deck)

The shuffle function does not return a new list but just rearranges the existing one. Here is the result:

[(‘Clubs’, ‘5’), (‘Diamonds’, ‘6’), (‘Diamonds’, ‘King’), (‘Spades’, ‘Jack’), (‘Spades’, ‘Ace’), (‘Diamonds’, ‘Jack’), (‘Clubs’, ‘3’), (‘Diamonds’, ‘Ace’), (‘Spades’, ‘2’), (‘Hearts’, ‘9’), (‘Hearts’, ‘4’), (‘Hearts’, ‘10’), (‘Clubs’, ‘9’), (‘Hearts’, ‘Queen’), (‘Spades’, ‘5’), (‘Clubs’, ‘Jack’), (‘Hearts’, ‘8’), (‘Spades’, ‘8’), (‘Spades’, ‘King’), (‘Diamonds’, ‘8’), (‘Spades’, ‘Queen’), (‘Spades’, ‘4’), (‘Clubs’, ‘8’), (‘Clubs’, ‘Queen’), (‘Clubs’, ‘2’), (‘Hearts’, ‘7’), (‘Hearts’, ‘5’), (‘Clubs’, ‘Ace’), (‘Hearts’, ‘6’), (‘Diamonds’, ‘3’), (‘Diamonds’, ‘4’), (‘Clubs’, ‘6’), (‘Hearts’, ‘King’), (‘Diamonds’, ‘7’), (‘Diamonds’, ‘9’), (‘Clubs’, ‘10’), (‘Hearts’, ‘Ace’), (‘Spades’, ‘7’), (‘Diamonds’, ‘10’), (‘Spades’, ‘3’), (‘Clubs’, ‘King’), (‘Spades’, ‘6’), (‘Hearts’, ‘2’), (‘Diamonds’, ‘Queen’), (‘Spades’, ‘10’), (‘Diamonds’, ‘2’), (‘Hearts’, ‘Jack’), (‘Clubs’, ‘4’), (‘Clubs’, ‘7’), (‘Hearts’, ‘3’), (‘Diamonds’, ‘5’), (‘Spades’, ‘9’)]

Drawing cards from a deck

Now we have a deck that is shuffled, it would be nice if we could draw cards from it. We can easily imitate the drawing of a card by using the pop method. This method removes the last item from a list and returns it. Meaning that not only we get a random card, but that card is also removed from the deck.

Here is a test of using the pop method to draw a card:

deck = create_deck()
shuffle(deck)

print(len(deck))

drawn_card = deck.pop()

print(drawn_card)
print(len(deck))

With this as the result:

52
('Clubs', '7')
51

The drawn card is 7 of Clubs. As you can see after drawing a card, the deck’s length is reduced from 52 to 51.

Keep in mind that everytime you run the code the deck gets put in a random order, so another card will highly likely be drawn. Here is a second test result:

52
('Diamonds', 'Ace')
51

That concludes it for this part of the creating Solitaire in Python series. Please consider following me to stay up-to-date on when the next part is released.

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

Python
Programming
Software Development
Game Development
Tutorial
Recommended from ReadMedium