Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning Visual C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2005
This is the forum to discuss the Wrox book Beginning Visual C# 2005 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780764578472
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 9th, 2008, 12:37 PM
Registered User
 
Join Date: Jun 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 10 Shuffle code page 266

Dear Sir/Madam,

I just recently purchased this book, and I should say that this book is pretty good for beginner like me. But sometimes I find the exercises are a bit tricky. Some of them are not covered in the topic or even the previous topics, but it is a good way to learn to look at the help section. Now I'm stuck at page 266 where I cannot understand the following code. I hope that someone can help me understand. Thank you.

 1 public void Shuffle()
 2 {
 3 Card[] newDeck = new Card[52];
 4 bool[] assigned = new bool[52];
 5 Random sourceGen = new Random();
 6 for (int i = 0; i < 52; i++)
 7 {
 8 int destCard = 0;
 9 bool foundCard = false;
10 while (foundCard == false)
11 {
12 destCard = sourceGen.Next(52);
13 if (assigned[destCard] == false)
14 foundCard = true;
15 }
16 assigned[destCard] = true;
17 newDeck[destCard] = cards[i];
18 }
19 newDeck.CopyTo(cards, 0);
20 }

Now this code I found it very confusing. On Line 13
"if (assigned[destCard] == false)" What is the value of assigned[destCard]? Is it false or is it true? I cannot find any assignment of boolean value. Can anyone please explain it to me, perhaps using an example please. Thank you.

Reply With Quote
  #2 (permalink)  
Old September 26th, 2008, 09:53 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

There are probably a bazillion ways to shuffle a deck of cards. How about this:

    public void shuffleDeck()
    {
        int index = 1;
        int val;
        Random rnd = new Random();

        // Clear out the array
        System.Array.Clear(deck, 0, deck.Length);

        // DECKSIZE is a symbolic constant set to 52. If another
        // game using more than one deck is used (e.g., canasta)
        // this could be changed

        while (index != DECKSIZE + 1) {
            val = rnd.Next(DECKSIZE + 1); // Values between 1 and 52
            if (deck[val] == 0) // Empty card element??
            {
                deck[val] = index; // Yep,assign it a card place
                index++; // Get ready for next card
            }
        }
    }

This code assumes deck[] has already been set elsewhere to DECKSIZE + 1 elements. I use the "+1" because I want the cards to run from 1 through 52. Using a zero-based array would go from 0-51. (I have some visual card elements that make non-zero easier to use.)

So how does it work? (You can find the full details in my book.) Suppose the first pass generates the random value 18 for val. The code examines element 18 to see if a value is already there. If not, the element value is 0 and the value of index (1) is assigned to element 18. My algorithm interprets this as the Ace of Spades is located at element 18. On the next pass, suppose val is 42. If that element is unused (0), deck[42] gets 2 (the 2 of spades) assigned into it since that's the current value of index. Now suppose the third pass generates 18 again. Now the test shows deck[18] is non-zero so that space is already filled. It will generate another value to place the third card, and so on.

As the array fills up with cards, there are more and more "collisions" with used deck elements. Still, it rarely takes more than 300 passes through the loop to "shuffle" the deck. While I'm sure there are more efficient algorithms, this is an easy one to teach.

Dr. Purdum


Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Errata - Chapter 10, Page 377, C# RobC BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 5 October 3rd, 2006 02:26 AM
Ex 10, Chapter 5, page 253 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 16th, 2006 01:14 AM
Chapter 10, Try It Out error, page 380 VictorVictor BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 2 February 13th, 2006 12:41 PM
Chapter 10 Sample Code jramkawsky BOOK: Professional Java Development with the Spring Framework 0 December 3rd, 2005 08:44 AM



All times are GMT -4. The time now is 08:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.