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
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 August 5th, 2006, 07:37 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default About Ch10 Exercise 4

Yes, I have downloaded and read the answer to this question. And then I thought of another way to solve it. I tried to display five random cards with my new code. But it always displays the first five,that it always flush! Anyone please tell me why?

Question: Write a console client for the Ch10CardLib library that draws five cards at a time from a shuffled Deck object. If all five cards are the same suit, then the client should display the card names on screen along with the text Flush!; otherwise, it should quit after 50 cards with the text No flush.

my new code:

using System;
using System.Collections.Generic;
using System.Text;
using Ch10CardLib;


namespace Ch10Exercise04
{
    class Program
    {
        static void Main(string[] args)
         {

            Deck theNewDeck = new Deck();
            theNewDeck.Shuffle();
            Card[] chosenCards = new Card[5];
            Random randomNum = new Random();
            bool[] cardChosen = new bool[52];
            for (int i = 0; i < 5; i++)
            {
                int tempInt = 0;
                bool tempBool = false;
                while (tempBool == false)
                {
                    tempInt = randomNum.Next(52);
                    if (cardChosen[tempInt] == false)
                        tempBool = true;
                }
                chosenCards[i] = theNewDeck.GetCard(tempInt);
                cardChosen[tempInt] = true;
            }
            foreach (Card chosenCard in chosenCards)
            {
                Console.WriteLine(chosenCard.ToString());
            }
            Console.ReadKey();

        }
    }
}




Reply With Quote
  #2 (permalink)  
Old August 5th, 2006, 07:56 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's reference files

Deck.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace Ch10CardLib
{
    public class Deck
    {
        private Card[] cards; //field;

        public Deck() //Constructor to intialize a deck of cards
        {
            cards = new Card[52];
            for (int suitVal = 0; suitVal < 4; suitVal++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                {
                    cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
                }
            }
        }
        public Card GetCard(int cardNum) //get one card in the cards array.
        //That one card has already got a value during Deck()'s initialization.
        {
            if (cardNum >= 0 && cardNum <= 51)
                return cards[cardNum];
            else
                throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must between 0 and 51."));
        }
        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;
                }
                newDeck[destCard] = cards[i];
                assigned[destCard] = true;
            }
            newDeck.CopyTo(cards, 0);
        }
    }
}


//Card.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace Ch10CardLib
{
    public enum Suit
    {
        Club,
        Diamond,
        Heart,
        Spade
    }
    public enum Rank
    {
        Ace = 1,
        Deuce,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King
    }
    public class Card
    {
        public readonly Suit suit;
        public readonly Rank rank;

        private Card()
        {
        }
        public Card(Suit newSuit, Rank newRank)
        {
            suit = newSuit;
            rank = newRank;
        }
        public override string ToString()
        {
            return "The " + rank + " of " + suit + "s";
        }
    }
}


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
VB Ch10 ObjectDataSource Rich57 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 May 16th, 2008 05:40 AM
ch10 action.saveErrors() AlienLoi BOOK: Professional Jakarta Struts 0 May 11th, 2007 04:37 AM
Errors in the book @ ch10 freezotic BOOK: Beginning Java 2 0 March 26th, 2004 04:54 PM



All times are GMT -4. The time now is 09:34 AM.


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