Hi, all!
For your review, I have posted a game I wrote to improve visual memory. ( It seems that visual memory is at the root of all learning, together with emotion. ) Blue is correct placement, red is incorrect placement, and white is non-placement. As one gets better, the array should be increased ( with the individual boxes reduced ) in size.
It works, but the code is hideous, I'm sure, particularly how the array of buttons is put together. I'm interested in what can be learned through this. If you know how it can be optimized without obfuscation, please post!
For your review, I have posted a game I wrote to improve visual memory. ( It seems that visual memory is at the root of all learning, together with emotion. ) Blue is correct placement, red is incorrect placement, and white is non-placement. As one gets better, the array should be increased ( with the individual boxes reduced ) in size.
It works, but the code is hideous, I'm sure, particularly how the array of buttons is put together. I'm interested in what can be learned through this. If you know how it can be optimized without obfuscation, please post!
Code:
use Tk;
my $mw = MainWindow->new;
$mw->title('Recreate the Pattern of X\'s');
$f = $mw->fontCreate( -family => "Verdana", -size => 50);
$h = 1;
$w = 2;
$n = 35;
$enter = $mw->Button(-height=>$h/2)->grid(-row=>0,-column=>0,-columnspan=>7,-sticky=>"nsew");
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>1,-column=>1));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>1,-column=>2));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>1,-column=>3));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>1,-column=>4));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>1,-column=>5));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>1,-column=>6));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>2,-column=>1));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>2,-column=>2));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>2,-column=>3));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>2,-column=>4));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>2,-column=>5));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>2,-column=>6));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>3,-column=>1));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>3,-column=>2));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>3,-column=>3));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>3,-column=>4));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>3,-column=>5));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>3,-column=>6));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>4,-column=>1));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>4,-column=>2));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>4,-column=>3));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>4,-column=>4));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>4,-column=>5));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>4,-column=>6));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>5,-column=>1));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>5,-column=>2));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>5,-column=>3));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>5,-column=>4));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>5,-column=>5));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>5,-column=>6));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>6,-column=>1));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>6,-column=>2));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>6,-column=>3));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>6,-column=>4));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>6,-column=>5));
push(@button,$mw->Button(-height=>$h,-width=>$w)->grid(-row=>6,-column=>6));
$enter -> configure ( -text => "New Pattern", -command => \&new_pattern );
foreach ( @button ) { $_ -> configure ( -text => "", -font=> $f ); }
foreach ( 0..$n ) { $random[$_] = 0 }
foreach ( 0..$n ) { $entry[$_] = 0 }
MainLoop;
sub new_pattern {
$enter -> configure ( -text => "Start", -command => \&start );
foreach ( 0..$n ) { $random[$_] = 0 }
foreach ( 0..$n ) { $entry[$_] = 0 }
for ( 1..10 ) { $random[rand($n)]=1 }
foreach ( 0..$n ) {
$button[$_] -> configure ( -text => "", -foreground => "black", -command => \&park );
if ( $random[$_]==1 ){
$button[$_] -> configure ( -text => "X", -foreground => "black" );
}
}
}
sub start {
$enter -> configure ( -text => "Check Answer", -command => \&check_answer );
foreach ( 0..$n ) { $button[$_] -> configure ( -text => "", -command => [\&choose, $_] ); }
}
sub choose {
$i=$_[0];
if ( $entry[$i]==0 ) {
$entry[$i]=1;
$button[$i] -> configure ( -text => "X" );
} else {
$entry[$i]=0;
$button[$i] -> configure ( -text => "" );
}
}
sub check_answer {
$enter -> configure ( -text => "New Pattern", -command => \&new_pattern );
foreach ( 0..$n ) {
$button[$_] -> configure ( -text => "", -foreground => "black", -command => \&park );
if ( $random[$_]==1 && $entry[$_]==1){
$button[$_] -> configure ( -text => "X", -foreground => "blue" );
}elsif ( $random[$_]==1 && $entry[$_]==0){
$button[$_] -> configure ( -text => "X", -foreground => "white" );
}elsif ( $random[$_]==0 && $entry[$_]==1){
$button[$_] -> configure ( -text => "X", -foreground => "red" );
}else{}
}
}
sub park {}