Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

Number Guessing Game using Perl

A number guessing game is a classic beginner's project, and implementing it in Perl provides a great way to understand basic programming concepts such as loops, conditional statements, and user input.

Number Guessing Game in Perl:

Objective:

The computer randomly selects a number between 1 and 100, and the player tries to guess it.

Steps:

  1. Generate a random number between 1 and 100.
  2. Prompt the user to guess the number.
  3. Check the guess:
    • Inform the user if the guess is too high, too low, or correct.
  4. Repeat steps 2-3 until the user guesses the number or chooses to exit.

Code:

#!/usr/bin/perl
use strict;
use warnings;

# 1. Generate a random number between 1 and 100
my $random_number = int(1 + rand 100);
my $guess;
my $attempts = 0;

print "Welcome to the Number Guessing Game!\n";
print "I've chosen a number between 1 and 100. Try to guess it!\n";

while (1) {  # Infinite loop to keep asking the user for a guess until they're correct or choose to exit.
    # 2. Prompt the user to guess the number
    print "Enter your guess (or 'exit' to quit): ";
    $guess = <STDIN>;
    chomp($guess);  # Removing newline character from user input
    
    # Check if the user wants to exit
    if ($guess eq "exit") {
        print "The number was $random_number. Better luck next time!\n";
        last;
    }

    # Ensure the input is a number
    if ($guess !~ /^\d+$/) {
        print "Please enter a valid number!\n";
        next;
    }

    $attempts++;

    # 3. Check the guess
    if ($guess == $random_number) {
        print "Congratulations! You've guessed the correct number in $attempts attempts.\n";
        last;  # Exit the loop as the game is over
    } elsif ($guess < $random_number) {
        print "Too low! Try again.\n";
    } else {
        print "Too high! Try again.\n";
    }
}

How to play:

  1. Save the above code to a file, say guess_game.pl.
  2. Run the game:
    perl guess_game.pl
    
  3. Follow the on-screen instructions to play.

Conclusion:

This game introduces basic Perl concepts like loops, conditional statements, user input processing, and random number generation. It's a fun and educational way to get a taste of programming in Perl.

  1. Creating a simple guessing game in Perl:

    • Description: Develop a basic number guessing game where the user tries to guess a randomly generated number.
    • Code:
      # Simple Guessing Game in Perl
      
      use strict;
      use warnings;
      
      # Generate a random number between 1 and 10
      my $secret_number = int(rand(10)) + 1;
      
      # Prompt user for a guess
      print "Guess the number (between 1 and 10): ";
      my $user_guess = <STDIN>;
      chomp $user_guess;
      
      # Check if the guess is correct
      if ($user_guess == $secret_number) {
          print "Congratulations! You guessed the correct number.\n";
      } else {
          print "Sorry, the correct number was $secret_number.\n";
      }
      
  2. Perl random number generation for games:

    • Description: Use Perl's rand() function to generate random numbers for games.
    • Code:
      # Generate a random number between 1 and 100
      my $random_number = int(rand(100)) + 1;
      
  3. User input handling in Perl for games:

    • Description: Handle user input using STDIN and chomp for cleaner input processing.
    • Code:
      # Prompt user for input and remove newline character
      print "Enter your guess: ";
      my $user_input = <STDIN>;
      chomp $user_input;
      
  4. Conditional statements in Perl for guessing game logic:

    • Description: Use if-else statements to implement game logic based on user input.
    • Code:
      if ($user_guess == $secret_number) {
          print "Congratulations! You guessed the correct number.\n";
      } else {
          print "Sorry, the correct number was $secret_number.\n";
      }
      
  5. Loop structures in Perl for game repetition:

    • Description: Utilize loop structures like while or for for repeated gameplay.
    • Code:
      # Play the game until the user guesses correctly
      while ($user_guess != $secret_number) {
          # Game logic...
      
          # Prompt user for a new guess
          print "Try again. Enter your guess: ";
          $user_guess = <STDIN>;
          chomp $user_guess;
      }
      
  6. Perl script for a basic number guessing game:

    • Description: Combine the elements to create a complete Perl script for a number guessing game.
    • Code:
      use strict;
      use warnings;
      
      # Generate a random number between 1 and 10
      my $secret_number = int(rand(10)) + 1;
      
      # Play the game until the user guesses correctly
      while (1) {
          print "Guess the number (between 1 and 10): ";
          my $user_guess = <STDIN>;
          chomp $user_guess;
      
          if ($user_guess == $secret_number) {
              print "Congratulations! You guessed the correct number.\n";
              last;  # Exit the loop
          } else {
              print "Sorry, try again.\n";
          }
      }
      
  7. Adding scoring and feedback to Perl guessing game:

    • Description: Enhance the game by adding scoring and feedback for the user's performance.
    • Code:
      use strict;
      use warnings;
      
      my $secret_number = int(rand(10)) + 1;
      my $attempts = 0;
      
      while (1) {
          print "Guess the number (between 1 and 10): ";
          my $user_guess = <STDIN>;
          chomp $user_guess;
          $attempts++;
      
          if ($user_guess == $secret_number) {
              print "Congratulations! You guessed the correct number in $attempts attempts.\n";
              last;
          } else {
              print "Sorry, try again.\n";
          }
      }
      
  8. Interactive console-based games in Perl:

    • Description: Create an interactive console-based game with a menu and multiple gameplay options.
    • Code:
      use strict;
      use warnings;
      
      sub play_guessing_game {
          # Game logic...
      }
      
      sub play_another_game {
          # Game logic...
      }
      
      # Main menu
      while (1) {
          print "Main Menu:\n";
          print "1. Play Guessing Game\n";
          print "2. Play Another Game\n";
          print "3. Quit\n";
          print "Enter your choice: ";
      
          my $choice = <STDIN>;
          chomp $choice;
      
          given ($choice) {
              when (1) { play_guessing_game() }
              when (2) { play_another_game() }
              when (3) { last }  # Exit the loop for quitting
              default  { print "Invalid choice. Try again.\n" }
          }
      }