Example Application


To illustrate some of the techniques you've been using so far, you'll develop a class module that you'll be able to build on and make use of in subsequent chapters. This class module will contain two classes:

  • Card: Represents a standard playing card, with a suit of club, diamond, heart, or spade, and a rank that lies between ace and king

  • Deck: Represents a full deck of 52 cards, with access to cards by position in the deck and the ability to shuffle the deck

You'll also develop a simple client to make sure that things are working, but you won't use the deck in a full card game application — yet!

Planning the Application

The class library for this application, Ch10CardLib, will contain your classes. Before you get down to any code, though, you should plan the required structure and functionality of your classes.

The Card Class

The Card class is basically a container for two read-only fields: suit and rank. The reason for making the fields read-only is that it doesn't make sense to have a "blank" card, and cards shouldn't be able to change once they have been created. To facilitate this, you'll make the default constructor private, and provide an alternative constructor that builds a card from a supplied suit and rank.

Other than this, the Card class will override the ToString() method of System.Object, so that you can easily obtain a human-readable string representing the card. To make things a little simpler, you'll provide enumerations for the two fields suit and rank.

The Card class is shown in Figure 10-8.

image from book
Figure 10-8

The Deck Class

The Deck class will maintain 52 Card objects. You'll just use a simple array type for this. This array won't be directly accessible, because access to the Card objects will be achieved through a GetCard() method, which will return the Card object with the given index.

This class should also expose a Shuffle() method to rearrange the cards in the array. the Deck class is shown in Figure 10-9.

image from book
Figure 10-9

Writing the Class Library

For the purposes of this example, I'll assume that you are familiar enough with VS to move away from the standard Try It Out way of doing things, so I won't list the steps explicitly. The important thing here is to look at and discuss the code, not to see how to enter it into VS, which is done with the same steps you've seen in many Try It Outs. Having said that, I will include some pointers to make sure that you don't run into any problems along the way.

Both your classes and your enumerations will be contained in a class library project called Ch10CardLib. This project will contain four .cs files, Card.cs that contains the Card class definition, Deck.cs that contains the Deck class definition, and Suit.cs and Rank.cs files containing enumerations.

To illustrate the power of the class diagram you saw earlier, you can put together a lot of this code using this tool. To get started here you need to do the following:

  1. Create a new class library project called Ch10CardLib in the directory C:\BegVCSharp\ Chapter10.

  2. Remove Class1.cs from the project.

  3. Open the class diagram for the project using the Solution Explorer window (you must have the project selected, rather than the solution, for the class diagram icon to appear).

The class diagram should be blank to start with, since the project contains no classes.

Note

If you can see the Resources and Settings classes in this view, they can be hidden by right-clicking on them and selecting Remove from Diagram.

Adding the Suit and Rank Enumerations

Using the Toolbox, you can add an enumeration by dragging an Enum from the Toolbox into the diagram, then filling in the dialog that appears. For example, for the Suit enumeration fill out the dialog as shown in Figure 10-10.

image from book
Figure 10-10

Next, you can add the members of the enumeration using the Class Details window. The values required are shown in Figure 10-11.

image from book
Figure 10-11

The values required for the Rank enumeration are shown in Figure 10-12.

image from book
Figure 10-12



Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net