- Home /
Method to loop through an unknown number of variable combinations in C#
Goal:
My goal is to set up a function that will find all possible combinations of letters that make words (assuming you can combine the letters in any direction, without using the same object twice or combining objects that are not next to each other).
Context:
I have a grid of objects with randomly picked letters as children. Each object has a script attached that has the other letters immediately around it stored in a GameObject[] variable. Like this:
0------1------2
7------X------3
6------5------4
I already have way to check against the letter combinations to see if they are actual words.
The "allLetters" variable below is an array of all the letter objects.
void FindPossibleWords()
{
foreach(GameObject al in allLetters)
{
string word = al.GetComponent<Letter>().letter;
for(int i = 0; i < 7; i++)
{
word += al.GetComponent<Letter>().near[i].GetComponent<Letter>().letter;
if("Letters can still potentially make a real word")
{
// Repeat?
}
}
}
}
I'm not looking for someone to write the code for me. I just can't get my brain around a method/strategy for looping through an "unknown" number of times.
I can already see that this will not work the way I am going about it. Essentially I am looking for a way for it to loop through an unknown number of letter combinations and then store the ones that are real words.
I apologize for the minimal amount of code, mostly I am stuck on what strategy to use to get the result. I feel like I could get the result with a ridiculous amount of code, but there is usually some other option that I am missing.
I appreciate any help.
Thanks,
Howey
Answer by Seizure · Sep 26, 2013 at 03:48 AM
OK, A little late for me to take a crack at this but lets hope I can shed some light for you...
Anyways I would use a circular method, so take X as your starting position, circle out from that position and continously check against you library of words, if it counts as one maintain that in an array and continue on with your array. I will try and psuedo code this for you...
string word = "";
for (int i = 0; i<gridWidth.Length; i++)
{
for (int k = 0; k<gridHeight.Length; k++)
{
for (int l = 0; l< i; l++)
{
word = Array.join(word,i,k);//This isn't the right syntax I hardly ever use this command
}
}
}
I hope this gets you on the way, if it helps but not quite gets you all the way let me know where you're stuck and I'll help some more.
Ok, sorry for not getting back to you sooner. I was gone over the weekend. I'm not sure I fully understand how this would work or at least I guess I don't get where the other pieces would fit in.
$$anonymous$$y GameObject[]s currently only contain the immediately adjacent GameObjects which have a GameObject[] variable that contains the ones immediately around it, etc. The concept of this method was that it would be easier to transition in any direction.
It looks like you are theorizing using a multidimensional array? Or is it just a normal array that contains all of the letter objects?
And thank you again for answering so quickly.
Howey
Thank you for the help, I appreciate it, and it helped me get to where I wanted to go.
Your answer
Follow this Question
Related Questions
Loop through array until certain value is found. 2 Answers
Trouble with for loop out of range. Simple? Maybe. 1 Answer
How to correctly shorten this script using arrays and iterations 2 Answers
Checking an array variable in C# vs JavaScript 3 Answers
How to create an image gallery with previous and next button with C#? 6 Answers