Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm proud of it, so give a nonexpert a break. That said, any constructive feedback would be most welcome.

For a non-expert it's pretty good. There are a few inconsistencies but I'm sure you can spot those and polish them out in due course. The only bit which really grates is this:

# GENERATE THE BOARD my @board; my $inversions; GEN: @board = (0, shuffle(1..15)); # @board = (1,0,2..15); # for testing $inversions = 0; for my $a(1..15) { for my $b(1..15) { ++$inversions if($a<$b and $board[$a]>$board[$b]) } } goto GEN if $inversions % 2;

There's really no need for a goto in here. You are clearly aware of conditional loops in Perl as you've used them elsewhere in this script. Let's re-write this to avoid the goto, avoid the special variables $a and $b and make it marginally more efficient by only checking the triangle rather than the square.

my @board; my $inversions = 1; while ($inversions % 2) { @board = (0, shuffle(1..15)); $inversions = 0; for my $x (1 .. 15) { for my $y ($x + 1 .. 15) { $inversions++ if $board[$x] > $board[$y]; } } }

Hopefully I have not altered the logic of your board construction at all, just tweaked the code to do the same thing but in a slightly more Perlish way. For completeness I would probably put this in its own subroutine and just call my @board = setup_board() in the main script as the setup is entirely independent of the rest of the script.

In reply to Re: The 15 Puzzle by hippo
in thread The 15 Puzzle by msh210

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (4)
    As of 2020-09-19 03:54 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (114 votes). Check out past polls.