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


In reply to Re: How to debug a long while loop by roboticus
in thread How to debug a long while loop by nbezzala

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.