-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGUESS3.pl
More file actions
57 lines (53 loc) · 1.98 KB
/
GUESS3.pl
File metadata and controls
57 lines (53 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl
#Author: John Bohne
#Date: 12/2012
#Program Name: GUESS3
#Program Description: Perl script that takes in low and high command arguments and generates a random number that the user must guess.
#The program tells the user if the inputted number is lower or higher than the random number to assist in the game.
$low = $ARGV[0]; #first command line argument
$high = $ARGV[1]; #second command line argument
if (($#ARGV + 1) < 2) #if there are too few arguments, exit
{
print("Too few arguments. Need 2 command line arguments (a low and high number). Try to run the program again.\n");
exit;
} elsif (($#ARGV + 1) > 2) { #if there are too many arguments, exit
print( "Too many arguments. Need 2 command line arguments (a low and high number). Try the run the program.\n");
exit;
}
if ($ARGV[0] == $ARGV[1]) #if the two arguments are equal, exit
{
print( "$low and $high cannot be equal. Try to run the program again.\n");
exit;
}
if ($low > $high) #if the low number is greater than the high number, swap them.
{
$temp = $low;
$low = $high;
$high = $temp;
print("First number (higher number) swapped with second number (lower number) for calculation purposes.\n");
}
$randomNumber = (int(rand($high - $low + 1)) + $low); #rand() goes from 0 inclusive to nothing more than its argument as a floating point. Since integer cast is necessary, +1 is added in the rand formula for a specific range.
print( "Enter a number between $low and $high.\n");
$guess=<STDIN>;
if ($guess > $high || $guess < $low)
{
while ( $guess > $high || $guess < $low )
{
print( "Guess must be between $low and $high. Try again.\n");
$guess = <STDIN>;
}
}
while ( $guess != $randomNumber)
{
if ( $randomNumber < $guess )
{
print("Input is too high. Try again.\n");
$guess = <STDIN>;
}
if ( $randomNumber > $guess )
{
print( "Input is too low. Try again.\n");
$guess = <STDIN>;
}
}
print("You guessed the number $randomNumber.\n");