I don't know where you found that code, but it's pretty poor.
this code for example:
for (int i = 0; i < shuffledDeck.Length; i++)
{
foundCard = false;
while (foundCard == false)
{
card = random.Next(1, 53);
if (Array.IndexOf(shuffledDeck, card) == -1)
{
shuffledDeck[i] = card;
foundCard = true;
}
}
}
How long does it take to "shuffle" the whole deck when you do it like that?
There are 52 "cards" in the deck: so by the half way point (26 cards "shuffled") the odds of a duplication using a random number are 50:50, and they get worse as you put more cards into the pack. So for the last card, there is one possibility in 52 that your card will be unique. Since random numbers are just that, it potentially could take an infinite amount of time to get that unique number!
Don't shuffle cards by generating new values: Write a method that returns a new deck, and then shuffle that:
List<int> NewDeckPlease()
{
return Enumerable.Range(1, 52).ToList();
}
private Random random = new Random();
int[] ShuffleDeck(List<int> deck)
{
int[] result = new int[deck.Count];
for (int i = 0; i < result.Length; i++)
{
int index = random.Next(deck.Count);
result[i] = deck[index];
deck.RemoveAt(index);
}
return result;
}
That takes a fixed amount of time and can easily be extended later if you decide to do your cards in a better way.
For example ... a better way would be to use the OOPs principles that C# is built on: instead of holding cards as an integer between 1 and 52 inclusive, why not create a Card class: that way, the card could have a suit, a colour, a value, and even a picture which would make your code a lot simpler:
public class PlayingCard
{
public enum CardSuit
{
Spade = 0,
Diamond = 1,
Club = 2,
Heart = 3,
}
private static Color[] Colours = new Color[] { Color.Black, Color.Red, Color.Black, Color.Red };
private static string[] Names = new string[] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
public int Value { get; set; }
public CardSuit Suit { get; set; }
public Color Color { get { return Colours[(int)Suit]; } }
public Image Picture { get; set; }
public string Name { get { return Names[Value - 1]; } }
public override string ToString()
{
return $"{Name} of {Suit}";
}
}
Now when you want to compare two cards when looking for a pair it's obvious:
if (cardA.Value == cardB.Value)
...
How were you going to compare values with your system?
I would strongly suggest that you throw away the mixture of code you have assembled from I don't know how many poor students homework, sit down and plan some code for yourself using classes. And you will probably find it's a lot easier and quicker than trying to hammer different bits of code into a working solution!