Skip to content

Instantly share code, notes, and snippets.

@xenoterracide
Created April 19, 2010 01:53
Show Gist options
  • Save xenoterracide/370673 to your computer and use it in GitHub Desktop.
Save xenoterracide/370673 to your computer and use it in GitHub Desktop.
guess a number game
#!/usr/bin/env perl
# guess a number game
use feature 'say';
use feature 'switch';
use strict;
use warnings;
say "welcome";
# generate the winning number between 1 and 10
# see perlfaq4 for algorithm details
my $winning_num = 1 + int( rand( (10-1)+1 ) );
my $guess = 0; #initialize outside of loop so loop executes properly
until ( $guess == $winning_num ) {
say "Guess a number between 1 and 10: ";
# get the number from the user
$guess = readline(*STDIN);
# check to see if we have a winner, or the guess is to high, or low.
given( $guess ) {
when ( $_ > $winning_num ) {
say "Too high";
}
when ( $_ < $winning_num ) {
say "Too low";
}
when ( $winning_num ) {
say "You Win!";
}
}
}
say "Game over!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment