2013-04-30 92 views
0

我创建问答&使用一些Perl代码我写一个游戏,拒绝无效的答案?

######################################### 
# Solar Game       # 
#          # 
# (C) 2013 Donovan Roudabush   # 
# MIT License       # 
#          # 
# [email protected]     # 
# 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答案(即字母或标点符号)。我会如何去做这件事?

+0

“'#语法更好的'#predeclare subs”嗯......什么? Perl不是C.你不需要预先声明函数原型。 – 2013-04-30 03:36:36

+0

至于你的问题,[你有什么尝试?](http://whathaveyoutried.com)只需抓住输入,并检查它是否是1--4中的任何整数。 – 2013-04-30 03:37:31

+0

@JackManey这比我之前做的更好。它更加混乱,现在更加有组织的潜艇。 – sharksfan98 2013-04-30 03:39:35

回答

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)他们这样做,但它是在$答案数组引用答案的数目之外。

+0

所以把它放在* ask_question *函数下面呢? – sharksfan98 2013-04-30 03:46:23

+0

它只是ask_question()中最后一条if语句的一部分。 – mcglk 2013-04-30 03:50:09

+0

啊,我明白了。感谢您的杰出答案。 @mcglk – sharksfan98 2013-04-30 03:53:46

相关问题