Click here to Skip to main content
15,868,120 members
Home / Discussions / C#
   

C#

 
JokeRe: North wind Database Pin
Kornfeld Eliyahu Peter3-Feb-14 20:19
professionalKornfeld Eliyahu Peter3-Feb-14 20:19 
AnswerRe: North wind Database Pin
Marco Bertschi3-Feb-14 21:04
protectorMarco Bertschi3-Feb-14 21:04 
QuestionWorking with Bitmaps Pin
BBatts3-Feb-14 7:45
BBatts3-Feb-14 7:45 
AnswerRe: Working with Bitmaps Pin
TnTinMn4-Feb-14 6:56
TnTinMn4-Feb-14 6:56 
QuestionProject Question Pin
reaper21913-Feb-14 5:15
reaper21913-Feb-14 5:15 
AnswerRe: Project Question Pin
Alan Balkany3-Feb-14 5:20
Alan Balkany3-Feb-14 5:20 
AnswerRe: Project Question Pin
Pete O'Hanlon3-Feb-14 5:48
subeditorPete O'Hanlon3-Feb-14 5:48 
AnswerRe: Project Question Pin
OriginalGriff3-Feb-14 6:11
mveOriginalGriff3-Feb-14 6:11 
There are a number of ways to do this, but the way I'd probably start is by creating a Card and Suit:
C#
public enum Suit
    {
    Spades = 0,
    Diamonds = 1,
    Clubs = 2,
    Hearts = 3,
    }
public class Card
    {
    private int _Value;
    public Suit Suit { get { return (Suit)(_Value / 13); } }
    public int Value { get { return _Value % 13; } }
    public Card(int value)
        {
        if (value < 0 || value >= 52)
            {
            throw new ArgumentException("Invalid Card : " + value);
            }
        _Value = value;
        }
    }

Then, it's a simple matter of creating the deck and shuffling it:
C#
private List<Card> deck = new List<Card>();
private Random rand = new Random();
private Card GetNextCard(List<Card> deck)
    {
    if (deck.Count <= 0)
        {
        Enumerable.Range(0, 52).Select(c => new Card(c)).ToList();
        deck = Shuffle(deck);
        }
    Card card = deck[0];
    deck.RemoveAt(0);
    //deck.Add(card); //Uncomment this line to return cards to the bottom of the deck.
    return card;
    }
private List<Card> Shuffle(List<Card> deck)
    {
    List<Card> shuffled = new List<Card>();
    int cards = deck.Count;
    for (int i = 0; i < cards; i++)
        {
        int index = rand.Next(deck.Count);
        shuffled.Add(deck[index]);
        deck.RemoveAt(index);
        }
    return shuffled;
    }
Then just "peel off" the cards you want:
C#
Card c1 = GetNextCard(deck);
Card c2 = GetNextCard(deck);
Card c3 = GetNextCard(deck);
Card c4 = GetNextCard(deck);

Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

GeneralRe: Project Question Pin
reaper21913-Feb-14 6:20
reaper21913-Feb-14 6:20 
GeneralRe: Project Question Pin
OriginalGriff3-Feb-14 6:43
mveOriginalGriff3-Feb-14 6:43 
GeneralRe: Project Question Pin
Matt T Heffron3-Feb-14 8:06
professionalMatt T Heffron3-Feb-14 8:06 
GeneralRe: Project Question Pin
reaper21913-Feb-14 17:09
reaper21913-Feb-14 17:09 
AnswerRe: Project Question Pin
Matt T Heffron3-Feb-14 18:44
professionalMatt T Heffron3-Feb-14 18:44 
AnswerRe: Project Question Pin
V.3-Feb-14 20:22
professionalV.3-Feb-14 20:22 
QuestionCreating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 0:11
Mohan Subramani3-Feb-14 0:11 
AnswerRe: Creating a small project using C# - Please Help Pin
Pete O'Hanlon3-Feb-14 0:18
subeditorPete O'Hanlon3-Feb-14 0:18 
QuestionRe: Creating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 1:02
Mohan Subramani3-Feb-14 1:02 
AnswerRe: Creating a small project using C# - Please Help Pin
Richard MacCutchan3-Feb-14 0:22
mveRichard MacCutchan3-Feb-14 0:22 
QuestionRe: Creating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 1:16
Mohan Subramani3-Feb-14 1:16 
AnswerRe: Creating a small project using C# - Please Help Pin
Richard MacCutchan3-Feb-14 2:18
mveRichard MacCutchan3-Feb-14 2:18 
GeneralRe: Creating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 2:48
Mohan Subramani3-Feb-14 2:48 
JokeRe: Creating a small project using C# - Please Help Pin
Ravi Bhavnani3-Feb-14 6:15
professionalRavi Bhavnani3-Feb-14 6:15 
GeneralIssues in creating SQL Server Database with C# windows Application Pin
Mohan Subramani4-Feb-14 3:15
Mohan Subramani4-Feb-14 3:15 
GeneralRe: Issues in creating SQL Server Database with C# windows Application Pin
Ravi Bhavnani4-Feb-14 6:24
professionalRavi Bhavnani4-Feb-14 6:24 
AnswerRe: Creating a small project using C# - Please Help Pin
John D. Sanders4-Feb-14 9:13
John D. Sanders4-Feb-14 9:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.