Messin' with loops...
Hi Guy:
There are probably a dozen different algorithms that could be used to simulate shuffling a deck of cards. The actual code to shuffle the deck appears on page 252 in the ShuffleDeck() method. You'll note that I use a while loop in that code, not a for loop. This is because I want to sit in that loop and spin around until all 52 cards have been assigned a position in the deck. This count is maintain by index.
The actual code to display the deck does use a for loop with the 3rd expression missing in the loop statement, although the increment takes place inside the nested for loop. You could do this more simply using a while loop again. I did it the hard way just to demonstrate a nested for loop. As an exercise, try to rewrite the loops on page 250 using just a while loop. (Hint: You might find the expression cardindex % 13 useful!) Also, I hate using what I call "magic numbers" in code. So, while you're at it, define a constant for the 13 you see on that page. Perhaps CARDSPERLINE would be a good name.
I am sorry about the typos. If you have a list, submit them to Wrox ASAP as they are planning a second printing of my book. If done quickly enough, they may be able to incorporate the changes. I'm sure some of them have already been corrected, but perhaps not all.
The Amazon review would help. If you read the review of the guy who dinged my book, the actual review is pretty positive even though he gives me a 2-star rating. That one review really hurt my average. Even though he stated he'd like to redo the review and give the book a higher rating, he's never done so and, to the cursory reader, it doesn't look too good. Oh well, is it what it is...
Let me know how your rewrite of the loop goes. Often, redoing someone else's code is a good learning method.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|