Skip to content

@latk /solar_game.pl
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A refactoring for sharksfan98's “Solar Game”. It now compiles with `use strict; use warnings`. Much of the repetitive code is offloaded into subs. The `ask_question` function takes a question string, the number of the correct answer, and an array reference (`[...]`) containing possible answers. The next refactoring step would put the questions t…
#########################################
# Solar Game #
# #
# (C) 2013 Donovan Roudabush #
# GPU/Creative Commons #
# #
# 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",
"(imagine rules here)\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",
];
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.0 Beta
Developed by Donovan Roudabush
https://github.com/sharksfan98/solargame
BANNER_END
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.