返回顶部

收藏

海龟图(For Fun)(修改版)

更多

海龟在房间散步,拿着一支笔.当笔尖向下的时,运动留下笔迹;当笔尖向上时,运动不留下笔迹。

#!/usr/bin/perl
#turtle walk in his room, writeing or doing nothing
#$pen=1(write) 0(do nothing) -1(end program)
use warnings;
use strict;

my $MaxSize = 30; my @turtle;
my $Now = 0; my $Old = 0;
my $pen = 0; my $step = 0;

while ($pen = <STDIN>){
    chomp $pen; last if $pen == -1; 

    chomp ($step = <STDIN>);
    $Now = $Old + $step;
    if ($Old <= $Now){
        for ($Old..$Now){
            $turtle[$_] = 1 if $pen == 1;   
        }
    }else{
        for ($Now..$Old){
            $turtle[$_] = 1 if $pen == 1;
        }
    }

    chomp ($step = <STDIN>);
    $Old = $Now;
    if ($step >= 0){
        for (1..$step){
            $turtle[$Old+$_*$MaxSize] = 1 
                if $pen==1;
        }
    }else{
        for ($step..-1){
            $turtle[$Old+$_*$MaxSize] = 1 
                if $pen==1;
        }
    }

    $Old = $Now + $step * $MaxSize;
    die "error, out of room."
        if $Old>=$MaxSize*$MaxSize || $Old<0;

    for (0..$MaxSize*$MaxSize-1){
        print "\\n" if ($_%$MaxSize==0 && $_!=0);
        $turtle[$_] ? print "*  " : print "-  ";
    }
}
#该片段来自于http://outofmemory.cn

标签:perl,基础

收藏

0人收藏

支持

0

反对

0