I have files on my computer containing all cards within a deck. What I want to do is generate all 54 numbers and give 7 of them to the user in the form of buttons so they can play these cards, another 7 cards to the computer, and the remaining cards stored in a queue for either of them to press to get more if needed. But the error I am facing is generating the 7 random numbers that need to be stored for the user to get.
This is the code I am referring to:
I believe this part of the code calls the SetButtonImage to begin to compile on load.
public FmClassic()
{
InitializeComponent();
SetButtonImage();
}
This is SetButtonImage gathering the images from within my computer as well as calling another function to generate the random numbers.
private void SetButtonImage()
{
Stack<int> ShuffleOnTime = ShuffleOneTime();
string Cards = String.Join("\n", ShuffleOnTime);
button1.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(0, Cards.IndexOf(Environment.NewLine))}.png");
button2.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(1, Cards.IndexOf(Environment.NewLine))}.png");
button3.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(2, Cards.IndexOf(Environment.NewLine))}.png");
button4.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(3, Cards.IndexOf(Environment.NewLine))}.png");
button5.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(4, Cards.IndexOf(Environment.NewLine))}.png");
button6.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(5, Cards.IndexOf(Environment.NewLine))}.png");
button7.Image = Image.FromFile($"C:\\filepath\\{Cards.Substring(6, Cards.IndexOf(Environment.NewLine))}.png");
}
This is then called to ensure they are random and not called twice with the hash table as well as converting to a string which I think is better for when searching for files.
public Stack<int> ShuffleOneTime()
{
Random random = new Random();
Stack<int> generatednumbers = new Stack<int>();
Stack<int> randomNumbers = GenerateRandomNumber(random, generatednumbers);
return randomNumbers;
}
This helps generate the numbers and return it back to the other function.
Stack<int> GenerateRandomNumber(Random random, Stack<int> generatednumbers)
{
//Queue<int> Cards = new Queue<int>();
//Queue<string> realNumbers = new Queue<string>();
int randomNumber;
do
{
randomNumber = random.Next(1, 54);
generatednumbers.Push(randomNumber);
if (generatednumbers.Count >= 2)
{
int Compare1 = generatednumbers.Pop();
int Compare2 = generatednumbers.Pop();
if (Compare1 == Compare2)
{
generatednumbers.Push(Compare1);
}
}
} while (generatednumbers.Count != 53);
//string number = generatednumbers.ToString();
//realNumbers.Enqueue(number);
return generatednumbers;
}
I have tried making it a string rather than an integer to help find it but it won't work. The filenames for the cards are all 1.jpg, 2.jpg etc so I don't know why there is an error. Everything compiles but then my screen freezes for 60 seconds and them inputs this error:
Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0xa73c68 to COM context 0xa73bb0 for 60 seconds.