A.8. Chapter 10

A.8.1. Exercise 1 Solution

Because the only difference is in the range of cards, no changes are needed to either the client (frmMain) or the server (clsCardDeck) objects. You do, however, need to change the way each card is viewed in the rules of the game. The change is quite simple: force each ace to have a value greater than a king. This means modifying the code for getFirstCard(), getSecondCard(), and getDealtCard() to reflect the following type of change:

public void getFirstCard()
{
    lowCardIndex = myDeck.getOneCard();
    lowCard = lowCardIndex % 13;
    if (lowCard == 0)               // A King
        lowCard = 13;
    if (lowCard == 1)               // View an Ace as high card
        lowCard = 14;
}

The last two lines of code would have to be added to each method, reflecting the card in question. This means that six new lines of code change the way the game is played. No other changes are needed.

A.8.2. Exercise 2 Solution

The code shown in getFirstCard(), for example, has several magic numbers in it, which are usually not a good idea. Suppose we make the following changes to clsInBetweenRules:

// =============== symbolic constants ===================
const int CARDSINSUIT = 13;
const int ACE = 1;
const int KING = 13;
const int MAKEACEHIGH = 14;

// Other code in class. . .

public void getFirstCard()
{
    lowCardIndex = myDeck.getOneCard();
    lowCard = lowCardIndex % CARDSINSUIT;
    if (lowCard == 0)               // A King
        lowCard = KING;
    if (lowCard == ACE)             // View an Ace as high card
        lowCard = MAKEACEHIGH;
}

The symbolic constants ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.