in reply to How to debug a long while loop

nbezalla:

I don't want to solve a homework problem for you, especially as you've made such a great start at it. Here's a hint that may help you out:

Unless you're specifically avoiding it, recursion would be your best bet here. You're doing a lot of work in your code that recursion would "automatically" take care of for you. You're making a complex data structure (board) to track a handful of state variables that you would get "free" using recursion.

For example, your board array is rather complicated. You could use local variables to store most of your state information for each move. Something like:

sub solve { my $move = shift; ... other code ... solve($move+1,...); }
Here, rather than putting the $move variable into your board structure, you're letting perl keep track of them for you on the stack. As you call solve, you get a new copy of $move, and when you return, you get the previous value back.

Using local variables and recursion, you should be able to use an 8x8 array of integers to track which squares have been used, the rest being local variables.

If you need another hint or two, let me know.!

(Note: Just as an example of how using recursion can simplify things a bit, I cobbled together a quickie solution that's a little less than half the size of yours.)

...roboticus

Replies are listed 'Best First'.
Queen's problem (was Re^2: How to debug a long while loop)
by roboticus (Chancellor) on May 17, 2007 at 14:22 UTC
    nbezalla:

    I recalled another amusing chess puzzle--specifically the "queens problem". It's another one that's easily solved with recursion. It might be too much of a hint, so I've put spoiler tags on it (below) so you don't see the solution to it if you don't want to. Anyway, the queens problem (on the offhand chance that you're unfamiliar with it) is basically to place eight queens on a chessboard such that none can attack another.

    My solution is:

    ...roboticus