[FLUG] [Fwd: [mongers@perl.it] giochetti perl]

sulla mailing list italiana dei perl monger stanno cominciando, finalmente, a passare delle cose interessanti, una e` questio giochetto di cui pero` non capisco bene la logica, e perdo inesorabilmente sempre, aiuto
--- Begin Message ---
Potremmo fare uin archivietto di giochi perl. 

Eccone uno (si usano i numeri del tastierino numerico) 
settare l'xterm (usa le sequenze ansi) a 24x80 caratteri


#!/usr/bin/perl -w

my $circuit=<<end;
################################################################################
######################                 #########################################
###########                            ##########                 ##############
########                                    #####                 ##############
########                                    #                     ##############
########                    ####                                        ########
##                  ############                         ###            ########
##           ###########################                 ###               #####
##           ###########################            ########               #####
###          ##################################################            #####
###          ##################################################            #####
###            ################################################            #####
###            ################################################               ##
####           ###################################################            ##
#####          #################    |      ########################           ##
#####           ############        |             #################           ##
#####            ########           |              ################           ##
######                              |                                         ##
#######                            #####                                      ##
########                         ##########                               ######
########                         ##########                               ######
#########                        ##############                       ##########
###############################################                  ###############
################################################################################
end

my $delta= [
        [0,0],   # keypad 0 -- not used 
        [-1,-1], # keypad 1 
        [0,-1],  # keypad 2 
        [1,-1],  # keypad 3 
        [-1,0],  # keypad 4 
        [0,0],   # keypad 5 
        [1,0],   # keypad 6 
        [-1,1],  # keypad 7 
        [0,1],   # keypad 8 
        [1,1]    # keypad 9 
];


my $height = $circuit =~ tr/\n//;
my $width = length($circuit)/($height);
print "width=$width\n";
substr($circuit, ($height-5)*$width+4,1) = '+';
my $x = 37;
my $y= 9;
my $dx=1;
my $dy=0;
my $status = "";
my $n=0;
my $coll="Collision";
my $won= "Congratulations. You won";
               
until ($status) {
 substr($circuit, ($height-$y)*$width+$x,1) = '@';
 print "\033[32m$circuit\033[39m";
 print "\033[${y}A\033[${x}C\033[31m@\033[39m\033[${x}D\033[${y}B";
 $n++;
 print "time: $n. Make your move: ";
 $c=<>-'0';
 print "\n";
 next if ($c < 0 || $c > 9);
 $dx += $delta->[$c]->[0];
 $dy += $delta->[$c]->[1];
 $x1 = $x + $dx;
 $y1 = $y + $dy;
 if ($x1 < 0 || $x1 >= $width || $y1 < 0 || $y1 > $height) {
                 $status = $coll;
                 last;
 }
 if (abs($dx) > abs($dy)) {
         for ($i=1; $i<=abs($dx); $i++) {
                 $j =  int(($i * $dy)/abs($dx));
                 if (substr($circuit, ($height-$y-$j)*$width+$x+$i*sgn($dx),1) 
eq '#') {
                         $status =$coll;
                 } else {
                         if (substr($circuit, 
($height-$y-$j)*$width+$x+$i*sgn($dx),1) eq '|') {
                                 $status=$won;
                         }
                         substr($circuit, 
($height-$y-$j)*$width+$x+$i*sgn($dx),1) = '*';
                 }
         }
 } else {
         for ($j=1; $j<=abs($dy); $j++) {
                 $i =  int(($j *$dx)/abs($dy));
                 if (substr($circuit, ($height-$y-$j*sgn($dy))*$width+$x+$i,1) 
eq '#') {
                         $status = $coll;
                 } else {
                         if 
(substr($circuit,($height-$y-$j*sgn($dy))*$width+$x+$i,1) eq '|') {
                                 $status=$won;
                         }
                         substr($circuit, 
($height-$y-$j*sgn($dy))*$width+$x+$i,1) = '*';
                 }
         }
 }
 $x += $dx;
 $y += $dy;
}     
        
print "$status in $n steps!\n";


sub sgn {
 my $x = shift;
 return $x? $x/abs($x): 0;
} 


-- 
Franco Bagnoli (franchino) <franco.bagnoli@xxxxxxxx> (bagnoli@xxxxxxxxxxxx)
virtual location: Dipartimento di Energetica "S. Stecco"
ultra-virtual affiliation: Centro Dinamiche Complesse (CSDC-Firenze)
real location: Dip. Matematica Applicata "G. Sansone", Universita' Firenze,
Via S. Marta, 3 I-50139 Firenze, Italy. Tel. +39 0554796422, fax: +39 055471787




--- End Message ---

Other related posts: