0

I'm currently trying to write my first program so my coding isn't nearly as knowledgeable as others...

I am trying to shuffle a deck of cards without using the exact code given in the Fischer-Yates method or at least my version of it...

My plan is to to assign the cards to an enum and that will assign them a number from 0-51. Then I set the cards in a list and remove the card from the list that the RNG generates. After the card is removed from the list the counter deletes one from the possible random numbers and I set the number to a temporary variable that I will then pass to a new list that will be the shuffled deck.

My issue is in finding code that will explicitly convert my first list to an int. I think I know how to remove from the first list using .RemoveAt. Will I need to make another conversion or can i store the Random Number I find into the shuffledDeck?

enum Cards
{
    AceSpade,
    TwoSpade,
    ThreeSpade,
    FourSpade,
    FiveSpade,
    SixSpade,
    SevenSpade,
    EightSpade,
    NineSpade,
    TenSpade,
    JackSpade,
    QueenSpade,
    KingSpade,
    AceHeart,
    TwoHeart,
    ThreeHeart,
    FourHeart,
    FiveHeart,
    SixHeart,
    SevenHeart,
    EightHeart,
    NineHeart,
    TenHeart,
    JackHeart,
    QueenHeart,
    KingHeart,
    AceDiamond,
    TwoDiamond,
    ThreeDiamond,
    FourDiamond,
    FiveDiamond,
    SixDiamond,
    SevenDiamond,
    EightDiamond,
    NineDiamond,
    TenDiamond,
    JackDiamond,
    QueenDiamond,
    KingDiamond,
    AceClub,
    TwoClub,
    ThreeClub,
    FourClub,
    FiveClub,
    SixClub,
    SevenClub,
    EightClub,
    NineClub,
    TenClub,
    JackClub,
    QueenClub,
    KingClub
}

class DeckShuffle
{
    static Random rndCard = new Random(DateTime.Now.Millisecond);
    int currentCard;
    int tempCard;
    int totalCards = 51;
    List<Cards> currentDeck = new List<Cards>();
    List<int> shuffledDeck = new List<int>();

    private void CardShuffler()
    {
        while (totalCards > -1)
        {
            tempCard = rndCard.Next(0, 51);
            totalCards--;
        }
    }
}
crashmstr
  • 28,043
  • 9
  • 61
  • 79
Jetsker
  • 13
  • 4
  • It is somewhat confusing what you actually have problem with as there is really no shuffling code in your sample. Since such question comes up a lot I closed this as duplicate of similar problem. I'd recommend to carefully read code in question and answer so you can either get answer to your question or will be able to narrow down problem you have to several lines of code (see [MCVE] for guidance on creating good sample for posts). – Alexei Levenkov Feb 04 '16 at 05:56

1 Answers1

0

So you have a list of cards, currentDeck.

I'd recommend a for loop to have an integer value like:

List<Cards> currentDeck = new List<Cards>();
List<Cards> shuffledDeck = new List<Cards>();

for(int i = 0; i < currentDeck.Count - 1; i++) {
    //Grab a card, insert it into ShuffledDeck
    int randoNum = rndCard.Next(0, 51-i); //This will grab a new number between 1 and however many cards are left
    shuffledDeck.Add(currentDeck[randoNum]);
    currentDeck.RemoveAt(i);
}

This way, every loop as i increases, the number of cards left in currentDeck decreases, and we grab one, and add it to the shuffled deck.


Edit: I'd probably recommend creating a Card class though (if you're that far into learning C#) - each "Card" object would have a numeric value (1-13, 11 for Jack, 12 for Queen, 13 for King, etc) and a suit.

That'll make most card-based games a lot easier.

Corey Thompson
  • 398
  • 6
  • 18