Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2011, 01:11 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 10 page 272

Whole code:

public void shuffle()
{
Card[] newdeck=new Card[52];
bool[] assigned=new bool[52];
Random sourcegen=new Random();
for (int i=0;i<52;i++)
{
int destcard=0;
bool foundcard=false;
while (foundcard==false)
{
destcard=sourcegen.Next(52);
if (assigned[destcard]==false)
foundcard=true;
}

assigned[destcard]=true;
newdeck[destcard]=cards[i];

}
newdeck.CopyTo(cards,0);


what exactly next lines do:

for (int i=0;i<52;i++)
{
int destcard=0;
bool foundcard=false;
while (foundcard==false)
{
destcard=sourcegen.Next(52);
if (assigned[destcard]==false)
foundcard=true;
}

Especially this part is confusing:if (assigned[destCard] == false)

I have found alternative and more understanding code on this link:Chapter 10 Shuffle code page 266

Last edited by dampyr; November 13th, 2011 at 01:20 PM..
 
Old November 13th, 2011, 01:46 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by dampyr View Post
Whole code:


Especially this part is confusing:if (assigned[destCard] == false)
The if statement is explained on page 73. This statement requires an expression that returns true or false. If it returns true, the next statement is invoked.

assigned is an array of Booleans. Arrays are explained on page 110. With the brackets the array implement an indexer. destCard is an int that is passed to the indexer of the array to access a single element.

The returned element of the array is compared to false with the == operator. The == operator is explained on page 60.

If the returned element from the assigned array returns false, the comparision false == false returns true, and thus the next statement is invoked.
If the returned element from the assigned array returns true, the comparision true == false returns false, and thus the next statement is not invoked.

Instead of writing
if (assigned[destcard] == false)

you can also write
if (!assigned[destcard])
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
dampyr (November 13th, 2011)
 
Old November 13th, 2011, 02:02 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Thanks,now all is clear.
 
Old November 18th, 2011, 04:31 AM
Registered User
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by dampyr View Post

Especially this part is confusing: if (assigned[destCard] == false)
This piece of code also had me puzzled for a while, but for a different reason. You seemed to be comparing the value of unassigned array element value to another given value.

Looking on the Net I discovered that arrays of boolean type (at least those initialized by size only via the new keyword) store a default value of false for all its elements.

In this context, the logic of the code in this example then made sense.

Hope this helps someone.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10 Page 359 Try It Out shilohcoder BOOK: Beginning ASP.NET 4 : in C# and VB 2 March 4th, 2011 10:03 AM
Page 354 Chapter 10 tariq BOOK: Beginning ASP.NET 4 : in C# and VB 4 August 20th, 2010 10:33 AM
Chapter 10, Page 334 Saranjiv BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 July 20th, 2010 04:39 PM
Chapter 8 pps 272-273 Featheriver BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 January 31st, 2009 05:00 PM
Ex 10, Chapter 5, page 253 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 16th, 2006 01:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.