![]() |
![]() Spring Semester 2003 |
Let's implement:
Then we can start thinking what it takes to turn them into CGI or PHP scripts.
Here's The Simple Quiz.
Here's the program running:#!/usr/bin/perl $count = 1; $correct = 0; print "Welcome to the Addition Quiz. \n"; while ($count <= 10) { $one = int(rand(100) - 50); $two = int(rand(100) - 50); print "Question $count: What is $one + $two?\n"; print " Answer: "; $answer = <STDIN>; if ($answer == $one + $two) { $correct += 1; print "Very good, that was the answer.\n"; } else { print "Sorry. The right answer was: ", ($one + $two) , "\n"; } $count += 1; } print "End of quiz. Thanks for your interest.\n"; print "Your score is: $correct out of ", ($count - 1), "\n";
All right, so this is almost exactly as Lab Seven.frilled.cs.indiana.edu%./quiz Welcome to the Addition Quiz. Question 1: What is -26 + -25? Answer: -51 Very good, that was the answer. Question 2: What is -41 + 20? Answer: -21 Very good, that was the answer. Question 3: What is 38 + 26? Answer: 64 Very good, that was the answer. Question 4: What is 22 + 15? Answer: 37 Very good, that was the answer. Question 5: What is -27 + 29? Answer: -2 Sorry. The right answer was: 2 Question 6: What is 0 + -11? Answer: -11 Very good, that was the answer. Question 7: What is -4 + -20? Answer: -24 Very good, that was the answer. Question 8: What is 11 + 33? Answer: 44 Very good, that was the answer. Question 9: What is 30 + 25? Answer: 65 Sorry. The right answer was: 55 Question 10: What is -27 + 46? Answer: 19 Very good, that was the answer. End of quiz. Thanks for your interest. Your score is: 8 out of 10 frilled.cs.indiana.edu%
You can (and should implement it) in three ways:
Here's A Simple Hangman.
Our first question is: where do we get the words?
The answer is:
Here's the solution./usr/share/lib/dict/words
Note how it uses a technique developed in Lecture Notes Nine.
Here's a sample run (actually two) with the program.#!/usr/bin/perl open (AB, "/usr/share/lib/dict/words"); @words = <AB>; close(AB); print "Welcome to Hangman!\n\n"; print "Hmm..., ", $#words, " words to choose from. \n"; $index = int(rand($#words + 1)); $masked = $word = $words[$index]; chop($word); print "I'll pick: $word (and you don't know it).\n\n"; $masked =~ s/./-/g; print "You have 7 attempts to guess it. \n", "I will provide feedback. \n", "Let's start, here's the word: ", $masked , "\n"; $count = 7; while ($masked ne $word && $count > 0) { print "***(Guesses available: $count)*** \nPlease guess a character: "; $char = <STDIN>; chop($char); if ($guesses =~ /$char/) { print "You aready picked $char.\n"; next; } $guesses .= $char; print "So far you have entered [$guesses].\n"; $masked = $word; $masked =~ s/(.)/&fun($1)/ge; print $masked, "\n"; $count -= 1 unless $word =~ /$char/; } if ($count <= 0) { print "You lost. The word was: $word\n"; } else { print "Congratulations, you won.\n"; } sub fun { local ($c) = @_; if ($guesses =~ /$c/) { return $c; } else { return "-"; } }
When we don't get the word we simply lose points.frilled.cs.indiana.edu%./hangman Welcome to Hangman! Hmm..., 25142 words to choose from. I'll pick: obduracy (and you don't know it). You have 7 attempts to guess it. I will provide feedback. Let's start, here's the word: -------- ***(Guesses available: 7)*** Please guess a character: a So far you have entered [a]. -----a-- ***(Guesses available: 7)*** Please guess a character: e So far you have entered [ae]. -----a-- ***(Guesses available: 6)*** Please guess a character: i So far you have entered [aei]. -----a-- ***(Guesses available: 5)*** Please guess a character: o So far you have entered [aeio]. o----a-- ***(Guesses available: 5)*** Please guess a character: u So far you have entered [aeiou]. o--u-a-- ***(Guesses available: 5)*** Please guess a character: y So far you have entered [aeiouy]. o--u-a-y ***(Guesses available: 5)*** Please guess a character: h So far you have entered [aeiouyh]. o--u-a-y ***(Guesses available: 4)*** Please guess a character: c So far you have entered [aeiouyhc]. o--u-acy ***(Guesses available: 4)*** Please guess a character: r So far you have entered [aeiouyhcr]. o--uracy ***(Guesses available: 4)*** Please guess a character: t So far you have entered [aeiouyhcrt]. o--uracy ***(Guesses available: 3)*** Please guess a character: c You aready picked c. ***(Guesses available: 3)*** Please guess a character: b So far you have entered [aeiouyhcrtb]. ob-uracy ***(Guesses available: 3)*** Please guess a character: t You aready picked t. ***(Guesses available: 3)*** Please guess a character: d So far you have entered [aeiouyhcrtbd]. obduracy Congratulations, you won. frilled.cs.indiana.edu%
Let's implement this one as a PHP script, on-line.frilled.cs.indiana.edu%./hangman Welcome to Hangman! Hmm..., 25142 words to choose from. I'll pick: soldier (and you don't know it). You have 7 attempts to guess it. I will provide feedback. Let's start, here's the word: ------- ***(Guesses available: 7)*** Please guess a character: a So far you have entered [a]. ------- ***(Guesses available: 6)*** Please guess a character: b So far you have entered [ab]. ------- ***(Guesses available: 5)*** Please guess a character: c So far you have entered [abc]. ------- ***(Guesses available: 4)*** Please guess a character: f So far you have entered [abcf]. ------- ***(Guesses available: 3)*** Please guess a character: g So far you have entered [abcfg]. ------- ***(Guesses available: 2)*** Please guess a character: h So far you have entered [abcfgh]. ------- ***(Guesses available: 1)*** Please guess a character: j So far you have entered [abcfghj]. ------- You lost. The word was: soldier frilled.cs.indiana.edu%
The question is: what do we need?
We need a way to:
So that's what we will study now.
We'll develop this together, in class.<? session_start(); if (session_is_registered("word")) { } else { session_register("word"); $word = "ludicrous"; } if (session_is_registered("guesses")) { } else { session_register("guesses"); } # instead of using ereg, and ereg_replace, use explode, and join. if ($reset) { $guesses = ""; } $masked = ""; if (!$guesses || !ereg($char, $guesses)) { $guesses .= $char; } $chars = preg_split('//', $word, -1, PREG_SPLIT_NO_EMPTY); // see php.net for ($i = 0; $i < sizeof($chars); $i++) { if (ereg($chars[$i], $guesses)) { $masked .= $chars[$i]; } else { $masked .= "-"; } } ?> <html><head><title>That's funny.</title></head><body bgcolor=white> <table> <tr> <td> The word is: <td> <font color="brown"><?=$word?></font> <tr> <td> Your guesses thus far: <td> <?=$guesses?> <tr> <td> The word for you at this stage: <td> <?=$masked?> </table> <form method="POST" action=<?=$SCRIPT_NAME?>> Please enter a character: <input type="text" name="char" size=1 maxsize=1> <p> Then push <input type="submit" value="Proceed"> To reset please press <input type="submit" name="reset" value="Reset"> <p> </form> </body></html>
Here's a working version of this particular program (limited scope, remember).
You can finish it, and add pictures to show the current stage in the game:
And the last stage
already shown above, at the beginning of the section.http://burrowww.cs.indiana.edu:10400/h6.gif
Now let's clarify how we can randomly generate a random word from the file.
Here's my version, working.<? $words = file('/usr/share/lib/dict/words'); echo sizeof($words), " words to choose from... <p>"; $index = rand(0, sizeof($words)); $word = $words[$index]; ?> <html><head><title>That's funnier.</title></head><body bgcolor=white> I think I will choose: <font color="#0066cc"><?=$word?></font> <p> </body></html>