-1

I'm doing simple Xamarin.Forms puzzle game and i need to have 9 puzzles with diffrent random values. I tried to check it with some loops, but it's still not working.

Random r = new Random();

            Label[] puzzles = { puz1, puz2, puz3, puz4, puz5, puz6, puz7, puz8, puz9 };
            string[] used = new string[9];
            for (int i = 0; i < puzzles.Length; i++)
            {
                if (i > 0)
                {
                    for (int x = 1; x < used.Length; x++)
                    {
                        do
                        {
                            puzzles[i].Text = Puzzles.puz[r.Next(0, 8)];
                            used[x] = puzzles[i].Text;
                        }
                        while (used[x - 1] == used[x]);
                    }
                }
                else
                {
                    puzzles[i].Text = Puzzles.puz[r.Next(0, 8)];
                    used[0] = puzzles[i].Text;
                }
            }

And Puzzles.cs class

class Puzzles
    {
        public static string[] puz = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };


    }

How can I check that new generated puzzle hasn't the same value that puzzles generated previously?

Jason
  • 86,222
  • 15
  • 131
  • 146
wstylok
  • 3
  • 1
  • 3
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details as per the How to Create a [mcve] page. Please [edit] your question to add these details into it or we may not be able to help. – gunr2171 Mar 28 '19 at 18:06
  • Possible duplicate of [Easiest way to compare arrays in C#](https://stackoverflow.com/questions/3232744/easiest-way-to-compare-arrays-in-c-sharp) – NineBerry Mar 28 '19 at 18:06
  • 2
    Are you trying to shuffle an array? This is a really bad way to shuffle an array. Implement a Knuth shuffle; there are hundreds of examples on this site or the internet at large. – Eric Lippert Mar 28 '19 at 18:07
  • "Checking for duplicate random values" almost always means you should really be using a shuffle: take N values and put them in random order. – Ňɏssa Pøngjǣrdenlarp Mar 28 '19 at 18:17

2 Answers2

0

This is because you only check the preceding value for duplicates, making it still possible for used[x -2] == used[x] to be true.

To achieve your goal, I would suggest implementing a shuffle function, like the one you can find here. It can give something like this

// Implemented somewhere in your code
private List<E> ShuffleList<E>(List<E> inputList)
{
     List<E> randomList = new List<E>();

     Random r = new Random();
     int randomIndex = 0;
     while (inputList.Count > 0)
     {
          randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
          randomList.Add(inputList[randomIndex]); //add it to the new, random list
          inputList.RemoveAt(randomIndex); //remove to avoid duplicates
     }

     return randomList; //return the new random list
}

// Then for each element of your puzzles array, you could do
puzzles[i].Text = SuffleList(Puzzles.puz);
Olivier Samson
  • 609
  • 4
  • 13
0

Thanks all for help, I didn't know Shuffle mechanism. Finally my 'working' code presents as follows

        static Random rnd = new Random();

        static void Shuffle<T>(T[] array)
        {
            int n = array.Length;
            for (int i = 0; i < n; i++)
            {
                int r = i + rnd.Next(n - i);
                T t = array[r];
                array[r] = array[i];
                array[i] = t;
            }
        }

        public Game()
        {
            InitializeComponent();


            Label[] puzzles = { puz1, puz2, puz3, puz4, puz5, puz6, puz7, puz8, puz9 };

            string[] puz = { "1", "2", "3", "4", "5", "6", "7", "8" };

            Shuffle(puz);
            for (int i = 0; i < puzzles.Length - 1; i++)
            {
                puzzles[i].Text = puz[i];
            }
        }
wstylok
  • 3
  • 1