Using Unicode to Produce Better Looking Cards and Decks

Updated BustACode 1 Tallied Votes 1K Views Share

Instead of using letters as substitutes for playing card suits, I came up with a way to use unicodes to produce the common suits. The following creates the coded needed, makes the deck, and then prints the deck. The printing was the hard part. Is 2.7, and should be 3.0+ compat.

# Imports #
from __future__ import print_function

# Main #
def main():
        # Main Code
            # Create Deck of Cards
    v_Deck = f_CreateDeck() ## Create deck of cards.
    print("DEBUG", "Deck") # DEBUG
    f_PrintDeck(v_Deck) # DEBUG


def f_CreateDeck():
    """Creates the card deck of 13 cards, 2 > A, of each of 4 suits
        None, Return(string list)"""
    v_Deck = [] ## Init deck variable
    for i in [u"u2663", u"u2660", u"u2666", u"u2665"]: ## Each suit: Clubs, Spades, Diamonds, Hearts
        for j in xrange(2, 11): ## Numbered cards
            v_Deck.append(str(j) + i) ## Append each number to a suit
        for j in ("J", "Q", "K", "A"): ## Face cards
            v_Deck.append(str(j) + i) ## Append each face card to a suit
    return v_Deck

            # DEBUG PRINT DECK OF CARDS
def f_PrintDeck(v_Deck):
    v_RowCount, v_CardCount = 0, 0 ## Init row count, and card count
    v_LenLine = 13 ## Init typical line length
    v_NumLines, v_LenLastLine = divmod(len(v_Deck), v_LenLine) ## Get this deck's computed num of lines, and the length of the last line
    for i in xrange(0, (v_NumLines + 1)):
        if v_RowCount == (v_NumLines): ## If at last line change length of line to computed value of last line
            v_LenLine = v_LenLastLine
        for j in xrange(v_LenLine): ## Run through printing each value to max of line length
            print((v_Deck[v_CardCount]), end = ",")
            v_CardCount += 1 ## Increment card count
        print() ## Advance to next line
        v_RowCount += 1 ## Increment row
    return
            
    
main()
Gribouillis 1,391 Programming Explorer Team Colleague

As there is no issue, you could post this as a code snippet (select the appropriate article type when the article is created). Why not add a call tomain() at the end so that it does something when we try it ?

commented: System won't let me post as "snippet," only discussion. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In the same vein...
Unicode also has characters for displaying chess pieces, starting at u2654

Gribouillis 1,391 Programming Explorer Team Colleague
TrustyTony 888 pyMod Team Colleague Featured Poster

I changed the snippet to code snippet and added call to main.

fonzali 0 Light Poster

to make the code compatable to python 3 , replace xrange with range in line 18 replace line 17 with the line below:

for i in ['\u2663', '\u2660', "\u2666", "\u2665"]:
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.