Method not returning all elements in a list

Byt3

New member
Joined
Dec 10, 2019
Messages
1
Programming Experience
Beginner
It supposed to return 54 cards, including 2 jokers. But it is only return 41 elements. There are cards missing. Please help...

Generate Deck method:
List<List<string>> GenerateDeck() {
    List<string> ranks = new List<string>();
    List<string> suits = new List<string>();

    suits.Add("clubs");
    suits.Add("spades");
    suits.Add("diamonds");
    suits.Add("hearts");

    for (int i = 1; i <= 13; i++) {
        switch (i) {
            case 1:
                ranks.Add("ace");
                break;
            case 11:
                ranks.Add("jack");
                break;
            case 12:
                ranks.Add("queen");
                break;
            case 13:
                ranks.Add("king");
                break;
            default:
                ranks.Add(i.ToString());
                break;
        }
    }

    Console.WriteLine("ranks count: " + ranks.Count);
    Console.WriteLine("suits count: " + suits.Count);

    List<List<string>> deck = new List<List<string>>();

    foreach (string rank in ranks) {
        foreach (string suit in suits) {
            deck.Add(new List<string> { rank, suit });
        }
    }

    deck.Add(new List<string> { "red", "joker"});
    deck.Add(new List<string> { "black", "joker"});

    Console.WriteLine("deck count: " + deck.Count); // deck count: 54

    Console.WriteLine();
    Console.WriteLine("TEMP DECK:");
    Console.WriteLine();

    foreach (List<string> card in deck)
        Console.WriteLine(card[0] + " " + card[1]);

    return deck; // returns 41 elements
}
 
Last edited by a moderator:
Have you debugged your code? I'm guessing not. When code doesn't behave as expected, the first thing to do is read it. If that doesn't yield any explanation, the next step is to debug it. Set a breakpoint at the top and step through it line by line, examining the state at each step. You should have a clear expectation of what each line will do, so you can test the state before and after that each line is executed to see whether it did that. If you find a line that doesn't behave as expected, that's where you need to concentrate your investigation. Even if you still can't solve the problem, at least you can provide us with far more information, i.e. where the unexpected behaviour occurred, what the expected and unexpected behaviours were and what data was in use at the time.
 
On an unrelated note, while it may be a bit advanced for you at the moment, code for dealing with a deck of cards should probably involve at least a Card class and possibly a Deck class as well, plus enumerations for Suit and Rank.
C#:
public enum Suit
{
    None,
    Hearts,
    Clubs,
    Diamonds,
    Spades
}

public enum Rank
{
    Joker,
    Ace,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King
}

public class Card
{
    public Rank Rank { get; }   
    public Suit Suit { get; }
    
    public Card(Rank rank, Suit suit)
    {
        Rank = rank;
        Suit = suit;
    }
    
    public override string ToString()
    {
        return Rank == Rank.Joker
            ? Rank.ToString()
            : $"{Rank} of {Suit}";
    }
}
A Deck class might inherit Collection<Card> and include its own Shuffle method.
 
Same for me as well. I suspect that the bug in the OP's code is not in the method posted above, but rather in the code that processes the results.
 
Same for me as well. I suspect that the bug in the OP's code is not in the method posted above, but rather in the code that processes the results.
Indeed, which is a perfect demonstration of why one MUST debug. Don't make assumptions about where the issue is. Rn the code and watch it as it executes to find out for sure. That means watching the code itself, not just the UI. You're the developer, not just a user.
 
Just ran your code and I received :
Screenshot_52.jpg

Looks like you're mistaken or you've got something else meddling with your lists.
 
Back
Top Bottom