Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use POSIX; # Need this for the floor() function.
- $SIG{'INT'} = sub {print "\nNO ^C! GET SOME BACKBONE AND FINISH THIS!\n"}; # Stop wimps from simply Ctrl-C'ing out of their fate.
- # Initialise stuff.
- my @questions;
- my @answers;
- my @used;
- my $max = 5; # Set number of questions to 5.
- my $passlimit = 4; # How many questions the user must answer correctly to win.
- my $score = 0;
- my $filename = "questions.txt";
- open(my $file, "<", $filename) or die "cannot open $filename for reading: $!\n";
- while(<$file>) {
- chomp; # We don't want any stray characters messing with things.
- my ($question, $answer) = split(/;\s/); # Question file syntax is "Question?; Answer"
- push(@questions, $question); # Shove the question into the questions array.
- push(@answers, $answer); # Likewise with the answer.
- }
- close($file);
- quiz_and_stuff(); # Get things going.
- sub get_question {
- my $num = floor(rand(@questions));
- my @tmp;
- while (grep {/$num/} @used) {
- # Keep regenerating random numbers until we get a question number that hasn't been asked.
- $num = floor(rand(@questions));
- }
- push(@used, $num); # Push the question number into the used array, so we know it's been asked.
- return ($questions[$num], $answers[$num]);
- }
- sub quiz_and_stuff {
- while(@used != $max) {
- # When used array size is equal to maximum number of questions, we're done.
- my ($question, $answer) = get_question();
- print ($question, "\n");
- my $input = <>; # <> is shorthand for <STDIN>.
- chomp $input; # Again, get rid of silly characters like newlines etc.
- if($answer =~ /^$input$/i) {
- # If a case insensitive match is achieved, they got it right.
- $score++;
- print "Correct!" . (@used == $max ? "End of quiz!\n\n" : " Next question!\n\n");
- }
- else {
- # This person sucks... hard.
- print "Nope!" . (@used == $max ? "End of quiz!\n\n" : " Next question!\n\n");
- }
- }
- print "Your score: $score/$max - " . ($score/$max)*100 . "%\n";
- if($score < $passlimit) {
- # They suck even harder than before. They really need to sort themselves out.
- # Don't take this person to the Cambrian pub quiz, or if you do, put them on somebody else's team.
- print "You did not answer enough questions correctly... *ahem*...\n";
- print "Loooooossseeeeerrrrrr! LOOOOOOOOOSSSSEEEERRRRRRR! HAHAHAHAHAHAHAHAHAHAHAHA!\n";
- }
- else {
- # This person is worthy of your recognition and will make a fine pub quiz team-mate.
- print "Congratulations! You won the quiz! Wooooop!\n";
- }
- # Now we give the player a chance to show off some more, or to attempt to redeem themselves...
- my $idiotproof = 0;
- while($idiotproof == 0) {
- print "\nPlay again? Y/N:";
- my $response = <>;
- chomp $response;
- if(lc($response) eq 'y') {
- $idiotproof = 1;
- print "\n";
- # Zero all the things.
- @used = (); # Clear out the used array;
- $score = 0;
- # RESTART!
- quiz_and_stuff();
- }
- elsif(lc($response) eq 'n') {
- # They don't want to play any more :(
- $idiotproof = 1;
- print "\nFine... be like that... I didn't want to play again anyway.\n";
- exit();
- }
- else {
- # They typed something silly. MOCK THEM MERCILESSLY!
- print "\nSeriously...? How hard is it to type a fucking \"y\" or a \"n\"... TRY AGAIN, KNOBCHEESE!\n";
- }
- }
- }
Add Comment
Please, Sign In to add comment