Java Reference
In-Depth Information
DeckOfCards Constructor
The class's constructor instantiates the deck array (line 20) with NUMBER_OF_CARDS (52) ele-
ments. The elements of deck are null by default, so the constructor uses a for statement
(lines 24-26) to fill the deck with Card s. The loop initializes control variable count to 0 and
loops while count is less than deck.length , causing count to take on each integer value from
0 to 51 (the indices of the deck array). Each Card is instantiated and initialized with two
String s—one from the faces array (which contains the String s "Ace" through "King" )
and one from the suits array (which contains the String s "Hearts" , "Diamonds" , "Clubs"
and "Spades" ). The calculation count % 13 always results in a value from 0 to 12 (the 13 in-
dices of the faces array in lines 16-17), and the calculation count / 13 always results in a
value from 0 to 3 (the four indices of the suits array in line 18). When the deck array is
initialized, it contains the Card s with faces "Ace" through "King" in order for each suit (all
13 "Hearts" , then all the "Diamonds" , then the "Clubs" , then the "Spades" ). We use arrays
of String s to represent the faces and suits in this example. In Exercise 7.34, we ask you to
modify this example to use arrays of enum constants to represent the faces and suits.
DeckOfCards Method shuffle
Method shuffle (lines 30-46) shuffles the Card s in the deck. The method loops through
all 52 Card s (array indices 0 to 51). For each Card , a number between 0 and 51 is picked
randomly to select another Card . Next, the current Card object and the randomly selected
Card object are swapped in the array. This exchange is performed by the three assignments
in lines 42-44. The extra variable temp temporarily stores one of the two Card objects be-
ing swapped. The swap cannot be performed with only the two statements
deck[first] = deck[second];
deck[second] = deck[first];
If deck[first] is the "Ace" of "Spades" and deck[second] is the "Queen" of "Hearts" ,
after the first assignment, both array elements contain the "Queen" of "Hearts" and the
"Ace" of "Spades" is lost—so, the extra variable temp is needed. After the for loop termi-
nates, the Card objects are randomly ordered. A total of only 52 swaps are made in a single
pass of the entire array, and the array of Card objects is shuffled!
[ Note: It's recommended that you use a so-called unbiased shuffling algorithm for real
card games. Such an algorithm ensures that all possible shuffled card sequences are equally
likely to occur. Exercise 7.35 asks you to research the popular unbiased Fisher-Yates shuf-
fling algorithm and use it to reimplement the DeckOfCards method shuffle .]
DeckOfCards Method dealCard
Method dealCard (lines 49-56) deals one Card in the array. Recall that currentCard in-
dicates the index of the next Card to be dealt (i.e., the Card at the top of the deck). Thus,
line 52 compares currentCard to the length of the deck array. If the deck is not empty
(i.e., currentCard is less than 52), line 53 returns the “top” Card and postincrements cur-
rentCard to prepare for the next call to dealCard —otherwise, null is returned. Recall
from Chapter 3 that null represents a “reference to nothing.”
Shuffling and Dealing Cards
Figure 7.11 demonstrates class DeckOfCards . Line 9 creates a DeckOfCards object named
myDeckOfCards . The DeckOfCards constructor creates the deck with the 52 Card objects
 
Search WWH ::




Custom Search