Val
Val

Reputation: 525

Compare two point arrays and check if there are any same items

I have a 4x4 board. And I need to create 2 random Point variables and then see that those values have not been randomised before.

I have 2 Point array variables, called Item1 and Item2

Let's say that after the first loop I have Item1[0] = (1,0) and Item2[0] = (3,2)
at the second loop I should not get (1,0) and (3,2) as values
at the third loop I should not get items from first, or second loop

Now, for the coming loops I need to find a way so that the above items are not selected again!

My code:

Random rnd = new Random();
Point[] Item1 = new Point[8];
Point[] Item2 = new Point[8];
for (int x = 0; x < 8; x++)
{
//create coordinates for the first item
    Item1[x].X = rnd.Next(8);
    Item1[x].Y = rnd.Next(8);
//create coordinates for the second item
    Item2[x].X = rnd.Next(8);
    Item2[x].Y = rnd.Next(8);
}

I was thinking on creating a List<Point> and add each point in the list. But I have no idea how to check all items on the list, and how to find that those items do not match.

Thank you.

p.s. I want to create a memory game. And that's why I need 2 points at a time and that's why it's important that I have unique points.

EDIT: Thanks. For future reference, This is how I did it.

Random rnd = new Random();
Point[] Item1 = new Point[8];
Point[] Item2 = new Point[8];
List<Point> usedPoint = new List<Point>();
for (int x = 0; x < 8; x++)
{
        do
        {
            Item1[x].X = rnd.Next(8);
            Item1[x].Y = rnd.Next(8);
        }
        while(usedPoint.Contains(new Point(Item1[x].X,Item1[x].Y))== true);
        usedPoint.Add(new Point(Item1[x].X,Item1[x].Y));
        do
        {
            Item2[x].X = rnd.Next(8);
            Item2[x].Y = rnd.Next(8);
        }
        while (usedPoint.Contains(new Point(Item2[x].X, Item2[x].Y))== true);
        usedPoint.Add(new Point(Item2[x].X, Item2[x].Y));
}

Upvotes: 1

Views: 1323

Answers (2)

Alexander V.
Alexander V.

Reputation: 1538

you could use Hashset for performance quick code sample: you will need something like that

var random = new Random();
var randomSet = new HashSet<int>();
const int size = 8 * 4; // 4 because for each coordinate and each item
for (int i = 0; i < size; i++)
{
    var r = random.Next();
    if (!randomSet.Contains(r)) {
        randomSet.Add(r);
    }
}
var uniqueRandowmNumbers = randomSet.ToArray();
int k = 0;
for (int x = 0; x < 8; x++)
{

//create coordinates for the first item
    Item1[x].X = uniqueRandowmNumbers[k++];
    Item1[x].Y = uniqueRandowmNumbers[k++];
//create coordinates for the second item
    Item2[x].X = uniqueRandowmNumbers[k++];
    Item2[x].Y = uniqueRandowmNumbers[k++];
}

Upvotes: 0

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

There are several ways to solve your issue. If the number of points grow you large you should consider using an Hashtable for performance sake. If you want to use a List<Point> list, you can use the list.Contains(new Point(x,y)) to check if such point is present.

More references about List<T>.Contains here: http://msdn.microsoft.com/en-us/library/bhkz42b3(v=vs.110).aspx

Upvotes: 2

Related Questions