Guest User

Untitled

a guest
Dec 9th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.66 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use POSIX; # Need this for the floor() function.
  7.  
  8. $SIG{'INT'} = sub {print "\nNO ^C! GET SOME BACKBONE AND FINISH THIS!\n"}; # Stop wimps from simply Ctrl-C'ing out of their fate.
  9.  
  10. # Initialise stuff.
  11.  
  12. my @questions;
  13. my @answers;
  14. my @used;
  15.  
  16. my $max = 5; # Set number of questions to 5.
  17. my $passlimit = 4; # How many questions the user must answer correctly to win.
  18. my $score = 0;
  19. my $filename = "questions.txt";
  20.  
  21. open(my $file, "<", $filename) or die "cannot open $filename for reading: $!\n";
  22. while(<$file>) {
  23.     chomp; # We don't want any stray characters messing with things.
  24.     my ($question, $answer) = split(/;\s/); # Question file syntax is "Question?; Answer"
  25.     push(@questions, $question); # Shove the question into the questions array.
  26.     push(@answers, $answer); # Likewise with the answer.
  27. }
  28. close($file);
  29.  
  30. quiz_and_stuff(); # Get things going.
  31.  
  32. sub get_question {
  33.     my $num = floor(rand(@questions));
  34.     my @tmp;
  35.     while (grep {/$num/} @used) {
  36.         # Keep regenerating random numbers until we get a question number that hasn't been asked.
  37.         $num = floor(rand(@questions));
  38.     }
  39.     push(@used, $num); # Push the question number into the used array, so we know it's been asked.
  40.     return ($questions[$num], $answers[$num]);
  41. }
  42.  
  43. sub quiz_and_stuff {
  44.     while(@used != $max) {
  45.         # When used array size is equal to maximum number of questions, we're done.
  46.         my ($question, $answer) = get_question();
  47.         print ($question, "\n");
  48.         my $input = <>; # <> is shorthand for <STDIN>.
  49.         chomp $input; # Again, get rid of silly characters like newlines etc.
  50.         if($answer =~ /^$input$/i) {
  51.             # If a case insensitive match is achieved, they got it right.
  52.             $score++;
  53.             print "Correct!" . (@used == $max ? "End of quiz!\n\n" : " Next question!\n\n");
  54.         }
  55.         else {
  56.             # This person sucks... hard.
  57.             print "Nope!" . (@used == $max ? "End of quiz!\n\n" : " Next question!\n\n");
  58.         }
  59.     }
  60.  
  61.     print "Your score: $score/$max - " . ($score/$max)*100 . "%\n";
  62.     if($score < $passlimit) {
  63.         # They suck even harder than before. They really need to sort themselves out.
  64.         # Don't take this person to the Cambrian pub quiz, or if you do, put them on somebody else's team.
  65.         print "You did not answer enough questions correctly... *ahem*...\n";
  66.         print "Loooooossseeeeerrrrrr! LOOOOOOOOOSSSSEEEERRRRRRR! HAHAHAHAHAHAHAHAHAHAHAHA!\n";
  67.     }
  68.     else {
  69.         # This person is worthy of your recognition and will make a fine pub quiz team-mate.
  70.         print "Congratulations! You won the quiz! Wooooop!\n";
  71.     }
  72.  
  73.     # Now we give the player a chance to show off some more, or to attempt to redeem themselves...
  74.  
  75.     my $idiotproof = 0;
  76.     while($idiotproof == 0) {
  77.  
  78.         print "\nPlay again? Y/N:";
  79.         my $response = <>;
  80.         chomp $response;
  81.  
  82.         if(lc($response) eq 'y') {
  83.             $idiotproof = 1;
  84.             print "\n";
  85.  
  86.             # Zero all the things.
  87.             @used = (); # Clear out the used array;
  88.             $score = 0;
  89.  
  90.             # RESTART!
  91.             quiz_and_stuff();
  92.         }
  93.         elsif(lc($response) eq 'n') {
  94.             # They don't want to play any more :(
  95.             $idiotproof = 1;
  96.             print "\nFine... be like that... I didn't want to play again anyway.\n";
  97.             exit();
  98.         }
  99.         else {
  100.             # They typed something silly. MOCK THEM MERCILESSLY!
  101.             print "\nSeriously...? How hard is it to type a fucking \"y\" or a \"n\"... TRY AGAIN, KNOBCHEESE!\n";
  102.         }
  103.     }
  104. }
Add Comment
Please, Sign In to add comment