-4

I have 5 separate image lists that all need to pull 5 values different from each other. I have been trying several different ways but haven't found one that works. The program is supposed to be a poker hand so they all need to be different cards.

This is what I have. (deleted some due to not working). What do I need to do to achieve these 5 different cards?

namespace Random_Card
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void getCardButton_Click(object sender, EventArgs e)
        {
            Random rand = new Random();

            int index = rand.Next(cardImageList.Images.Count);
            int index1 = rand.Next(cardImageList.Images.Count);
            int index2 = rand.Next(cardImageList.Images.Count);
            int index3 = rand.Next(cardImageList.Images.Count);
            int index4 = rand.Next(cardImageList.Images.Count);

            cardPictureBox.Image = cardImageList.Images[index];
            cardPictureBox1.Image = cardImageList.Images[index1];
            cardPictureBox2.Image = cardImageList.Images[index2];
            cardPictureBox3.Image = cardImageList.Images[index3];
            cardPictureBox4.Image = cardImageList.Images[index4];



            while (index == index1)
            {
                new Random();
            }
        }
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 1
    Please read [ask] and take the [tour]. When a NET component doesnt seem to work - run, dont stroll, to MSDN and confirm that you are using it correctly. Also picking cards randomly from a list is nothing like shuffling a deck and dealing them. Random picks wont preclude dupes/repeats – Ňɏssa Pøngjǣrdenlarp Apr 26 '18 at 18:37
  • 1
    You only need a single instance of the `Random` object for a class, and it should be initialized once. – Rufus L Apr 26 '18 at 18:41
  • and you will need to add a data structure to keep track of the card indices you aready dealt, so when the same random number comes again repeat randomizing until an available card index is hit (bad idea) or pre-sort the list of cards with a random sort order into a queue or stack (better idea). – Cee McSharpface Apr 26 '18 at 18:48
  • 1
    If you are attempting to simulate a deck of cards then *write a simulation of a deck of cards*. Make a data structure that represents a card, make a data structure that represents a deck, and write operations which shuffle and deal from the deck. Programming is the art of making abstractions that represent concepts, so start doing that. – Eric Lippert Apr 26 '18 at 18:49

1 Answers1

1

First of all, to use Random, you should only create a single instance for your class and initialize it once. Otherwise you run the risk of getting the same number from them if called in a loop, since it's initialized by the system clock.

Secondly, if you want to pick five unique indexes from a list, you can simply create a list of all the indexes, shuffle them, and then pick the first five from that.

In the code below, I use Enumerable.Range(0, cardImageList.Images.Count) to get a sequential list of cardImageList.Images.Count numbers, starting at 0. This is simplified syntax that could be replaced with a for loop. Next I use the System.Linq extension method OrderBy to sort the index list, and I pass Random.NextDouble() to it. This makes it order the list in a random way; basically a simple way to "shuffle" a list. After the list of indexes is shuffled, I use the Take() extension method to take the first five items from the list:

namespace Random_Card
{
    public partial class Form1 : Form
    {
        private Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void getCardButton_Click(object sender, EventArgs e)
        {
            // Get five unique random indexes
            List<int> shuffledIndexes = Enumerable.Range(0, cardImageList.Images.Count)
                .OrderBy(x => rand.NextDouble()).Take(5).ToList();   

            cardPictureBox.Image = cardImageList.Images[shuffledIndexes[0]];
            cardPictureBox1.Image = cardImageList.Images[shuffledIndexes[1]];
            cardPictureBox2.Image = cardImageList.Images[shuffledIndexes[2]];
            cardPictureBox3.Image = cardImageList.Images[shuffledIndexes[3]];
            cardPictureBox4.Image = cardImageList.Images[shuffledIndexes[4]];          
        }
    }
}

Update: You mentioned that you didn't know how to do this in a loop, so here's an example of the "long-hand" method to do the same thing, which may make more sense.

var shuffledIndexes = new List<int>();

// Get a sequential list of integers representing the indexes
for (int i = 0; i < cardImageList.Images.Count; i++)
{
    shuffledIndexes.Add(i);
}

// Shuffle the items in the index list by randomly swapping each item with another
for (int i = 0; i < cardImageList.Images.Count; i++)
{
    // Pick a random item to swap with
    var swapIndex = rand.Next(shuffledIndexes.Count);

    // If we picked the current item, continue without doing anything
    if (swapIndex == i) continue;

    // Otherwise, swap the current item with the random item
    var temp = shuffledIndexes[i];
    shuffledIndexes[i] = shuffledIndexes[swapIndex];
    shuffledIndexes[swapIndex] = temp;
}

// Assign the images from the first five shuffled indexes
cardPictureBox.Image = cardImageList.Images[shuffledIndexes[0]];
cardPictureBox1.Image = cardImageList.Images[shuffledIndexes[1]];
cardPictureBox2.Image = cardImageList.Images[shuffledIndexes[2]];
cardPictureBox3.Image = cardImageList.Images[shuffledIndexes[3]];
cardPictureBox4.Image = cardImageList.Images[shuffledIndexes[4]];
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • I tried fixing it and its throwing an error. _italic_rand is giving me an issue, it says "A namespace cannot directly contain members such as fields or methods". Also i've tried looking how but dont know how I would implement that code in a for loop. I am learning C# at the moment so there is a lot I dont know. just trying to do projects. – Michael Daniels Apr 26 '18 at 19:43
  • Sorry that's my fault... Random should be in the class, not the name space. I fixed it now. – Rufus L Apr 26 '18 at 19:50
  • No problem. I added another example that uses `for` loops to create the list of indexes and to shuffle the list. It might be easier to understand. – Rufus L Apr 26 '18 at 20:16