1 2 3 4 5 6 7 8 9 10
@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 ] );
1 2 3 4 5 6 7
for my $y (0..7) { for my $x (0..7) { push(@moves,find_move(\@Feld,$x,$y)); } }
1 2 3 4 5 6
return ( {posx=>$x, posy=>$y, dirx=>0, diry=>1, quality=>5}, {posx=>$x, posy=>$y, dirx=>0, diry=>-1, quality=>1}, {posx=>$x, posy=>$y, dirx=>1, diry=>0, quality=>3}, {posx=>$x, posy=>$y, dirx=>-1, diry=>0, quality=>1}, );
@moves=sort{$b->{quality} <=> $a->{quality}}@moves;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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; }
${$TestFeld[$_[2]]}[$_[1]]
$$TestFeld[$_[2]][$_[1]]
@{@$ref[$x]}
@{$$ref[$x]}
$string=join('',map{$$ref[$_][$x]}(0..7));
undef $string;
1 2 3 4 5 6 7 8
my @TestFeld; for my $y (0..$#Feld) { for my $x (0..$#{$Feld[$y]}) { $TestFeld[$y][$x]=$Feld[$y][$x]; } }
1 2 3 4 5 6 7 8 9 10
sub mach_was { my $x=shift; # ist das gleiche unten my $y=shift(); # das gleiche wie unten my $feld=shift(@_); # besagt: verschiebe den ersten Wert aus @_ in $feld my @rest=@_; # ... # code # ... }
2009-07-26T22:32:19 topeg
$TestFeld[ $_[2] ]->[ $_[1] ]
${$TestFeld[$_[2]]}[$_[1]]
$TestFeld[$_[2]][$_[1]]
Quotemit Leerzeichen sollte man nicht sparen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
sub GetMoves { my @Moves; for my $y (0..7) { for my $x (0..7) { if ($x != 7) { my $qualityR = TestMove(\@{$_[0]}, $x, $y, 'right'); if ($qualityR > 0) { push(@Moves, [$x, $y, $x+1, $y, $qualityR]) } } if ($y != 7) { my $qualityD = TestMove(\@{$_[0]}, $x, $y, 'down'); if ($qualityD > 0) { push(@Moves, [$x, $y, $x, $y+1, $qualityD]) } } } } return @Moves; } sub TestMove { foreach my $temparray (@{$_[0]}) { push(@TestFeld, [@$temparray]); } if ($_[3] eq 'right') { my $ToSwap = ${$TestFeld[$_[2]]}[$_[1]]; ${$TestFeld[$_[2]]}[$_[1]] = ${$TestFeld[$_[2]]}[$_[1]+1]; ${$TestFeld[$_[2]]}[$_[1]+1] = $ToSwap; } elsif ($_[3] eq 'down') { my $ToSwap = ${$TestFeld[$_[2]]}[$_[1]]; ${$TestFeld[$_[2]]}[$_[1]] = ${$TestFeld[$_[2]+1]}[$_[1]]; ${$TestFeld[$_[2]+1]}[$_[1]] = $ToSwap; } my $quality = GetQuality(\@TestFeld); undef @TestFeld; return $quality; } sub GetQuality { my $ref = $_[0]; my $quality = 0; for my $x (0..7) { my $string = join("", @{@$ref[$x]}); while ($string =~ m/(11111|1111|111)|(22222|2222|222)|(33333|3333|333)|(44444|4444|444)|(55555|5555|555)|(66666|6666|666)|(77777|7777|777)|(88888|8888|888)/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/(11111|1111|111)|(22222|2222|222)|(33333|3333|333)|(44444|4444|444)|(55555|5555|555)|(66666|6666|666)|(77777|7777|777)|(88888|8888|888)/g) { $quality += length($&); } undef $string; } return $quality; }
Guest TheDudeHier gibt's eins auf Deutsch: http://wiki.perl-community.de/cgi-bin/foswiki/view...Hm durch die Referenzen Blick ich irgendwie noch nicht durch.. Werd mir da nochmal nen paar Tuts durchlesen..