Hey!

This post is quite long, but please read and try to help me. Thanks!

I was trying to make a program that computes a combination to the following problem:
Use the numbers 0-8 and place them in a 3x3 square in a order to make the summation of every row, col and diagonal equal to 12. One number can only be used one time.
The first possible combination is:
Code:
 1  6  5
 8  4  0
 3  2  7
As you can see, the summation of every row, col and diagonal is equal to 12 (1+6+5=12, 1+8+3=12, 1+4+7=12 etc). In my program, I have a button to compute next possible sulotion. There are 8 possible combinations to this problem (actually a problem from a math-school-book - sorry for my bad english).


Well, my program is generally based, so you can slove any problem of this kind (#x# squares, summation/product=#, horizontal?, vertical?, diagonal?, nums to use, OnlyUseANumberOneTime?). When is was trying to compute if there is any possible combination to this problem:...
Use the numbers 0-8 and place them in a 4x4 square in a order to make the summation of every row, col and diagonal equal to 17. One number can be used more than one time.
...I get a function stack error (too many functions were computed) after a several seconds. I think this limit is approx 2570 (anyway what I've got with my computer). The compution stops and the computer can not show any complete sulotion. I can't see what exactly is the problem, but it must be a function stack limit (in my output window, I only see "...at RowCombine.action(RowCombine.java:397), at RowCombine.action(RowCombine.java:387), at RowCombine.action(RowCombine.java:387), at RowCombine.action(RowCombine.java:397), at RowCombine.action(RowCombine.java:397), at RowCombine.action(RowCombine.java:369)...").

Now I wonder, if there is any way to work around this. My function is running as a thread in the background, and it looks like this (commented and easy version):
Code:
public void actionPerformed(ActionEvent e){ // OK is pressed
	new Thread().start();
}
public void run(){
	action();
}
public void action(){
	indexes[x][y]++; // get and try the next number at (x,y)
	if(indexes[x][y]>=possibleNumbersToUse.length){
		indexes[x][y] = -1; // reset the number at (x,y)
		l[x][y].setText(""); // reset the text at label (x,y)
		backOneBox(); // move to the previous box
		action(); // make next try at new (x,y)
		return;
	}
	l[x][y].setText(Integer.toString(possibleNumbersToUse[indexes[x][y]])); // show which number is used at label (x,y)
	while((useOneNumberOneTime && currentNumberIsInUse()) || rowsColsOrDiagonalSummationNotOK()){
		indexes[x][y]++; // try next number at (x,y)
		if(indexes[x][y]>=possibleNumbersToUser.length){
			indexes[x][y] = -1; // reset the number at (x,y)
			l[x][y].setText(""); // reset the text at label (x,y)
			backOneBox(); // move to the previous box
			action(); // make next try at new (x,y)
			return;
		}
		l[x][y].setText(Integer.toString(possibleNumbersToUse[indexes[x][y]])); // display the new number at label (x,y)
	}
	forwardOneBox(); // move to next box
	action();
	return;
}
I've tried to set a counter to this function. Every time it is called, the counter increases. When the counter is 2500 (2500 functions stacked), the thread stops and creates a new one, which starts at the position where the old one stopped. This makes allows the computer tocall this function approx 4900 times.

You may think my english not is very good, but I did my best. If you want, I can upload my source and you can have a look.


Maybe, there is no possible way to work around this, but it's worth a try...

Thanks for your help! (if you not could help, you should anyway have a thanks for reading this and trying to help me...)
//Totte_ch