0

我正在使用我编写的一些 Perl 代码创建一个问答游戏,

#########################################
# Solar Game                            #
#                                       #
# (C) 2013 Donovan Roudabush            #
# MIT License                           #
#                                       #
# sharksfan98@gmail.com                 #
# github.com/sharksfan98/solargame      #
#########################################

use strict; use warnings;

# predeclare subs for nicer syntax
sub prompt;
sub ask_question;

print banner();

prompt "Press enter to start...";
my $num = prompt "Enter the number of players:";

my @names;
for (1 .. $num) {
    push @names, prompt "Player $_, please enter your name:";
}

my $exp = lc prompt "Hello, Players! Have you played before? (Y/N):";

unless ($exp =~ /^y/) {
    print "These are the rules:\n\n",
        "There are 50 questions that will be presented in several parts.\n",
        "When presented a question, enter the correct answer according to the chronological number.\n\n",
        "For example, you would enter 3 below since 2+2 = 4.\n\n";
        "2+2=...\n";
        "2\n",
        "3\n";
        "4\n";
        "5\n\n";
        "At the end, your stats will be presented\n\n";
}

print "Ok! The game will begin.\n";
prompt "Press Enter to begin the questions...";

my $correct = 0;
my $incorrect = 0;

my $question_counter = 0;

print "\nPart 1: Measurements",
      "\n====================\n\n";

ask_question
    "What measurement is used to measurements within our Solar System?",
    1 => [
        "Astronomical Unit (AU)",
        "Light Year",
        "Parsec",
    ];

ask_question
    "What do you use to measure farther objects, like stars and galaxies?",
    2 => [
        "Astronomical Unit (AU)",
        "Light Year",
        "Parsec",
    ];

ask_question
    "Which measurement would you use to measure Earth to the Sun?",
    1 => [
        "Astronomical Unit (AU)",
        "Light Year",
        "Parsec",
    ];

ask_question
    "Which measurement would you use measure the Sun to Polaris?",
    2 => [
        "Astronomical Unit (AU)",
        "Light Year",
        "Parsec",
    ];

ask_question
    "Which would you use to measure Earth to Uranus?",
    1 => [
        "Astronomical Unit (AU)",
        "Light Year",
        "Parsec",
    ];

print "\nPart 2: Galaxies",
      "\n====================\n\n";

ask_question
   "What is a galaxy?",
   3 => [
        "Another word for planet",
        "Our Universe",
        "A large gravitationally bound system composed of gas and dust",
   ];

ask_question
   "Which is NOT A Type of Galaxy?",
   4 => [
       "Elliptical",
       "Spiral",
       "Irregular",
       "Triangular",
   ],

ask_question
  "How many stars are usually in a Galaxy?",
  4 => [
       "Tens to Hundreds",
       "Hundreds to Thousands",
       "Thousands to Millions",
       "Billions to Trillions",
  ],

ask_question
  "What is the name of our Galaxy?",
  2 => [
      "NGC 4414",
      "Milky Way",
      "PS2",
      "Rolo",
  ],  

my $percentage = int($correct/($correct + $incorrect) * 100 + 0.5);
print "Percentage: $percentage%\n";

exit;

sub prompt {
    print "@_ ";
    chomp(my $answer = <STDIN>);
    return $answer;
}

sub ask_question {
    my ($question, $correct_answer, $answers) = @_;
    $question_counter++;
    print "Question $question_counter\n";
    print "$question\n\n";
    for my $i (1 .. @$answers) {
        print "$i) $answers->[$i-1]\n";
    }
    my $answer = prompt "Your answer is:";
    if ($answer == $correct_answer) {
        print "Correct!\n\n";
        $correct++;
    } else {
        print "Wrong\n\n";
        $incorrect++;
    }
}

sub banner {
return <<'BANNER_END';

 /   _____/ ____ |  | _____ _______   /  _____/
 \_____  \ /  _ \|  | \__  \\_  __ \ /   \  ___\__  \  /     \_/ __ \
 /        (  <_> )  |__/ __ \|  | \/ \    \_\  \/ __ \|  Y Y  \  ___/
/_______  /\____/|____(____  /__|     \______  (____  /__|_|  /\___  >
        \/                 \/                \/     \/      \/     \/
Version 1.4.1 Alpha

Developed by Donovan Roudabush
https://github.com/sharksfan98/solargame

BANNER_END
}

假设如果我想提示用户,如果他们尝试提交无效答案(即字母或标点符号),他们必须提交 1-4 的答案。我该怎么做呢?

4

1 回答 1

2
if( $answer !~ /^\d+/ || $answer < 1 || $answer > scalar( @$answers ) ) {
  printf "You can only answer with a number betweeen 1 and %d\n\n", scalar( @$answers );
} elsif( $answer == $correct_answer ) {
  ...

如果 (a) 他们没有提供数字,或者 (b) 他们提供了数字,但它超出了 $answers arrayref 中的答案数量,这会打印一个范围警告。

于 2013-04-30T03:43:54.210 回答