[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionWrite a program that will shuffle and deal hands for a standard deck of playing cards. Background and TechniquesThis 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
Suggestions for Further ExplorationsEventually 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. |