Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

NICE! A big PLUS to you. The generation of the board can go into an endless loop, and can be done much simpler directly:

# GENERATE THE BOARD my @board = shuffle 1 .. 15; splice @board, int rand 16, 0, 0;

I've added my version here, which is how I would re-write yours (TIMTOWTDI):

use 5.18.3; use warnings; #se Data::Peek; use List::Util qw( shuffle first ); use Term::TransKeys; say <<"INTRO"; Welcome to the 15 puzzle! Use an arrow key to move a block into the empty position. You're trying to reach either of these two positions: 01 02 03 01 02 03 04 04 05 06 07 05 06 07 08 08 09 10 11 09 10 11 12 12 13 14 15 13 14 15 ^C to stop. INTRO # GENERATE THE BOARD my @board = shuffle 1 .. 15; splice @board, int rand 16, 0, 0; # PLAY THE GAME sub printboard { for (my $start = 0; $start < 16; $start += 4) { say join " " => map { sprintf ("%02d", $_) =~ s/00/ /r } @board[$start .. ($start + 3)]; } say ""; } # printboard say "The starting position is:"; printboard (); my $listener = Term::TransKeys->new; my %ok = map {( "@$_" => 1 )} [0 .. 15], [1 .. 15, 0]; my %keys = map { $_ => 1 } qw( <UP> <DOWN> <RIGHT> <LEFT> <CONTROL+C> +); while (1) { defined (my $key = $listener->TransKey) or redo; #DPeek $key; $keys{$key} or redo; $key eq "<CONTROL+C>" and do { warn "Have a great day!\n"; exit 0; + }; if ($key eq "<UP>" and grep { !$_ } @board[12 .. 15]) { print "You can't move up. The empty space is at the bottom."; next; } if ($key eq "<DOWN>" and grep { !$_ } @board[ 0 .. 3]) { print "You can't move down. The empty space is at the top."; next; } if ($key eq "<RIGHT>" and grep { !$_ } @board[0, 4, 8, 12]) { print "You can't move right. The empty space is at the left."; next; } if ($key eq "<LEFT>" and grep { !$_ } @board[3, 7, 11, 15]) { print "You can't move left. The empty space is at the right."; next; } my $zero = first { !$board[$_] } 0..15; $key eq "<UP>" and @board = @board[0 .. ($zero - 1), $zero + 4, ($zero + 1) .. ($zero + 3) +, $zero, ($zero + 5) .. 15 ]; $key eq "<DOWN>" and @board = @board[0 .. ($zero - 5), $zero, ($zero - 3) .. ($zero - 1), $z +ero - 4, ($zero + 1) .. 15 ]; $key eq "<RIGHT>" and @board = @board[0 .. ($zero - 2), $zero, $zero - 1, ($zero + 1) .. 15]; $key eq "<LEFT>" and @board = @board[0 .. ($zero - 1), $zero + 1, $zero, ($zero + 2) .. 15]; printboard (); $ok{"@board"} and last; } say "You've solved it!"

Enjoy, Have FUN! H.Merijn

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

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, 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (7)
    As of 2020-09-29 14:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If at first I donít succeed, I Ö










      Results (146 votes). Check out past polls.

      Notices?