Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: The 15 Puzzle

by Tux (Abbot)
on Jun 10, 2020 at 09:27 UTC ( #11117896=note: print w/replies, xml ) Need Help??


in reply to The 15 Puzzle

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

Replies are listed 'Best First'.
Re^2: The 15 Puzzle
by msh210 (Monk) on Jun 10, 2020 at 09:54 UTC

    Thanks.

    Note that not all configurations of the puzzle are solvable. My check for  $inversions % 2 == 0 makes sure we get a solvable configuration. I don't see anything comparable in your code, though it could be I'm just not seeing it.

    $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6
      not all configurations of the puzzle are solvable

      This Numberphile video explains the math behind.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11117896]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-09-23 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (131 votes). Check out past polls.

    Notices?