Skip to main content
3 of 3
added 110 characters in body
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309

I'm not convinced that what you are doing really makes sense, but I did see the recursive pattern in your code and I believe that this is how you can refactor it.

First of all, we create a method that you can call recursively:

void DoSomething(int index) {
    for (Pointers[index] = 0; Pointers[index] < 8; Pointers[index]++) {
        if (AdjM(Pointers[index], Pointers[index - 1]))
            if (Pointers[index] == VertEnd)
                AddPath(index + 1);

        if (index < 7) {
            DoSomething(index + 1);
        }
    }
}

Then, unfortunately, the outermost loop did not completely match this pattern so the first loop remains, but it can be used to call the second:

for (Pointers[0] = 0; Pointers[0] < 8; Pointers[0]++)
    if (AdjM(Pointers[0], VertStart))
    {
        DoSomething(1);
    }

A few other notes:

  • It is recommended to always use braces
  • Your number '8' is a 'magic number'. That should be extracted to a constant. Then you can also rewrite '7' as YOUR_MAGIC_NUMBER - 1
  • VertStart + 0 can be simplified to simply VertStart
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309