Reputation: 599
My plan is to place each element in the GridPane on a JavaFX program. But the javafx isn't actually my problem is that I couldn't really go further in the abstraction to create an algorithm with the following characteristics:
This is an example of the algorithm by itself so you don't have to look at unecessary javaFX elements.
public static void main(String[] args) {
String[] v1 = {null,"1",null,"3"};
String[] v2 = {"0","\"1",null,"3"};
String[] v3= {"4","5","6","","8","9","","11","12","13"};
int row=0, col=0, index=0;
String[] v = concat(v1, v3);
for(String s: v){
if(s != null && !s.isEmpty()){
if(index<3){
if(col<2){
col++;
index++;
}
else{
col=0;
row++;
System.out.println("");
}
//System.out.println("index="+index);
System.out.print(" "+s);
}
}
}
The desired output for v1, v3 is:
1 3 6
4 5 8
11 12 13
Desired output for v2, v3 is:
0 1 5 12
3 4 6 13
8 9 11
As you can see: the objective is to begin at a upperleft square and expand that square when is necessary while not putting any null or empty
items that might be in the concatenated array.
My code works well for the first 4, but I cannot really think about how should I expand it in a generic way. How can I write an generic algorithm for such task?
Edit for clarification: using the number of the placed element the placement pattern should look like this:
1 2 5 10
3 4 6 11
7 8 9
Upvotes: 0
Views: 94
Reputation: 604
Here is some Python code I wrote from what I understood about your problem. Hopefully you will be able to translate it.
The main idea is the following: if you finished a square, go fill the rightmost part, from top to bottom, then the bottom part, from left to right.
from pprint import pprint
t = range(1,92)
n = len(t)
s = int(n**.5+1)
mat = [[None]*s for _ in range(s)]
i, j = 0, 0
k = 0
st = "right"
while k<n:
mat[i][j] = t[k]
k += 1
if i==j and st=="right":
i = 0
j += 1
st = "down"
elif st == "right":
j += 1
elif i+1==j and st=="down":
i += 1
j = 0
st = "right"
else:
i += 1
pprint(mat)
Upvotes: 2