Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use v5.38;
- no warnings 'experimental';
- use builtin 'trim';
- my $score_board = 10;
- my $hidden_number = int(rand(20));
- my $name = '';
- say "\n\tWelcome to find the Hidden Number";
- until($name){
- print "\tPlease provide your name : ";
- $name = ucfirst(<STDIN>);
- $name = trim($name);
- }
- say "\n\tOkay $name let's start the game.\n\n
- You will try entering the correct number that computer thinks.
- If you nailed it, you will get the full score of 10.
- if you failed to guess it, the game will end.\n";
- while (1){
- # check to see if game is already lost
- unless (score()){
- say "GAME OVER";
- say "The Secret Number is : $hidden_number";
- say "";
- exit();
- }
- # ask the player to enter a number
- print "Type the hidden number: ";
- my $number = <STDIN>;
- # check for high guesses
- if ($number > $hidden_number){
- say "\nIt's too high!";
- say "New score : ".score(-3);
- say "";
- }
- # check for low guesses
- if ($number < $hidden_number){
- say "\nIt's too low!";
- say "New score :".score(-1);
- say "";
- }
- # check to see if game is won with perfect score
- if ($number == $hidden_number and score() == 10){
- say "YOU WON THE GAME! CONGRATULATIONS!";
- say "Total Score : $score_board";
- say "The Secret Number is : $hidden_number";
- say "";
- exit();
- }
- # check to see if game is won with less than perfect score
- if ($number == $hidden_number){
- say "You did a good job $name! $hidden_number is the secret number";
- say "Total Score : $score_board";
- say "";
- exit();
- }
- }
- # receives one optional argument
- # adds the argument to the $score_board
- # returns the current score or 0 if there's none left
- sub score($num = 0){
- $score_board += $num;
- if ($score_board < 1){
- return 0;
- }
- return $score_board;
- }
Advertisement