Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 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 August 20th, 2003, 06:54 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default Chapter 10 Q4

Been playing with this last question, so far I got the following which keeps drawing cards until it gets a flush:
Code:
        static void Main(string[] args)
        {
            Deck myDeck=new Deck();        //the full deck of cards
            Card[] hand=new Card[5];    //the hand of 5 cards
            bool flush=false;            //flush or not?
            int i=0,j=0,k=1;
            myDeck.Shuffle();            //shuffle the deck

            while (flush==false)
            {
                for(i=0;i<50;i+=5)
                {
                    for (j=0;j<5;j++)
                    {
                        hand[j]=myDeck.GetCard(i+j);
                        Console.WriteLine("Card {0}: {1}-{2}",i+j+1,hand[j].suit,hand[j].rank);
                    }
                    flush=sameSuit(hand);
                    Console.WriteLine("==============");
                }
                myDeck.Shuffle();
                k++;
            }
            Console.WriteLine("FLUSH! After {0} packs.",k);
            Console.ReadLine();
        }

        static bool sameSuit(Card[] hand)
        {
            //loop through all cards starting at the 2nd
            for (int i=1;i<hand.Length;i++)
            {
                //if the current suit is different from the previous then return false
                if (hand[i].suit != hand[i-1].suit)
                    return false;
            }
            //we got here so all cards must be the same suit
            return true;
        }
But I've got a few q's about it
a) is there any really stupid bits of code?
b) although the number of packs needed to get a flush varies, it is always the last cards drawn that produce the flush...this seems highly unlikely. so i) is it my code that's telling porkies ii) or just that the shuffle method is pretty crap?



 
Old September 7th, 2003, 02:21 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to HuhOiC
Default

Maybe it's your shuffle method. If not i don't get why you have two for loops? The first one is 0, 5, 10, ... , 50

while the inner for does 0 ,1, 2, 3, 4 and rewrites you hand all the time. So everytime you get a card it will be i+j which is

i=0
hand[0] = 0
hand[1] = 1
...

i=5
hand[0] = 0
hand[1] = 6
...

so their always constant, unless your shuffle really shuffles. Has anything I said helped you or sparked anything, let us know.


RAyRAy
Huh O i C

Program or Be Programmed!!!
 
Old September 7th, 2003, 01:53 PM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default

Yep I realized it was the loop

Code:
                for(i=0;i<50;i+=5)
                {
                    for (j=0;j<5;j++)
                    {
                        hand[j]=myDeck.GetCard(i+j);
                        Console.WriteLine("{0}:{1}:{2}: {3}-{4}",k,i,j,hand[j].suit,hand[j].rank);
                    }
                    flush=sameSuit(hand);
                    Console.WriteLine("==============");
                    if (flush) break;
                }
                myDeck.Shuffle();
                k++;


But I just need the break line to solve it






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10: TreeView example postoak7 BOOK: Access 2003 VBA Programmer's Reference 0 September 19th, 2006 12:25 PM
Chapter 10 gogeo BOOK: Beginning Access 2003 VBA 1 January 22nd, 2006 09:41 AM
Chapter 10 czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 March 29th, 2005 09:35 AM
Chapter 10 columbiasmiles JSP Basics 0 May 17th, 2004 08:09 PM





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