  Fall Semester 2002

Lecture Notes Fifteen: If we were to write the Hangman game using PHP
Let's solve to more problems, like the one the exam is asking for.

Let's implement:

• A Simple Quiz (as described below)
• A Simple Version of Hangman
Let's first implement the programs in Perl, as standalone entities.

Then we can start thinking what it takes to turn them into CGI or PHP scripts.

Here's The Simple Quiz.

```#!/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";
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"; ```
Here's the program running:

```frilled.cs.indiana.edu%./quiz
Question 1: What is -26 + -25?
Very good, that was the answer.
Question 2: What is -41 + 20?
Very good, that was the answer.
Question 3: What is 38 + 26?
Very good, that was the answer.
Question 4: What is 22 + 15?
Very good, that was the answer.
Question 5: What is -27 + 29?
Sorry. The right answer was: 2
Question 6: What is 0 + -11?
Very good, that was the answer.
Question 7: What is -4 + -20?
Very good, that was the answer.
Question 8: What is 11 + 33?
Very good, that was the answer.
Question 9: What is 30 + 25?
Sorry. The right answer was: 55
Question 10: What is -27 + 46?
Very good, that was the answer.
End of quiz. Thanks for your interest.
Your score is: 8 out of 10
frilled.cs.indiana.edu%```
All right, so this is almost exactly as Lab Seven.

You can (and should implement it) in three ways:

• CGI with hidden fields.
• PHP with hidden fields
• PHP with sessions Now let's look at the more complicated problem.

Here's A Simple Hangman.

Our first question is: where do we get the words?

`/usr/share/lib/dict/words`
Here's the solution.

Note how it uses a technique developed in Lecture Notes Nine.

```#!/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";

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/) {
next;
}
\$guesses .= \$char;
print "So far you have entered [\$guesses].\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 "-"; }
}```
Here's a sample run (actually two) with the program.

```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)***
So far you have entered [a].
-----a--
***(Guesses available: 7)***
So far you have entered [ae].
-----a--
***(Guesses available: 6)***
So far you have entered [aei].
-----a--
***(Guesses available: 5)***
So far you have entered [aeio].
o----a--
***(Guesses available: 5)***
So far you have entered [aeiou].
o--u-a--
***(Guesses available: 5)***
So far you have entered [aeiouy].
o--u-a-y
***(Guesses available: 5)***
So far you have entered [aeiouyh].
o--u-a-y
***(Guesses available: 4)***
So far you have entered [aeiouyhc].
o--u-acy
***(Guesses available: 4)***
So far you have entered [aeiouyhcr].
o--uracy
***(Guesses available: 4)***
So far you have entered [aeiouyhcrt].
o--uracy
***(Guesses available: 3)***
***(Guesses available: 3)***
So far you have entered [aeiouyhcrtb].
ob-uracy
***(Guesses available: 3)***
***(Guesses available: 3)***
So far you have entered [aeiouyhcrtbd].
obduracy
Congratulations, you won.
frilled.cs.indiana.edu%```
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: 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)***
So far you have entered [a].
-------
***(Guesses available: 6)***
So far you have entered [ab].
-------
***(Guesses available: 5)***
So far you have entered [abc].
-------
***(Guesses available: 4)***
So far you have entered [abcf].
-------
***(Guesses available: 3)***
So far you have entered [abcfg].
-------
***(Guesses available: 2)***
So far you have entered [abcfgh].
-------
***(Guesses available: 1)***
So far you have entered [abcfghj].
-------
You lost. The word was: soldier
frilled.cs.indiana.edu%```
Let's implement this one as a PHP script, on-line.

The question is: what do we need?

We need a way to:

• substitute patterns

So that's what we will study now.

```<? 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 = ""; }

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)) {
} else {
}
}

?>

<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>```
We'll develop this together, in class.

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

`http://burrowww.cs.indiana.edu:10400/h6.gif`
already shown above, at the beginning of the section.

Now let's clarify how we can randomly generate a random word from the file.

```<?

\$words = file('/usr/share/lib/dict/words');

echo sizeof(\$words), " words to choose from... <p>";

\$index = rand(0, sizeof(\$words));

\$word = \$words[\$index];

?>