2013-04-30 2 views
0

나는 나는 그들이 잘못을 제출하려고하면 그들은 1-4 인 대답을 제출해야한다는 메시지를 표시하기를 원한다면 가정하는 Q &에게유효하지 않은 응답 거절?

######################################### 
# 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 
} 

을 내가 쓴 몇 가지 펄 코드를 사용하여 게임을 만드는거야 답 (예 : 편지 또는 구두점). 이 일을 어떻게 하죠? (A)가 번호를 제공하지 않습니다, 또는 (b) 그들이 할 경우

+0

"'# 더 나은 구문을위한 미리 정의 된 서브 세트"음 ... 뭐라구? Perl은 C가 아닙니다. 함수 프로토 타입을 미리 선언 할 필요는 없습니다. –

+0

그리고 귀하의 질문에 관해서는, [당신은 무엇을 했습니까?] (http://whathaveyoutried.com) 그냥 입력을 잡아 하나의 정수 1-4 중 하나인지 확인하십시오. –

+0

@JackManey 이전보다 좋았습니다. 그것은 훨씬 복잡해졌으며 현재 잠수정으로 훨씬 조직되어 있습니다. – sharksfan98

답변

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) { 
    ... 

는이 범위 경고를 출력하지만, 답변 arrayref $에 답변 수 밖에입니다.

+0

그래서 이것을 ask_question * 함수 아래에 두십시오. – sharksfan98

+0

이것은 ask_question()의 마지막 if 문 중 일부일뿐입니다. – mcglk

+0

아, 알겠습니다. 좋은 답변 주셔서 감사합니다. @mcglk – sharksfan98