Seit gestern beschäftige ich mich mit Perl TK,
und da meiner Meinung nach die gute alte "trial & error"-Methode immer noch die beste ist,
hab ich mich dran gemacht ein kleines Sachspiel damit zu programmieren...
Das ganze soll (natürlich) eine grafische Oberfläche bekommen,
die soweit auch steht.
Damit man die Figuren bewegen kann hab ich mir gedacht
dass ich die Felder einfach zu Buttons mache,
die dann an eine Subroutine die Feldkoordinaten als Parameter mitgibt.
Dieser Code soll 8 Buttons, abwechselnd schwarz, bzw weis ausgeben.
Problem hierbei ist
bei dem $j nicht den Wert der Zählervariable der For-Schleife hat, sondern immer irgendeine (im aktuellen Programmdurchlauf immer gleichbleibende) Zahl.
Kann mir jemand sagen warum $j nicht den Wert der Zählervariable erhällt :-?
[ EDIT: ]
ok...
jetzt hat $j in der "command-Anweisung" immer den Wert 9...
obwohl ich eigentlich nix geändert habe... egal...
und da meiner Meinung nach die gute alte "trial & error"-Methode immer noch die beste ist,
hab ich mich dran gemacht ein kleines Sachspiel damit zu programmieren...
Das ganze soll (natürlich) eine grafische Oberfläche bekommen,
die soweit auch steht.
Damit man die Figuren bewegen kann hab ich mir gedacht
dass ich die Felder einfach zu Buttons mache,
die dann an eine Subroutine die Feldkoordinaten als Parameter mitgibt.
Code:
for(my $j=1; $j <=8; $j++){
if($j % 2 == 0){
my $piece = $line -> Button(-text => ($chessboard[($i*8)-9+$j]),
-background => 'white',
-activebackground => 'lightgrey',
-foreground => 'black',
-width => $field_width,
-height => $field_height,
-command => sub{ out($j) })
-> pack(-side => 'left', -anchor => 'w');
}else{
my $piece = $line -> Button(-text => ($chessboard[($i*8)-9+$j]),
-background => 'black',
-activebackground => 'darkgrey',
-foreground => 'white',
-width => $field_width,
-height => $field_height,
-command => sub{ out($j) })
-> pack(-side => 'left', -anchor => 'w');
}
}
Dieser Code soll 8 Buttons, abwechselnd schwarz, bzw weis ausgeben.
Problem hierbei ist
Code:
-command => sub{ out( $j ) }
Kann mir jemand sagen warum $j nicht den Wert der Zählervariable erhällt :-?
[ EDIT: ]
ok...
jetzt hat $j in der "command-Anweisung" immer den Wert 9...
obwohl ich eigentlich nix geändert habe... egal...
Code:
#!/usr/bin/perl
use English;
require Tk;
use Tk;
my $field_width = 9;
my $field_height = 4;
($playername[0],$playername[1]) = ('','');
my @chessboard=('R','K','B','I','Q','B','K','R',
'P','P','P','P','P','P','P','P',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
'p','p','p','p','p','p','p','p',
'r','k','b','i','q','b','k','r');
### CREATE MAIN-WINDOW-OBJECT
my $main = MainWindow -> new();
$main -> minsize( qw(500 550));
$main -> title("KEKSINAT0R's GUI-CHESS v1.0");
$main -> configure(-background => 'darkgrey');
### CREATE MENU BAR:
my $menu_bar = $main -> Frame(-relief => 'groove',
-borderwidth => 3,
-background => 'darkred',)
-> pack(-side => 'top', -fill => 'x');
### CREATE FILE GROUP:
my $file_mb = $menu_bar -> Menubutton(-text => 'File',
-background => 'darkred',
-activebackground => 'darkgrey',
-foreground => 'white')
-> pack(-side => 'left');
$file_mb -> command(-label => 'Exit',
-activebackground => 'black',
-command => sub{$main->destroy});
$file_mb -> separator();
### SPACER:
my $spacer = $main -> Frame(-background => 'yellow')
-> pack(-side => 'top', -fill => 'x');
my $t1 = $spacer -> Label(-text => '', - background => 'black')
-> pack();
### BOARD:
my $center = $main -> Frame(-background => 'black')
-> pack(-side => 'top', -fill => 'both');
for($i=1; $i <=8; $i++){
my $line = $main -> Frame()
-> pack(-side => 'top');
if($i % 2 == 0){
for(my $j=1; $j <=8; $j++){
if($j % 2 == 0){
my $piece = $line -> Button(-text => ($chessboard[($i*8)-9+$j]),
-background => 'white',
-activebackground => 'lightgrey',
-foreground => 'black',
-width => $field_width,
-height => $field_height,
-command => sub{ out($j) })
-> pack(-side => 'left', -anchor => 'w');
}else{
my $piece = $line -> Button(-text => ($chessboard[($i*8)-9+$j]),
-background => 'black',
-activebackground => 'darkgrey',
-foreground => 'white',
-width => $field_width,
-height => $field_height,
-command => sub{ out($j) })
-> pack(-side => 'left', -anchor => 'w');
}
}
}else{
for(our $j=1; $j <=8; $j++){
if($j % 2 == 0){
my $piece = $line -> Button(-text => ($chessboard[($i*8)-9+$j]),
-background => 'black',
-foreground => 'white',
-activebackground => 'darkgrey',
-width => $field_width,
-height => $field_height,
-command => sub{ out($j) })
-> pack(-side => 'left', -anchor => 'w');
}else{
my $piece = $line -> Button(-text => ($chessboard[($i*8)-9+$j]),
-background => 'white',
-activebackground => 'lightgrey',
-foreground => 'black',
-width => $field_width,
-height => $field_height,
-command => sub{ out($j) })
-> pack(-side => 'left', -anchor => 'w');
}
}
}
}
### SPACER:
my $spacer = $main -> Frame(-background => 'yellow')
-> pack(-side => 'top', -fill => 'x');
my $t1 = $spacer -> Label(-text => '', - background => 'black')
-> pack();
MainLoop();
sub out ($){
print @_; print "\n";
}