An If alternative to this?

enonod

Well-Known Member
Licensed User
Longtime User
Is there a more efficient way to compare shapes in an array than multiple If statements please? i.e. patterns are a bit like chess Knight moves.
A few of the 18 tests are shown as example and take account of edges and corners of the grid by use of col/row tests first.

B4X:
If col>3 AND row>3 Then
         If aGrid(col,row,0)=aGrid(col-1,row,0) AND aGrid(col,row,0)=aGrid(col-1,row-1,0) Then
            aDel(1,2)=True : aDel(1,1)=True
         End If
         If aGrid(col,row,0)=aGrid(col,row-1,0) AND aGrid(col,row,0)=aGrid(col-1,row-1,0) Then 
            aDel(2,1)=True : aDel(1,1)=True
         End If
         If aGrid(col,row,0)=aGrid(col,row-1,0) AND aGrid(col,row,0)=aGrid(col-1,row,0) Then 
            aDel(2,1)=True : aDel(1,2)=True
         End If
      End If
'Tests 5-6-17
      If col<12 AND row<12 Then
         If aGrid(col,row,0)=aGrid(col+1,row,0) AND aGrid(col,row,0)=aGrid(col+1,row+1,0) Then 
            aDel(3,2)=True : aDel(3,3)=True
         End If
         If aGrid(col,row,0)=aGrid(col,row+1,0) AND aGrid(col,row,0)=aGrid(col+1,row+1,0) Then
            aDel(2,3)=True : aDel(3,3)=True
         End If
         If aGrid(col,row,0)=aGrid(col,row+1,0) AND aGrid(col,row,0)=aGrid(col+1,row,0) Then
            aDel(2,3)=True : aDel(3,2)=True
         End If
      End If
 

mc73

Well-Known Member
Licensed User
Longtime User
I will try to think of something, but could you please tell me what the aDel are supposed to do? Probably delete previous areas?..
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks mc73. They are to be deleted but but could be anything. No deletions take place until all tests done so the data must be retained rather than immediate.
I just have the feeling that there 'must'? be a better way because the tests are somewhat repetitive. I wondered about some kind of non-sequential pattern matching but have no knowledge of array or matrix manipulation or some alternative mathematical method.
The shapes are mirrored and almost all 'L' shaped.
Please do not expend valuable time on my behalf unless that is what you do for pleasure. But if you know something I will be grateful.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I simply like quizzes and puzzles! Ok, I'll give it a try. But... Talked about 18 tests? Are they quite similar to the ones you posted?
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Yes mc73, just the rest of combinations really, but the same technique
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Ok, so here's one way, I am not very happy with but you can give it a try. (not happy because I can see the symmetry involved but I didn't generate it by code). The idea is to map the neighborhood of the active cell, in a way that we can do our checks from inside a simple loop. The map is shown in the attachment and here's the obviously untested code:
B4X:
dim cMin,rMin,cMax, rMax, base,tempX1,tempY1,tempX2,tempY2 as int
dim tempString as string
tempstring="108111202130718000003140617051604150"
dim combs(9,2)
for c=0 to 2
    for  r=0 to 2
        base=4*(c+3*r)
        tempx1=tempstring.charat(base)
        tempy1=tempstring.charat(base+1)
        tempx2=tempstring.charat(base+2)
        tempy2=tempstring.charat(base+3)
        if col0+c-1>=cmin and row0+r-1>=rmin and col0+c-1<=cmax and row0+r-1<=rmax then
            combs(tempx1,tempy1)=agrid(col0+c-1,row0+r-1)
            combs(tempx2,tempy2)=agrid(col0+c-1,row0+r-1)
        else
            combs(tempx1,tempy1)=invVal:'a preset invalid value, in order to avoid illegal equalities
            combs(tempx2,tempy2)=invVal
        end if
    next
next
for t=0 to 8
    if combs(t,0)=combs(t,1) and combs(t,1)=combs(0,0) then
        'place the aDels here: I don't know what they represent but you could extend the tempString
        'in order to get them in too
    end if
next
Ah, now that I see the map better, there are combinations missing. I see 4, for e.g. cells (2,0)/(3,1), (3,1)/(2,2) etc. This is not really bad, can be handled by adding these combinations and setting the tempstring to have each individual cell to 8 values.
 

Attachments

  • amap.PNG
    amap.PNG
    3.8 KB · Views: 163
Last edited:
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks mc73.
From the centre cell there are in fact 3 tests in each quadrant, (that include the centre), of the 9 cells; plus 2 extra.
Top left quad is
c-1 and c-1-r-1
r-1 and r-1-c-1;
r-1 and c-1
The odd 2 are
c-1 and c+1
r-1 and r+1
In my actual case I have to use a grid test which includes an additional cell on the centre of each side to give the four tests, c-1 and c-2 and the mirrors, which then covers all possible sets that include the centre cell

I have spent some time on your proposal which certainly improves the coding immensely and is reusable as a general principle. However, while following it through, it produced a Eureka moment when I saw your methodology. I now need to rewrite the section that produces the original grid and data using your same method and then analysing it at the same time. Your method (and combining it for both production and analysis) seems like it will reduce my total code by at least half.
I am extremely grateful to you for providing this insight.
[Edit] What did you use to produce your png map?
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Glad I could help! Today I had some second thoughts, don't know, perhaps you could try getting into maps as well, instead of arrays.
Png was created in Excel, manually, so that I can understand better the subject :)
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks again. I think I will stick to what I now have instead of trying to improve yet again, ad infinitum.
 
Upvote 0
Top