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:
#!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); }
These two if statements look a lot alike:
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:
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 } }
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:
sub double_for { /&CODE_REFERENCE = $ARG[0]; for $i (1..$row_count) { for $j (1..$col_count) { &CODE_REFERENCE($i, $j); } } }
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?

Thanks and Cheers!
-P

That is what the textbook says, but it's wrong.

-DC

In reply to In Search of a Smarter Sub by Petras

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.