-2

I want to give even number cards to player 1 and odd number cards to player 2 . If any of you guys could do it that will be really appreciated thank you. I know how to do this with array, but I am not sure with list.

class MatchDeck
{

    private const int NUMBER_OF_CARDS = 36;
    private Random r = new Random();
    List<MatchCard> deck = new List<MatchCard>();

    public MatchDeck(){ }

    public void createDeck()
    {

        int[] number = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        string[] suits = { "R[]", "R0", "B0", "B[]" };//these are suits i just made up 
        // deck = new MatchCard[NUMBER_OF_CARDS];
        foreach (var suit in suits)
        {
            for (int num = 2; num <= 10; num++)
            {
                deck.Add(new MatchCard(num, suit));
                //   Console.WriteLine(deck[i++].toString());
            }
        }

        foreach(MatchCard card in deck)
        {
            Console.WriteLine(card.Number + " " + card.Suit);
        }

    }

    public void Shuffle()
    {

        Random ran = new Random();

        int n = deck.Count;

        while (n > 1)
        {
            n--;
            int k = ran.Next(n + 1);
            MatchCard value = deck[k];
            deck[k] = deck[n];
            deck[n] = value;
        }

        foreach (MatchCard card in deck)
        {
            Console.WriteLine(card.Number + " " + card.Suit);
        }
    }

    public void deal()
    {
        List<MatchCard> player1 = new List<MatchCard>();
        List<MatchCard> player2 = new List<MatchCard>();

    }
}
Tim
  • 28,212
  • 8
  • 63
  • 76

2 Answers2

1

What you trying to achieve can be done by using a loop or Linq. If you want to loop through your array or List the condition would be if(deck[i] % 2 == 0) for even numbers. The opposite for odd. But this is already mentioned in the comment of @misha130.

May be you want to read about the Linq possibilities that List offers. Here is a post that gives you a nice example how to extract values from a List according to a condition (like in your case). To get to know the class List and its methods please read the documentation These two particular methods would be of your main interest in this case: FindAll && Where

Now you should have all the necessary information to solve your problem. Good luck. Comment if you are still stuck.

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
1

I've refactored your code so to what you're after. The key part relating to dealing the cards is the .Where((x, n) => n % 2 == 0)& .Where((x, n) => n % 2 == 1) code.

public class MatchDeck
{
    private Random r = new Random();
    List<MatchCard> deck = null;

    public MatchDeck()
    {
        int[] number = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        string[] suits = { "R[]", "R0", "B0", "B[]" };
        deck = number.SelectMany(n => suits, (n, s) => new MatchCard(n, s)).ToList();
    }

    public void Shuffle()
    {
        deck = deck.OrderBy(x => r.Next()).ToList();
    }

    public void Deal()
    {
        List<MatchCard> player1 = deck.Where((x, n) => n % 2 == 0).ToList();
        List<MatchCard> player2 = deck.Where((x, n) => n % 1 == 0).ToList();
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172