1

Im writing a program where I have a Deck of cards (MyDeck Object) which contains a list (List of type Card) within which are my cards (Card Objects). There is also a Magazine (Magazine Object) with a list of cards (List of type string) for sale. I want to add cards that are in the magazine to my deck only if I dont have the card yet. The difficult part is that my cards are objects while the ones in the magazine are strings, so I have to call mycard.getCardName() to get the name of my card and with the cards in the magazine I can simply run down the list because they are simply a list of strings.

Here is my code for that and the problem is that only the first 2 cards in the magazine are added to my deck.

Perhaps Im approaching the problem wrong.

public static class MyDeck
{
    public static List<Card> Cards = new List<Card>();
    //Other Members
}

public class Card
{
    //associated memebers   
}

public class Magazine
{
    public List<string> MagCards = new List<string>();
    //Other Members
}


private void AddCardsToMyDeck(List<string> MagCards)
{
    //If I have no cards in my deck then add the first one on the magazine to my deck
    if(MyDeck.MyCards.Count == 0)
    {
       Card c = new Card(MagCards[0]);
       MyDeck.MyCards.Add(c);
    }

    //for each name of each card in the magazine       
    foreach(string MagCard in MagCards)
    {
      //for each card in my deck
      foreach (Card crd in MyDeck.MyCards)
      {
         //if the my card is in the magazine then don't add it, else add it
         if (MagCards.Contains(crd.getCardName()))
         {
            break;
         }
         else
         {
            Card cd = new Card(MagCard);
            MyDeck.MyCards.Add(cd);
         }
      }
    }
}


// NOTE: MyDeck.Cards is empty
Magazine.MagCards.Add("B. Ruth");
Magazine.MagCards.Add("M. Piazza");
Magazine.MagCards.Add("J. Robinson");
AddCardsToMyDeck(Magazine.MagCards);

//Only Ruth and Piazza are added

I think you're doing part of it backwards. The text of your question says you want to add cards from the magazine to your deck if the card isn't already in your deck, but the code looks at cards in your deck and tries to do something if they aren't in the magazine. Either way, you're doing this the hard way by writing for loops:

private void AddCardsToMyDeck(List<string> MagCards)
{
    var newCards = MagCards.Except(MyDeck.MyCards.Select(c => c.getCardName()));
    MyDeck.MyCards.AddRange(newCards.Select(c => new Card(c)).ToArray());
}
4

3 回答 3

1

I am not sure I understand what your question is, but to solve the problem you are presenting you could use linq to transform the list first into a List of names.

  foreach (string crd in from c in MyDeck.MyCards select c.getCardName() )
  {
     if ( MagCards.Contains( crd ) )
     {
        break;
     }
     else
     {
        Card cd = new Card(MagCard);
        MyDeck.MyCards.Add(cd);
     }
  }

Joel Coehoorn presents a good answer for this as a design issue.

于 2013-10-03T17:02:01.057 回答
1
// get card names in your deck
var deckCardNames = MyDeck.Cards.Select( c => c.getCardName() );
// get card names in magazine cards that are not in your deck
var missingMagCardNames = MagCards.Except( deckCardNames );
// instantiate card objects for each missing card name
var missingCards = missingMagCardNames.Select(mc => new Card(mc));
// add missing cards to your deck
MyDeck.Cards.AddRange( missingCards );

// whole thing inline
MyDeck.Cards.AddRange( MagCards.Except( MyDeck.Cards.Select( c => c.getCardName() ) ).Select (mc => new Card(mc) ) );
于 2013-10-03T17:14:45.117 回答
1

我认为你在做它的一部分。您的问题文本说,如果卡片尚未在您的套牌中,您想将卡片从杂志中添加到您的套牌中,但代码会查看您套牌中的卡片,如果它们不在杂志中,则尝试做一些事情. 无论哪种方式,您都通过编写 for 循环来实现这一点:

private void AddCardsToMyDeck(List<string> MagCards)
{
    var newCards = MagCards.Except(MyDeck.MyCards.Select(c => c.getCardName()));
    MyDeck.MyCards.AddRange(newCards.Select(c => new Card(c)).ToArray());
}
于 2013-10-03T17:00:43.073 回答