| By Jeff Peters | Article Rating: |
|
| October 12, 2006 12:00 PM EDT | Reads: |
15,653 |
As with the CardsArray.cfm file, we don't produce any output with ShuffleDeck.cfm. We test it with another test harness called testShuffleDeck.cfm. It's shown in Listing 4 and just includes CardsArray.cfm and ShuffleDeck.cfm, and then uses <cfdump> to show the shuffledDeckArray variable after the shuffling process. Its output is shown in Figure 2.
Okay, now that the deck is shuffled, may I deal the cards? Thank you; I'm glad this is such a friendly table. Listing 5 shows DealCards.cfm. It creates a two-dimensioned array named handsArray. The two dimensions represent hands and cards in each hand.
To deal, we use another nested loop (lines 34-40). For each hand, we create a new element in handsArray and initialize it as an empty array. Then for the number of cards per hand we just append the first element from the shuffledDeckArray to the hand and delete that first element from the shuffledDeckArray. The result of this process is the handsArray with the specified number of hands and cards per hand, and the shuffledDeckArray, which now holds the remainder of the deck. We use another test harness to set the parameters for the hands and cards per hand, run DealCards.cfm, and display the resulting arrays (Listing 6). Figure 3 shows those arrays.
So the deck has been shuffled and the cards have been dealt. But so far all we have to show for it are these lousy <cfdump> outputs. Not a very attractive game, is it? So now we want to build a way to make the dealt hands look like dealt hands. For that, we can use a set of images - one for each card, with the names matching the notation we used for cards in the arrays. Then it's just a matter of writing a nested loop to output the handsArray. That code from ShowHands.cfm is shown in Listing 7. It uses two nested counting loops, and their indices are i and j like the common practice we looked at earlier.
Since we need all the previous non-test code to make ShowHands.cfm work, we once again use a test harness as shown in Listing 8. It just includes the necessary files and doesn't set values for the number of hands or the number of cards per hand. We're just allowing ShowHands.cfm to use its default values for those variables. The final output is shown in Figure 4.
Arrays provide a powerful way to handle data that has a common purpose, like the cards in a deck; and whose elements aren't identified by name or idea. But until we've used them a few times, they aren't really a natural concept. So how do we decide an array would be a good idea? If you find yourself tempted to create variables with names like item1, item2, item3, etc., it's probably a good case for an array. On the other hand, if there seems to be related sets of variables - like firstName1, firstName2 and lastName1, lastName2 - it's probably a good case for a structure. Fortunately, you can find information on structures in this issue as well.
CFML also offers several array functions we haven't seen in this article, including ArrayIsEmpty, ArrayPrepend, ArrayAvg, ArrayResize, ArrayToList, ArrayClear, ArrayMax, IsArray, ArrayMin, ArraySort, ListToArray, ArrayInsertAt, and ArraySum. The names of these functions provide a pretty good indication of their purposes, so feel free to experiment with them on your own. I highly recommend the use of a test directory on your development box. You can create little CFML templates to test your ideas and play with code before you try it in a "real" environment.
If you're interested in a more complete discussion of complex datatypes in ColdFusion, you might be interested in my book ColdFusion Lists, Arrays & Structures. It can be ordered online at www.ProtonArts.com.
Published October 12, 2006 Reads 15,653
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Jeff Peters
Jeff Peters works for Open Source Data Integration Software company XAware.
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe Fiddles with its Web Apps
- Adobe & Salesforce Cut Cloud Deal
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe Flex Developer Earns $100K in New York City
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF




























