haxmeister

Guess the number

Sep 30th, 2023
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.98 KB | Source Code | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use v5.38;
  4. no warnings 'experimental';
  5. use builtin 'trim';
  6.  
  7. my $score_board   = 10;
  8. my $hidden_number = int(rand(20));
  9. my $name = '';
  10.  
  11. say "\n\tWelcome to find the Hidden Number";
  12.  
  13. until($name){
  14.     print "\tPlease provide your name : ";
  15.     $name = ucfirst(<STDIN>);
  16.     $name = trim($name);
  17.  
  18.  }
  19.  
  20. say "\n\tOkay $name let's start the game.\n\n
  21. You will try entering the correct number that computer thinks.
  22. If you nailed it, you will get the full score of 10.
  23. if you failed to guess it, the game will end.\n";
  24.  
  25. while (1){
  26.  
  27.     # check to see if game is already lost
  28.     unless (score()){
  29.         say "GAME OVER";
  30.         say "The Secret Number is : $hidden_number";
  31.         say "";
  32.         exit();
  33.     }
  34.  
  35.     # ask the player to enter a number
  36.     print "Type the hidden number: ";
  37.     my $number = <STDIN>;
  38.  
  39.     # check for high guesses
  40.     if ($number > $hidden_number){
  41.         say "\nIt's too high!";
  42.         say "New score : ".score(-3);
  43.         say "";
  44.     }
  45.  
  46.     # check for low guesses
  47.     if ($number < $hidden_number){
  48.         say "\nIt's too low!";
  49.         say "New score :".score(-1);
  50.         say "";
  51.     }
  52.  
  53.     # check to see if game is won with perfect score
  54.     if ($number == $hidden_number and score() == 10){
  55.         say "YOU WON THE GAME! CONGRATULATIONS!";
  56.         say "Total Score : $score_board";
  57.         say "The Secret Number is : $hidden_number";
  58.         say "";
  59.         exit();
  60.     }
  61.  
  62.     # check to see if game is won with less than perfect score
  63.     if ($number == $hidden_number){
  64.         say "You did a good job $name! $hidden_number is the secret number";
  65.         say "Total Score : $score_board";
  66.         say "";
  67.         exit();
  68.     }
  69.  
  70. }
  71.  
  72. # receives one optional argument
  73. # adds the argument to the $score_board
  74. # returns the current score or 0 if there's none left
  75. sub score($num = 0){
  76.     $score_board += $num;
  77.     if ($score_board < 1){
  78.         return 0;
  79.     }
  80.     return $score_board;
  81. }
  82.  
Advertisement