Card Game
So im making a card game, i have 7 dynamic buttons which i want to display 7 shuffled cards, how does one approach this situation
4 Replies
public void AssignCardsToButtons()
{
for (int i = 2; i < Math.Min(deck.Count + 2, 9); i++)
{
// Assuming the CardX methods create dynamic buttons
GetType().GetMethod($"Card{i}").Invoke(this, null);
int cardIndex = i - 2;
if (cardIndex >= 0 && cardIndex < deck.Count)
{
string newdir = @"(where the cards are stored)";
newdir = Path.Combine(newdir, $"{deck[cardIndex].Value.ToLower()}of{deck[cardIndex].Suit.ToLower()}.png");
var dynamicButton = Controls.OfType<Button>().Last(); // Assuming the dynamic button is the last added button
dynamicButton.Click += (sender, e) => MessageBox.Show($"Clicked: {deck[cardIndex]}");
dynamicButton.Image?.Dispose();
dynamicButton.Image = Image.FromStream(new MemoryStream(File.ReadAllBytes(newdir)), true, false);
}
else
{
MessageBox.Show($"Not enough cards in the deck for button {i}");
}
}
}
this is what i have but when i run it i get an error saying
help.
please.
Have you checked your
deck
variable to see whether it's got anything in it?