Reputation: 171
I am developing an App which uses recursion.
void Keres(char[,] array, int width, int height)
{
_found = Search(array, height, width, _words);
if (_found.Count < 6)
{
_found.Clear();
Keres(array, width, height);
}
}
Search is a recursive method, and it gives back a string List. And I need the count of it to be bigger than 5. But if it's not, I have to call again and again the Keres method, until it's count is 6 or greater, but my app freezes.
Here is where I call Keres method:
if ((string)appSettings["gamelanguage"] == "english")
{
szo = EngInput(3, 3); //szo is a char[,] array
Keres(szo, 3, 3);
}
What can I do to avoid the recursion, or avoid the crash, and get my >6 items?
Edit: Search method
List<string> Search(char[,] letter_table, int height, int width, List<string> words_list)
{
List<string> possible_words = new List<string>();
char[,] _tmp_letter_table = _tmp_letter_table = new char[height, width];
bool possible = false;
foreach (String word in words_list)
{
possible = false;
Array.Copy(letter_table, _tmp_letter_table, width * height);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (_tmp_letter_table[i, j] == word[0])
{
if (IsNeighborTest(word, i, j, height, width, _tmp_letter_table, 0) == true)
{
possible = true;
break;
}
else
{
Array.Copy(letter_table, _tmp_letter_table, width * height);
}
}
}
if (possible == true)
{
possible_words.Add(word);
break;
}
}
}
return possible_words;
}
Upvotes: 1
Views: 166
Reputation: 2552
your code is not a correct recursion, actually you are calling always the same method, each time the recursive method is called something must have been changed, obviously in your code you are never exiting the method and the application freezes.
I think, if I understood what you want, that the problem you are facing is not solvable with recursion.
Maybe the array is something that changes and until is changed to >6 you want to check with the Keres method? Then recursion is not the way to do it.
Upvotes: 2
Reputation: 26078
You can avoid the recursion with a simple loop:
void Keres(char[,] array, int width, int height)
{
do
{
_found = Search(array,height,width,_words);
} while (_found.Count < 6)
}
But if the app is freezing with recursion, it may likely freeze without it since they should do the same thing (this method may avoid a StackOverflow Exception
however if this takes many iterations to complete)
Upvotes: 2