views:

1659

answers:

5

I am trying to write a code for a project that lists the contents of a deck of cards, asks how much times the person wants to shuffle the deck, and then shuffles them. It has to use a method to create two random integers using the System.Random class.

These are my classes:

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Deck mydeck = new Deck();
            foreach (Card c in mydeck.Cards)
            {
                Console.WriteLine(c);
            }
            Console.WriteLine("How Many Times Do You Want To Shuffle?");

        }
    }
}

Deck.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Deck
    {


        Card[] cards = new Card[52];
        string[] numbers = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "J", "Q", "K" };
        public Deck()
        {
            int i = 0;
            foreach(string s in numbers)
            {
                cards[i] = new Card(Suits.Clubs, s);
                i++;

            }
            foreach (string s in numbers)
            {
                cards[i] = new Card(Suits.Spades, s);
                i++;

            }
            foreach (string s in numbers)
            {
                cards[i] = new Card(Suits.Hearts, s);
                i++;

            }
            foreach (string s in numbers)
            {
                cards[i] = new Card(Suits.Diamonds, s);
                i++;

            }
        }

        public Card[] Cards
        {
            get
            {
                return cards;


            }
        }

    }  
}

classes.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{

    enum Suits 
        {
            Hearts,
            Diamonds,
            Spades,
            Clubs

    }
}

Card.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Card
    {
        protected Suits suit;
        protected string cardvalue;
        public Card()
        {
        }
        public Card(Suits suit2, string cardvalue2)
        {
            suit = suit2;
            cardvalue = cardvalue2;
        }
        public override string ToString()
        {
            return string.Format("{0} of {1}", cardvalue, suit);
        }
    }
 }

Please tell me how to make the cards shuffle as much as the person wants and then list the shuffled cards. Sorry about the formatting im new to this site.

A: 

Shuffling a deck of cards is something that seems trivial at first, but usually the algorithm that most people come up with is incorrect.

Jeff Atwood (Coding Horror) wrote a few very good articles on the subject:

http://www.codinghorror.com/blog/archives/001008.html

http://www.codinghorror.com/blog/archives/001015.html

(especially the second one is a must-read)

Philippe Leybaert
This is not what im looking for. I am looking for someone to show me how to shuffle cards through the code i am using
Are you looking for someone to write the code for you or tell you how to do it? (Assuming this is an assignment, you'll get more out of the class if you write it yourself. If you don't do the assignment you usually fail the test.)
NoMoreZealots
+5  A: 

Use Fisher-Yates shuffle.

Your C# code should look something like this:

static public class FisherYates
{
 static Random r = new Random();
 // Based on Java code from wikipedia:
 // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
 static public void Shuffle(int[] deck)
 {
  for (int n = deck.Length - 1; n > 0; --n)
  {
   int k = r.Next(n+1);
   int temp = deck[n];
   deck[n] = deck[k];
   deck[k] = temp;
  }
 }
}
hughdbrown
FisherYates? Hmm, I didn't know there was a name for it. The first time I implement that was on an 8-bit Atari in Basic! I think I was in 8th grade.
NoMoreZealots
i get these errors when i use it in my Deck.cs class Every place it says Deck (yes I did capitalize it to match my code) it says it is a 'type but is used as a 'variable'. and for when it says Deck.Length it says that My application does not have a definition for 'length'
You shouldn't capitalize the variable name 'deck' to match your class name. If you do, yes, you will get an error because you will be confusing a variable with a class.Others have told you what to do:- create an array of 52 integers [0..51] inclusive- shuffle the integers- dealing the cards is removing the top int from the array- cards are represented as strings by translating integer values to card+suitGood luck.
hughdbrown
+2  A: 

I think this is a case where you may just be getting too caught up in the abstraction.

Shuffling a deck of cards in software is a matter of providing the deck to the user in a random order. This doesn't actually require you to shuffle them ahead of time.

Init your deck. (I typically use a number from 1 to 52 to represent the card and mathmatically compute which card is.)

1)Deal a card by using a random number generator to
  pick a card out of the Deck of availible cards.
2)Swap that card with the one at the end of the deck.
3)Decrement a counter pointing to the end of the deck,
  to remove that card from the deck.
4)Goto step 1 until you are done drawing cards.

Edit: And generally speaking, if you have a good random number generator nothing is gained by "Shuffling" it multiple times.

This should be possible using the data structures you've shown. You just need to add a "Draw" method, and member variable to keep track of the end of the deck. If you are hell bent on actually performing the "shuffle" ahead of time, then A your professor's a jerk, B anytime you draw 52 cards the deck will be shuffled. Once you've draw all cards, you need to provide a "DeckEmpty" method, and method to reset the End of Deck to include all cards again.

NoMoreZealots
A: 

The shuffling should work in this manner:

You take two random cards in the deck (the index of the card in the deck is the random numbers) And swap positions of the two cards. For instance take card at index 2 and card at index 9 and have them change place.

And that can be repeated a certain number of times.

The algorithm should look something like this:

int firstNum = rnd.Next(52); int secondNum = rnd.Next(52);

Card tempCard = MyCards[firstNum]; MyCards[firstNum] = MyCards[secondNum]; MyCards[secondNum] = tempCard;

A: 

Hi too difficult ha????

pnn