Code: Select all
[[0,6],[1,4],[1,2],[3],[0,4],[0,5],[0,6],[7],[7,8]]
note: I don't think the arrays can be empty, they can however contain more than 2 numbers
[[3],[7,8],[0,1,2,4,5,6]]
take the last group as an example: [0,6] contains 0 and 6.
- (One of) these two number are also found in [0,4] and [0,5] creating [0,4,5,6]
- again these numbers from the last set are found in [1,4]
- this makes [0,1,4,5,6]
- check again to find [1,2]
- making the final total group: [0,1,2,4,5,6]
rinse and repeat for the remainders which result in: [3] and [7,8]
What I need is a routine that does the above automatically.
Any suggestions?
Please note: I don't need fully developed code, just a description of the algorithm is enough.
why
This is part of the random dungeon generator. The numbers correspond with the number of the room and their corridors. E.g. in room 1 (R1) starts corridor 1 (C1). C1 takes of in a random direction until it hits another room in this case R4. From room 4 C4 starts which in this case happened to end up in room 0, etc.
this can end up with all sections connected OR with a couple of dungeon section which are NOT connected. In this example you end up with 3 sections. I need to figure out which is which so I can connect these sections with additional corridors to make the dungeon complete.
You might have noticed that R3 has no corridor, that means that the corridor could not find another room and the process was aborted
this can end up with all sections connected OR with a couple of dungeon section which are NOT connected. In this example you end up with 3 sections. I need to figure out which is which so I can connect these sections with additional corridors to make the dungeon complete.
You might have noticed that R3 has no corridor, that means that the corridor could not find another room and the process was aborted
total utter failure so far
Code: Select all
<pre>
[array = "[[7],[0,4],[3],[1,2],[0,6],[7,8],[0,5],[1,4]]"]
[origArray = array]
[newArray = "[]"]
[while(!json.isEmpty(origArray)), CODE:{
[foreach(item,origArray), CODE:{
<!-- remove item from array so you dont compare with itself -->
[match = 0]
[tmpArray = json.difference(origArray, json.append("[]", item))]
[foreach(item1, tmpArray),CODE:{
[intersect = !json.isEmpty(json.intersection(item,item1))]
[if(intersect), CODE:{
[match = 1]
[subArray = json.union(item, item1)]
[newArray = json.append(newArray, subArray)]
[origArray = json.difference(origArray, json.append("[]",item, item1))]
[pause("array", "origArray","newArray","subArray", "item", "item1")]
''
}]
''
}]
[if(!match), CODE:{
<!-- if an item finds no match at all, add and remove it separately -->
[newArray = json.append(newArray, item)]
[origArray = json.difference(origArray, json.append("[]",item))]
''
}]
''
}]
[pause("array", "origArray","newArray")]
[origArray = newArray]
[newArray = "[]"]
''
}]