Graphics Reference
In-Depth Information
_shuffleCard =
this.splice([Math.floor(Math.random()*_deckCount)],1);
this.push(_shuffleCard);
}
_shuffleNumber++;
}
return this;
}
}
}
DeckArray Breakdown
First, the package. Since DeckArray is a utility, we
ll go ahead and
place it in the com.flashadbook.utils package. The first thing we do
inside the class is to declare a few variables that will be used by the
shuffle method. First is _deckCount , which is used to hold
the number of cards in the deck (also known as the objects in the
array). The reason we
'
re going to
reference it in a for loop in the shuffle method. By referencing this
variable instead of the .length property of the array, we save the vir-
tual machine the trouble of having to actually get the length on each
and every loop. The next variable is _shuffleNumber and is used to
count how many times the deck has been fully shuffled in a single
call to the shuffle method. The third variable is the _shuffleCard
variable. The _shuffleCard variable will be used as the card
that
'
ll need this is because we
'
s being pulled from the deck and then immediately put back
in a new position. The next item in the DeckArray class is the
constructor. Since it
'
'
s a pretty standard Array constructor, I
'
ll just
say that it
s there and it fills the DeckArray with the items passed in
via the args parameter. And finally, we get to the shuffle method.
You
'
ll notice that the shuffle method returns an Array and
has a single optional parameter named timesToShuffle .This
parameter allows you to determine how many times the deck
should be fully shuffled, and it defaults to 2. Once inside
this method, we
'
ll immediately (re)set the values of two of the vari-
ables that were declared at the top of the class: _deckCount and
_shuffleNumber . Now for the actual shuffling of the cards. The
while loop keeps track of how many times the deck has been fully
shuffled by comparing _shuffleNumber against timesToShuffle ,and
the for loop contained within does the shuffle. Inside the for loop,
a single, random card is assigned to the _shuffleCard variable,
pulled from the deck ( this.splice ), and placed back on top of the
deck ( this.push ). Once the number of cards that have been
shuffled matches the number of cards in the deck ( _deckCount ),
_shuffleNumber is increased, and the deck is shuffled again
if needed.
'
Search WWH ::




Custom Search