Search code examples
c#windowsxamlobjectstore

How do I select a random object when a button is clicked? C# XAML Win Store App


So I'm making a windows store app. It's based on the Top Trumps card game. SO far I've got a card that will appear on the left of the screen. When you select a stat, it will be compared to the related stat of the card on the RIGHT of the screen. If you win, the card on the right should change to a random different card. So far this is working but I can't get the cards to stop repeating. At present when the user clicks a button, a method called CardComp is invoked and the card is chosen randomly using a switch statement. Does anyone have a better method of doing this. I'm willing to completely restart if anyone has any ideas. The code is below:

public void CardComp()

int i;
CardsComp nextCardComp;


var randomEnemyFighter = new Random();
for(i = 0; i < 3; ++i)
{
    int ranEnemyF = randomEnemyFighter.Next(0, 3);

    switch (ranEnemyF)
    {
        case 1:
            nextCardComp = new CardsComp();
            nextCardComp.cardNameComp = "Tyson";
            nextCardComp.cardPictureComp = "Assets/tyson.png";
            nextCardComp.powerComp = 96;
            tysNum.Text = nextCardComp.powerComp.ToString();
            nextCardComp.speedComp = 83;
            tysSpeed.Text = nextCardComp.speedComp.ToString();
            myListofCardsComp.Add(nextCardComp);
            listOfCardsComp.ItemsSource = myListofCardsComp;
            break;

        case 2:
            nextCardComp = new CardsComp();
            nextCardComp.cardNameComp = "Groves";
            nextCardComp.cardPictureComp = "Assets/groves.png";
            nextCardComp.powerComp = 84;
            tysNum.Text = nextCardComp.powerComp.ToString();
            nextCardComp.speedComp = 88;
            tysSpeed.Text = nextCardComp.speedComp.ToString();
            myListofCardsComp.Add(nextCardComp);
            listOfCardsComp.ItemsSource = myListofCardsComp;
            break;

        default:
            nextCardComp = new CardsComp();
            nextCardComp.cardNameComp = "Ali";
            nextCardComp.cardPictureComp = "Assets/ali.png";
            nextCardComp.powerComp = 86;
            tysNum.Text = nextCardComp.powerComp.ToString();
            nextCardComp.speedComp = 91;
            tysSpeed.Text = nextCardComp.speedComp.ToString();
            myListofCardsComp.Add(nextCardComp);
            listOfCardsComp.ItemsSource = myListofCardsComp;
            break;
    }
}

myListofCardsComp = new List<CardsComp>();

Solution

  • A simple PickRandom method might serve your purposes. You pass in an IEnumerable of any object type (like a List of Cards) and it will give you a randomly selected one.

    Sample code (note the using System.Linq):

        using System.Linq
    
        public void SetItemSourceToRandomCard()
        {
            var cardsList = new List<CardsComp>();
            cardsList.Add(card1);
            cardsList.Add(card2);
            cardsList.Add(card3);
    
            var myRandomCard = PickRandom(cardsList);
    
            listOfCardsComp.ItemsSource = new List<CardsComp> { myRandomCard };
        }
    
        public T PickRandom<T>(IEnumerable<T> objects)
        {
            var randomItemIndex = new Random().Next(objects.Count());
            return (T)objects.ElementAt(randomItemIndex);
        }