Wrox Programmer Forums
|
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 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 5th, 2006, 07:37 AM
Authorized User
 
Join Date: May 2006
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();

        }
    }
}




 
Old August 5th, 2006, 07:56 AM
Authorized User
 
Join Date: May 2006
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";
        }
    }
}







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





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