p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=222)
-   -   About Ch10 Exercise 4 (http://p2p.wrox.com/showthread.php?t=46168)

webworldman August 5th, 2006 07:37 AM

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();

        }
    }
}





webworldman August 5th, 2006 07:56 AM

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";
        }
    }
}




All times are GMT -4. The time now is 04:03 PM.

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