public
Created

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 themselves into a data structure. This would allow switching between players more easily than with hardcoded logic. The `prompt` function simplifiess user interaction. **Note:** a “GPU” is not a license, did you mean GPL?. Also, “Creative Commons” denotes a family of modular licenses, not a specific license. For the sake of compability, I waive all copyright on this gist.

  • Download Gist
solar_game.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#########################################
# 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
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.