文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>經典的設計

經典的設計

时间:2008-10-10  来源:ttleezhaoyu

#!/usr/bin/perl

use strict;
use warnings;

our ($You, $Me, $Draw) = (0,0,0);
my @options = ("rock", "paper", "scissors");
my($my_turn, $your_turn);

PrintIntro();

while(1) {  
        $my_turn = MyGo();
        $your_turn = YourGo()-1;
       
        print "Me: $options[$my_turn], You: $options[$your_turn] -- ";
   
        Results($my_turn, $your_turn);
        PrintScore($You, $Me, $Draw);

        last unless Continue();
}

sub PrintIntro {
        print "\nRock, paper, scissors\n";  
        print "---------------------\n\n";
        print "Rock beats scissors,\n";
        print "Scissors beats paper, \n";
        print "Paper beats rock.\n\n";
}

sub MyGo { return int(rand(3)); }

sub YourGo {
    my $Turn = 0;
        print "Enter 1-rock, 2-paper, 3-scissors\n";
        chomp($Turn = <STDIN>);

        while(($Turn < 1) || ($Turn > 3)) {
                print "Enter 1-rock, 2-paper, 3-scissors\n";
        chomp($Turn = <STDIN>);
    }
   
        return $Turn;
}

sub PrintScore {
        print "You scored $You, I scored $Me with $Draw draws.\n";
       
        if($You > $Me) {
                print "You are winning.\n";
        }

        if($You < $Me) {
                print "I am winning.\n";
        }
       
        print "\n\n";
}

sub Results {
        my($my, $your) = @_;
        my $Test = $my - $your;
       
        if($Test == 0) {
                $Draw++;  
                print " Draw.\n";
        } elsif ($Test == -1) {
                $You++;
                print " You won.\n";
        }  elsif ($Test == 2) {
                $You++;
                print " You won.\n";
        } else {
                $Me++;
            print " I won.\n";
        }
}

sub Continue {
        print "\nAnother game?(y/n): ";
        my $answer;
        while (1) {
                $answer = <STDIN>;
                chomp $answer;
                last if (lc $answer eq 'y') || (lc $answer eq 'n');
        }

        return $answer eq 'y';
}
相关阅读 更多 +
排行榜 更多 +
gate交易平台app免费版

gate交易平台app免费版

金融理财 下载
森林有鬼小游戏下载

森林有鬼小游戏下载

策略塔防 下载
易欧交易所app免费版

易欧交易所app免费版

金融理财 下载