Assignment #3
Due Tuesday, March 11
50 points
The objective of this assignment is to give you an opportunity to gain
more practice with PHP and write some slightly more complicated Web
applications.
All pages should be accessible via their own appropriately labeled links
in the coursework list on your course home page. Furthermore, unless
otherwise noted, each page should contain a button that will automatically
validate that page using the W3C Markup Validation Service when it
is clicked. Of course, you should use the validator to eliminate
the validation errors from your code prior to submission!
- (10 points) Write a separate PHP file that will
process whatever elements it finds in the $_REQUEST array, displaying
the keys and values in a nicely formatted XHTML table. Write at least
two separate XHTML pages containing at least two reasonably
complex XHTML forms that submit their contents to the PHP file for
processing. One should submit its contents using the POST method,
and the other should submit using the GET method. There should be
links to each of these pages on your home page. The point here
is to keep the PHP file generic and separate from the XHTML forms
that submit to it. If you do so, your PHP file may be used to display
the contents of any form you choose to submit to it. This will likely
come in handy later when you need to see exactly what one of your
forms is actually sending to the server. You should validate any
files used to produce your forms, but you do not need to validate
the output of the PHP file (though you should look it over manually
to ensure it is valid XHTML).
- (15 points) Palindromes are words or phrases that are
spelled the same forwards or backwards, such as “kayak” and
“No devil lived on.” There are many variations on the palindrome
concept, involving consideration of spaces, punctuation and
various other aspects of the text. For this part of the assignment,
you’re going to write a small Web application that will accept a
string from the user and report on various aspects of its status
as a palindrome. Specifically, each time a string is entered, you
should report the following:
- The length of the string;
- Whether or not the string is a one word palindrome, such as “kayak”;
- Whether or not the string is a phrase palindrome taking spaces
into account, such as “No devil lived on”;
- Whether or not the string is a phrase palindrome when spaces are
ignored, such as “Cora sees a roc”; and
- Whether or not the string is a phrase palindrome when spaces and punctuation
are ignored, such as “No sign, in evening, is on.”
You should write a single PHP file that will both generate the form and
process it. Your results page should contain a link that will allow
the user to return to the form itself and enter another string. If
you’re looking for palindromes to use for testing purposes, you can
find plenty at Palindromelist.com, where I grabbed the examples above.
- (25 points) Write a single-file Web application
that implements a simple guessing game. In this game, the program
will pick a random integer between 1 and 50 and the user will have
5 chances to guess what that number is. When first loaded, the game
should present the user with a brief explanation of the rules and
a simple form into which the user can enter their guess. Each time
a guess is entered, the program should provide the user with feedback
as follows:
- If the guess is not an integer in the range 1 to 50, redisplay the
guess form with a message indicating the problem and ask the user to
try again.
- If the guess is correct, notify the user and provide them with a link that
they can use to play again.
- If the guess is incorrect, inform the user whether
their guess is too high or too low and how close they are as follows:
- If their guess is within 5 of the correct answer, let the user know they
are “Getting Hot”;
- If their guess is within 10 of the correct answer, let the user know they
are “Getting Warm”;
- If their guess is within 15 of the correct answer, let the user know they
are “Getting Cool”; and
- In all other cases, let the user know they are “Totally Cold”;
- If the user has no chances remaining, thank them for playing and provide
them with a link they can use to play again. Otherwise, present
them with a page that tells them how many chances they have left and
provides a form into which they can enter their next guess.
You must use a regular expression to determine whether the user’s
guess is a valid integer value. You’ll
need to figure out how to maintain the state of your program using
hidden input fields within your forms (don’t worry that users will
be able to cheat by examining the source code—the stakes aren’t
that high!), and this will be simpler if you combine the various
messages with a form to solicit the next guess (rather than use intermediate
pages to provide the messages). Look up the rand() function in the
online PHP reference manual for generating random values.