Codeprasa

Number guessing game

Sep 30th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.41 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use v5.32.1;
  5.  
  6. my $scoreBoard = 10;
  7. #our $rounds = 3;
  8. my $limit = 20;
  9. my $hiddenNumber = int(rand($limit));
  10. my $name;
  11. my $number = 0;
  12.  
  13. sub welcome_screen() {
  14.     say "\tWelcome to find the Hidden Number";
  15. while(!$name)   {
  16.     say "Please provide your name : ";
  17.     $name = <STDIN>;
  18. }
  19.     print "Okay ", ucfirst($name), " let\'s start the game.";
  20.  
  21.     print q\
  22.     You will try entering the correct number that computer thinks.
  23.     If you nailed it, you will get the full score of 10.
  24.     If you failed to guess it, the game will end.\, "\n\n";
  25. }
  26.    
  27. sub main_program{  
  28.  
  29. while($number != $hiddenNumber) {
  30.     say "Type the hidden number:";
  31.     chomp($number = <STDIN>);
  32.  
  33.     if($number > $limit) {
  34.         $scoreBoard = $scoreBoard - 3;
  35.         say "It's too high!";
  36.     } elsif ($number < $hiddenNumber){
  37.         $scoreBoard = $scoreBoard - 1;
  38.         say "It's too low!";
  39.     }
  40.      if($scoreBoard == 0 or $scoreBoard < 0) {
  41.         say "GAME OVER";
  42.         say "The Secret Number is :", $hiddenNumber ;
  43.         last;
  44.    
  45.     } elsif($number == $hiddenNumber and $scoreBoard == 10) {
  46.         say "YOU WON THE GAME! CONGRATULATIONS!";
  47.         say "Total Score : ", $scoreBoard;
  48.         say "The Secret Number is :", $hiddenNumber;
  49.         last;
  50.        
  51.    
  52.     } elsif($number == $hiddenNumber) {
  53.         say "You did a good job ", ucfirst($name), $hiddenNumber ," is the secret number.";
  54.         say "Total Score : ", $scoreBoard;
  55.      }
  56.     }
  57. }
  58.         welcome_screen();
  59.         main_program();
  60.  
  61.    
Add Comment
Please, Sign In to add comment