-
Notifications
You must be signed in to change notification settings - Fork 0
/
JokeMachine.pl
78 lines (54 loc) · 2.42 KB
/
JokeMachine.pl
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl -w
#JokeMachine.pl
###DESIGNING THE GAME LOGIC###
#1. Create a new script file and add opening comment statements.
#2. Clear the screen and prepare to collect the player's responses.
#3. Prompt the player for permission to tell the joke and process the player's response.
#4. Clear the screen and display the trick question.
#5. Analyze the player's guess in order to determine whether it is correct.
#6. Develop programming logic to clear the screen.
###PREPING THE SCREEN AND GAME###
clear_the_screen();
$reply = " ";
#the first statement calls the subroutine called "clear_the_screen()" located at the bottom
#of the script.
#the second statement defines a variable the script will use to store player input.
###PROMPTING THE PLAYER###
#setup WHILE loop that iterates over and over again until the script gets the response it wants
#from the player (e.g 'yes').
while ($reply ne 'yes'){
print 'Would you like to hear a joke? (yes/no): ';
chomp($reply = <STDIN>);
if ($reply ne 'yes'){
print "\nHum...Perhaps you misunderstood. \n\n";
}
}
###DISPLAY THE TRICK QUESTION###
#Once the player gives the Perl script permission to tell its joke, the screen is cleared again
#and the script's trick question needs to be presented to the player. The script then collects the
#player's guess
clear_the_screen();
print "\nWhat disappears the momement you say its name?";
chomp ($reply = <STDIN>);
#the first statement executes the "clear_the_screen()" subroutine again before printing the trick
#question. The last statement above is responsible for capturing the player's response and assigning
#it to the variable setup earlier ($reply).
###ANALYZING PLAYER GUESS###
if ($reply ne 'silence') {
print "\nSorry. Wrong answer. Think about it and try again later. \n\n";
}
else {
print "\nYes, that's right. Well done!\n\n";
}
#These statments analyze the player's response (which is stored in $reply) to see what it is equal to.
#Based on the response either of two strings is printed. The above statments are controlled by an IF_ELSE
#statement.
###CONTROLING THE DISPLAY###
#this subroutine loops and adds 20 blank lines to the screen when called.
sub clear_the_screen {
for ($i=0; $i < 20; ++$i){
print "\n";
}
}
#these code statements define the "clear_the_screen" subroutine. By grouping statements into subroutines
#you enable them to be called whenever necessary from any point in a script.