Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
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 September 24th, 2004, 04:15 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 10 Answers

1. public class MyClass
        {
            protected string myString;

            public string ContainedString
            {
                set
                {
                    myString = value;
                }
            }

            public virtual string GetString()
            {
                return myString;
            }

            public MyClass(string theMessage)
            {
                myString = theMessage;
            }
        }

        ... Instantiate and call as follows
        MyClass testThis = new MyClass("This is my base class!");
        Console.WriteLine(testThis.GetString());

2. public class MyDerivedClass : MyClass
        {
            public override string GetString()
            {
                return base.GetString() + " (output from derived class)";
            }

            public MyDerivedClass(string theMessage) : base(theMessage)
            {
            }
        }

        ... Instantiate and call as follows
        MyDerivedClass testThis = new MyDerivedClass("This is my derived answer!");
        Console.WriteLine(testThis.GetString());

3. public class MyCopyableClass
        {
            public int theNumber;

            public object GetCopy()
            {
                // Make a shallow copy
                return MemberwiseClone();
            }
        }

        ... Instantiate and compare field value
        MyCopyableClass theOriginal = new MyCopyableClass();
        theOriginal.theNumber = 33;
        MyCopyableClass theDuplicate = (MyCopyableClass)theOriginal.GetCopy();
        // field value in both objects should match
        if (theOriginal.theNumber == theDuplicate.theNumber)
            Console.WriteLine("Test 1: The two field values match each other");
        else
            Console.WriteLine("Test 1: Oh no! The two field values differ");

        theDuplicate.theNumber = 16;
        // Field value now should not match
        if (theOriginal.theNumber == theDuplicate.theNumber)
            Console.WriteLine("Test 2: The two field values match each other");
        else
            Console.WriteLine("Test 2: Oh no! The two field values differ");

4. ... At top of code
        using Ch10CardLib;

        ... Takes many attempts to obtain a flush!!
        int i, j;
        const int rawDeal = 5;
        string theHand, theSuit;
        bool isFlush;

        // Get the deck ready to deal
        Deck myDeck = new Deck();
        myDeck.Shuffle();

        // Deal maximum 50 cards @ 5 cards per hand (loop 10 times)
        for (i = 0; i < 10; i++)
        {
            // Initialize the hand before each deal
            theHand = "";
            // Peek at the suit of first card dealt
            theSuit = myDeck.GetCard((i * rawDeal) + 1).suit.ToString();
            // Presume this hand will be a flush
            isFlush = true;

            // Deal five cards and compare suit to first card
            for (j = 1; j <= rawDeal; j++)
            {
                // Look at card dealt
                Card tempCard = myDeck.GetCard((i * rawDeal) + j);
                // Stow card into your hand
                theHand = theHand + tempCard.ToString() + "\n";
                // Do you still have a flush?
                isFlush = isFlush && tempCard.suit.ToString() == theSuit;
            }

            // Celebrate if you have a flush!
            if (isFlush == true)
            {
                Console.WriteLine(theHand + "Flush!");
                continue;
            }

            // Tell the world you are an unlucky poker player
            if (i == 9 && isFlush == false)
                Console.WriteLine("No flush");
            }
        }
 
Old October 18th, 2005, 12:05 PM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I worked through example 4 and would like to know if it answers the question posed. I know it runs and finds a flush if it exists, but only by reshuffling each hand.

I looked at seblakes code and, to be honest, I don't see how it is keeping track of what cards have already been dealt and therefor not redeal them.

If someone can help me out by modifying my code to only shuffle once and always deal the next card in the deck instead of the same ones over again, that would be great.

Thanks again and heres my code.

Jael

Code:
using System;
using Ch10ClassLib;

namespace Ch10SumEx04
{
    class MyClass
    {
        static void Main(string[] args)
        {
            //Initialize a Clean Deck
            Deck myDeck = new Deck();

            //Deal out 50 cards @ 5 cards per hand @ 10 hands.
            for (int x = 1; x <= 10; x++)
            {
                //Shuffle Deck each hand
                myDeck.Shuffle();

                //Initialize bool to check if flush exists
                bool isFlush = true;
                //Initialize theHand Array and String theSuit
                string[] theHand = new string[5];
                string[] theHandSuit = new string[5];

                //Suit to check against.
                string theSuit;
                theSuit = "";

                //Get the suit of the first card (base zero)
                theSuit = myDeck.GetCard(0).suit.ToString();
                Console.WriteLine (theSuit + " is the suit to Match.\n");

                //Deal 5 Cards from Top of deck
                for(int i = 0; i < 5; i++)
                {
                    Card tempCard = myDeck.GetCard(i);
                    //Console.Write(tempCard.ToString() + "\n");

                    //Put dealt card into hand
                    theHand[i] = tempCard.ToString();
                    //Store suits in hand dealt
                    theHandSuit[i] = tempCard.suit.ToString();
                }

                //Check if the suits match each other in the hand dealt
                foreach (string suit in theHandSuit)
                {
                    if(suit == theSuit)
                    {
                        isFlush = true;
                    }
                    else
                    {
                        isFlush = false;
                        break;
                    }
                }

                //Display hand.
                foreach (string card in theHand)
                {
                    Console.Write(card + "\n");
                }
                if (isFlush == true)
                {
                    Console.WriteLine("*** You have a flush ! ***\n\n");
                }
                else
                {
                    Console.WriteLine("Sorry, you don't have a flush ...\n\n");
                }
            }
        }
    }
}
 
Old October 18th, 2005, 12:22 PM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok ... I think I figured it out. Can someone double check this to make sure?

Heres my code

Code:
using System;
using Ch10ClassLib;

namespace Ch10SumEx04
{
    class MyClass
    {
        static void Main(string[] args)
        {
            //Initialize a Clean Deck
            Deck myDeck = new Deck();

                //Shuffle Deck each hand
                myDeck.Shuffle();

            //Deal out 50 cards @ 5 cards per hand @ 10 hands.
            for (int x = 0; x < 10; x++)
            {

                //Initialize bool to check if flush exists
                bool isFlush = true;
                //Initialize theHand Array and String theSuit
                string[] theHand = new string[5];
                string[] theHandSuit = new string[5];

                //Suit to check against.
                string theSuit;
                theSuit = "";

                //Get the suit of the first card (base zero)
                theSuit = myDeck.GetCard(x * 5 + 1).suit.ToString();
                Console.WriteLine (theSuit + " is the suit to Match.\n");

                //Deal 5 Cards from deck
                for(int i = 0; i < 5; i++)
                {
                    Card tempCard = myDeck.GetCard(x * 5 + i);

                    //Put dealt card into hand
                    theHand[i] = tempCard.ToString();
                    //Store suits in hand dealt
                    theHandSuit[i] = tempCard.suit.ToString();
                }

                //Check if the suits match each other in the hand dealt
                foreach (string suit in theHandSuit)
                {
                    if(suit == theSuit)
                    {
                        isFlush = true;
                    }
                    else
                    {
                        isFlush = false;
                        break;
                    }
                }

                //Display hand.
                foreach (string card in theHand)
                {
                    Console.Write(card + "\n");
                }
                if (isFlush == true)
                {
                    Console.WriteLine("*** You have a flush ! ***\n\n");
                }
                else
                {
                    Console.WriteLine("Sorry, you don't have a flush ...\n\n");
                }
            }
        }
    }
}









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