Petras has asked for the wisdom of the Perl Monks concerning the following question:
Recently I saw a thread about when to use a subroutine. It had some useful ideas, but now I want to ask How to Make a SMARTER Subroutine?
In a fit of boredome-turned-educational I wrote this small Tk game:
These two if statements look a lot alike:
and I wish I knew a way to include some toggles to make one sub work for either color, but I'm at a loss how to do that.
When writing a Minesweeper clone I hit something similar:
Thanks and Cheers!
-P
That is what the textbook says, but it's wrong.
-DC
In a fit of boredome-turned-educational I wrote this small Tk game:
#!perl use warnings; use strict; use Tk; require Tk::ROText; my $MW=MainWindow->new; $MW->title("Jump!"); $MW->geometry("775x240+10+10"); my $game_frame=$MW->Frame->pack; $MW->fontCreate('readable', -family => 'arial', -size => 13); my $instruct=$game_frame->ROText( -wrap => "word", -height => 4, -font => "readable", -relief => "flat", -takefocus => 0 )->grid (-row => 0, -columnspan=>1 +3); $instruct->insert('end', "You've five red and five blue chips. One spa +ce is blank. Red chips can only move to the right, blue chips to the +left. Any chip can move into the blank space. A red chip can jump ove +r a single blue chip into the blank space. A blue chip can jump over +a single red chip into the blank space. Your goal is to get all the r +eds on the right, and all the blues on the left."); my @button; for (1..11) { $button[$_]=$game_frame->Button( -text => "X", -width => 8, -height =>4, -relief => "solid", -activeforeground => "black", -borderwidth => 3, -command => [ \&move, $_ ] )-> grid(-row => 1, -column => $_); } $game_frame->Button( -text => "Start Over", -command => [ \&setup ], -font => "readable" ) -> grid(-row =>2, -columnspan => 13, + -pady => 2); my $msg; my $message=$game_frame->Label (-textvariable => \$msg, -font => "read +able", -foreground => "red") -> grid(-row=>3, -columnspan => 13); setup(); MainLoop; sub setup { $msg=""; for (1..5) { make_color("red", $_); } for (7..11) { make_color("blue", $_); } make_blank(6); } sub make_color { my ($c, $index) = @_; $button[$index]->configure ( -activebackground => $c, -foreground => $c, -background => $c, -state => "normal"); } sub make_blank { $_=shift; $button[$_]->configure ( -background => "white", -foreground => "black", -activebackground => "white", -state => "disabled", -disabledforeground => "black"); } sub move { $msg=""; $_=shift; my $old_color = $button[$_]->cget(-background); if ($old_color eq "red") { if ($_ == 11) { alert(1); } elsif ($button[$_+1]->cget(-background) eq "red") { alert(2); } elsif ($_ <= 9 && $button[$_+1]->cget(-background) eq "blue" && +$button[$_+2]->cget(-background) ne "white") { alert(3); } elsif ($button[$_+1]->cget(-background) eq "white") { make_color ("red", $_+1); make_blank ($_); check_win(); } else { make_color ("red", $_+2); make_blank ($_); check_win(); } } if ($old_color eq "blue") { if ($_ == 1) { alert(1); } elsif ($button[$_-1]->cget(-background) eq "blue") { alert(2); } elsif ($_ >= 2 && $button[$_-1]->cget(-background) eq "red" && $ +button[$_-2]->cget(-background) ne "white") { alert(3); } elsif ($button[$_-1]->cget(-background) eq "white") { make_color ("blue", $_-1); make_blank ($_); check_win(); } else { make_color ("blue", $_-2); make_blank ($_); check_win(); } } } sub alert { $_=shift; if ($_ == 1) { $msg = "You can't move any further than the end!"; } elsif ($_ == 2) { $msg = "You can't jump over the same color chip!"; } elsif ($_ == 3) { $msg = "You can only jump over the opposite colored chip if the ne +xt space is blank!"; } else { $msg = "You won!"; for (1..11) { $button[$_]->configure(-state => "disabled"); } for (1..5) { $button[$_]->configure(-disabledforeground => "blue"); } $button[6]->configure(-disabledforeground => "white"); for (7..11) { $button[$_]->configure(-disabledforeground => "red"); } } } sub check_win { for (1..5) { if ( $button[$_]->cget(-background) ne "blue" ) { return; } } for (7..11) { if ( $button[$_]->cget(-background) ne "red" ) { return; } } alert(4); }
if ($old_color eq "red") { if ($_ == 11) { alert(1); } elsif ($button[$_+1]->cget(-background) eq "red") { alert(2); } elsif ($_ <= 9 && $button[$_+1]->cget(-background) eq "blue" && +$button[$_+2]->cget(-background) ne "white") { alert(3); } elsif ($button[$_+1]->cget(-background) eq "white") { make_color ("red", $_+1); make_blank ($_); check_win(); } else { make_color ("red", $_+2); make_blank ($_); check_win(); } } if ($old_color eq "blue") { if ($_ == 1) { alert(1); } elsif ($button[$_-1]->cget(-background) eq "blue") { alert(2); } elsif ($_ >= 2 && $button[$_-1]->cget(-background) eq "red" && $ +button[$_-2]->cget(-background) ne "white") { alert(3); } elsif ($button[$_-1]->cget(-background) eq "white") { make_color ("blue", $_-1); make_blank ($_); check_win(); } else { make_color ("blue", $_-2); make_blank ($_); check_win(); } }
and I wish I knew a way to include some toggles to make one sub work for either color, but I'm at a loss how to do that.
When writing a Minesweeper clone I hit something similar:
What I wound up doing was repeating the double-for loop several times in the code. I'd like to be able to do something like:for $j (1..$row_count) { for $k (1..$col_count) { #DO SOMETHING, LIKE PLACE RANDOM MINES, #FIGURE OUT WHAT NUMBER GOES HERE, CLEAR #ADJACENT EMPTY SPACES, WHATEVER } }
But it's beyond me. It's been said "The Map is Posted on Every Corner" but it's a pretty big map. Any pointers on where to go or where to look?sub double_for { /&CODE_REFERENCE = $ARG[0]; for $i (1..$row_count) { for $j (1..$col_count) { &CODE_REFERENCE($i, $j); } } }
Thanks and Cheers!
-P
That is what the textbook says, but it's wrong.
-DC
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: In Search of a Smarter Sub
by moritz (Cardinal) on Dec 11, 2007 at 10:53 UTC | |
Re: In Search of a Smarter Sub
by KurtSchwind (Chaplain) on Dec 11, 2007 at 13:10 UTC | |
Re: In Search of a Smarter Sub
by cdarke (Prior) on Dec 11, 2007 at 13:00 UTC | |
Re: In Search of a Smarter Sub
by dragonchild (Archbishop) on Dec 11, 2007 at 21:05 UTC |
Back to
Seekers of Perl Wisdom