10.4. Class Design for Deck-of-Cards Program

Let's try our hand at a new program to incorporate some of the design elements I've been discussing in the last two chapters. Let's suppose you want to write a program that models shuffling a deck of cards.

Try It Out: Shuffle Cards

The program should simply display the shuffled deck of cards in a listbox object. Figure 10-1 shows a starting point of how you might construct the user interface for the program.

Figure 10-1. Figure 10-1

The figure shows what the output might look like after two clicks of the Shuffle button. Also note that a label object appears below the listbox to inform the user how many passes it took through the "shuffle loop" to produce a shuffled deck. (More on the shuffle loop in a minute.) Given that Figure 10-1 describes the way you want the user interface to look, how would you design and write the code? Equally important, where do you start the design process?

How It Works

As is almost always the case, you could write the code and stuff it all into frmMain and be done with it. However, is there a chance that at some time in the future you might write a game program that needs a deck of cards? A related question to ask is how much more difficult writing the program would be if you wrote a class to simulate a deck of cards instead of jamming all the code into frmMain. The answer is that there is virtually no ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.