A Card Deck

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only





Problem Description

Write a program that will shuffle and deal hands for a standard deck of playing cards.

Background and Techniques

This program uses list boxes to display cards in the deck and in the hands.  The next version will introduce card  graphics like those used in Solitaire and Hearts games that come with Windows OS. 

We will create a TDeck object to contain the cards.  For now the cards are just an array of integers from 0 to 51.  The suit will be calculated as cardnbr div 13 giving a number from 0 to 3.  The value of the card is cardnbr mod 13 which returns a number from 0 to 12 representing A,2,3... J,Q,K.   The TDeck object has 4 important  methods:  Shuffle - shuffles the deck, GetNextCard to return the next card from the top of the deck, and GetSuit and GetValue functions to return the string representations of suit and value for a given card.  

The most interesting problem solved here is how to shuffle the deck, i.e. how to randomly re-arrange the array of 52 numbers that are the deck.  The technique used here is not original, in fact it's probably the most common way.  For each of the 52 entries in the array, we'll generate a random index number from 0 to 51 and swap the current card with the random position just generated.

Running/Exploring the Program 

bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

Eventually the idea is to create some card games.  Games with multiple hands that must be kept hidden are a problem on a single computer monitor.  So we need games like solitaire or where the players hands are kept face down, like Old Maid.  


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.