# How do I check values in an array?

0 favourites
• 4 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
• Hi,

I am trying to use arrays to build a match fixture generator, but with certain rules.

At the moment I've create a 6 x 5 array (To simulate 6 teams and 5 matches, once each)

I've worked out how to fill each row in the array with a random number between 1 and 6, and ensure its not a duplicate.

The next stage is to check a rule to make sure once all the matches are populated there is not a duplicate match (ie team 1 vs 3 week one. Then again 1 vs 3 in week 2-5)

Is there any good examples of using loops to check this kind of thing?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Worst case you could brute force check each pair and shift things around. Probably a tripple nested loop or something like that.

Instead would it be enough to just randomize the first row and use an offset pattern for the others?

``````//// comment: fill the first col with 1-6. You'd do you random numbering here.
for "x" from 0 to 5
-- array: set at (loopindex("x"), 0) to loopindex("x")

for "y" from 1 to 4
for "x" from 0 to 5
-- array: set at (loopindex("x"), loopindex("y")) to array.at((loopindex("x")+loopindex("y"))%6, 0)``````

Then that spits out an array like this (6x6 actually):

``````123456
234561
345612
456123
561234
612345``````

And from a cursory glance you should be able to just shuffle rows and columns around and there will still be no duplicate matches. So here are some events to swap random rows and columns 100 times:

``````global number i0=0
global number i1=0
global number temp=0
global text axis= "x or y"

repeat 100 times
-- set axis to choose("x","y")
-- set i0 to int(random(6))
-- set i1 to int(random(6))
-- repeat 6 times
-- -- axis equals "x"
-- -- -- set temp to array.at(loopindex, i0)
-- -- -- array: set at(loopindex, i0) to array.at(loopindex, i1)
-- -- -- array: set at(loopindex, i1) to temp
-- -- axis equals "y"
-- -- -- set temp to array.at(i0, loopindex)
-- -- -- array: set at(i0, loopindex) to array.at(i1, loopindex)
-- -- -- array: set at(i1, loopindex) to temp``````
• thanks R0J0hound

I'll look into what you sggested, although the other rules I need to try and work out might mean its not possible. I don;t think what I am trying will actually be possible but wanted to try and work it out.

I've basically got 6 teams to create a league fixture for. Each team plays each other team 3 times (15 games total)

There are three mats that hold the matches each week (Mat 1, Mat 2, Mat 3)

I am trying to work out if it is possible to arrange fixtures in a way that allows each team to play each other 3 times, no team plays the same mat two weeks in a row - ideally each team playing on each mat 3 times. I actually think its mathmateically impossible and I've not been able to work it out in excel.

My goal was to try and build the rules into arrays and leave something trying brute force to see if its possible or not. At the moment I've managed to setup an array to mimic one round of games (6 teams and 5 matches). It can put 1 - 6 in each row and then check to make sure that no pair of combinations are duplicates (for example so that team 1 and 2 play once only in the 5 games etc...)

I was hoping there would be easy ways of checking things like count how many of the same value in each X position of an array :(

• Ah ok. Well here is a different way to do it. since it's only six different teams you can combine the pair in one number, and instead of generating it with code (which you still could do), you could just populate the array with this: It will be a 5x3 array.

Three pairs per week for five weeks.

``````12 13 14 15 16
34 25 35 24 23
56 46 26 36 45``````

you can swap rows/columns and pairs in the same column.

For all 15 weeks. just the above duplicated three times. A 3x15 array.

``````12 13 14 15 16 12 13 14 15 16 12 13 14 15 16
34 25 35 24 23 34 25 35 24 23 34 25 35 24 23
56 46 26 36 45 56 46 26 36 45 56 46 26 36 45``````

You can still swap stuff around but you can cause the same match to occur on consecutive weeks.

To check you can do this:

``````global number x=0
global number y=0

for "x" from 0 to 15-2
for "y" from 0 to 3-1
-- set x to loopindex("x")
-- set y to loopindex("y")
-- if array.at(x,y)=array.at(x+1,y)
-- or array.at(x,y)=array.at(x+1,(y+1)%3)
-- or array.at(x,y)=array.at(x+1,(y+2)%3)
-- -- ... there is the same match on consecutive weeks.
``````

Probably the algo at that point would be:

2. swap stuff around

3. check if there are any repeat matches from one week to the next.

4. if there are repeats go back to 2 and try again.

5. otherwise you have a good layout.

That could succeed first try or never succeed.

You probably can do something smarter by knowing that there are ony five different weeks of pairs.

Then it simplifies to a string a sting of numbers

123451234512345

Then just scramble those around, and then fill the weeks in the array form that. Then all that would be left is to swap items around in each column.

I mean the algo would be the same, just simpler to do.

• 4 posts