use strict; use warnings; my @Feld = ([ 3, 6, 4, 3, 7, 3, 3, 6 ], [ 6, 5, 8, 7, 6, 8, 2, 8 ], [ 5, 5, 7, 4, 5, 2, 7, 7 ], [ 6, 3, 4, 1, 6, 8, 8, 3 ], [ 1, 5, 7, 7, 5, 1, 7, 4 ], [ 7, 2, 8, 5, 2, 3, 3, 2 ], [ 2, 2, 4, 8, 1, 1, 8, 3 ], [ 2, 5, 3, 1, 7, 5, 7, 8 ]); my @Possible; my @TestFeld; for my $y (0..7) { for my $x (0..7) { if ($x != 7) { my $qualityR = TestMove(\@Feld, $x, $y, 'right'); if ($qualityR > 0) { push(@Possible, [$x, $y, $x+1, $y, $qualityR]) } } if ($y != 7) { my $qualityD = TestMove(\@Feld, $x, $y, 'down'); if ($qualityD > 0) { push(@Possible, [$x, $y, $x, $y+1, $qualityD]) } } } } foreach my $Move (@Possible) { print "Swap $$Move[0]/$$Move[1] with $$Move[2]/$$Move[3] for $$Move[4] Points\n"; } sub TestMove { if ($_[3] eq 'right') { @TestFeld = @{$_[0]}; my $ToSwap = ${$TestFeld[$_[2]]}[$_[1]]; ${$TestFeld[$_[2]]}[$_[1]] = ${$TestFeld[$_[2]]}[$_[1]+1]; ${$TestFeld[$_[2]]}[$_[1]+1] = $ToSwap; my $quality = GetQuality(\@TestFeld); return $quality; } elsif ($_[3] eq 'down') { @TestFeld = @{$_[0]}; my $ToSwap = ${$TestFeld[$_[2]]}[$_[1]]; ${$TestFeld[$_[2]]}[$_[1]] = ${$TestFeld[$_[2]+1]}[$_[1]]; ${$TestFeld[$_[2]+1]}[$_[1]] = $ToSwap; my $quality = GetQuality(\@TestFeld); return $quality; } } sub GetQuality { my $ref = $_[0]; my $quality = 0; for my $x (0..7) { my $string = join("", @{@$ref[$x]}); while ($string =~ m/1{3,5}|2{3,5}|3{3,5}|4{3,5}|5{3,5}|6{3,5}|7{3,5}|8{3,5}/g) { $quality += length($&); } undef $string; } for my $y (0..7) { my $string = ""; for my $z (0..7) { $string = $string . ${@$ref[$z]}[$y]; } while ($string =~ m/1{3,5}|2{3,5}|3{3,5}|4{3,5}|5{3,5}|6{3,5}|7{3,5}|8{3,5}/g) { $quality += length($&); } undef $string; } return $quality; }