The text is the .pdf we have been using since the beginning of the semester:
Some of the things we need are here:http://www.cs.indiana.edu/~dgerman/eowp06.pdf
Every time we work out a problem like this we can apply our template:http://silo.cs.indiana.edu:11350/images/
First step is to determine what our state is made of.
The following are immediate candidates:
The inputs also need to be discussed:
So I guess we have what we need to get started.
Basically this includes a code portion, along with a "get ready for more input" portion.
<?
?>
<form>
</form>
We sketch more of the basic layout:
We also enhance the content of the form as well:
if ($message) {
} else {
$message = "Welcome to the game, are you ready?";
}
Obviously, we are just acknowledging the initialization of
<?=$message?> <p>
Press <input type="submit" value="Proceed"> to move on.
<input type="hidden" name="message" value="<?=$message?>">
$message. Perhaps this would be a good time to approach state initialization in general:
Let's decide to save the state on the client side:
$correct = 0;
$total = 0;
$questions = "Italy,Australia,United States,South Africa,Spain,China,Russia,Brazil";
$key = ""; // there is no key in the beginning
Now let's make some changes. Add (at the top of the code part) these definitions:
<input type="hidden" name="correct" value="<?=$correct?>">
<input type="hidden" name="total" value="<?=$total?>">
<input type="hidden" name="questions" value="<?=$questions?>">
<input type="hidden" name="key" value="<?=$key?>">
Also modify the line that initializes$url = "http://www.cs.indiana.edu/classes/a202-dger/fall2005/notes/flagquiz/images"; $names = array ("Australia", "United States", "Russia", "Spain", "Italy", "South Africa", "Brazil", "China");
$questions to:
This way we leave ourselves open to the idea of shuffling, initially.$questions = join(",", $names);
We should now take care of the user submissions coming after the initial screen.
Next we need to grab the answer key that follows from the remaining questions:if ($key) { if ($answer == $key) { $correct += 1; } else { } $total += 1; } else { }
At the same time we need to update the message, and be ready to ask questions. So we have:list($key, $questions) = split(",", $questions, 2);
Update the message if the answer is correct:
Update the message if the answer is incorrect:$message = "Very good.";
Update the message with the new values of the score:$message = "No, that was not it.";
Prepare the question you want to ask:$message .= " Score currently: $correct out of $total.";
Show the question and get ready for (more) user input:$question = "Whose country is this flag: <img src='$url/$key.png'> <input type=text name='answer'> <p>";
Now the program works almost as intended, except at the end of the quiz.<?=$question?>
So we need to not ask a new question if we are not able to:
Furthermore we need to tie seamlessly into the next game.if ($questions) { // ... } else { $question = "The game has ended, new game starting, are you ready? <p>"; $correct = 0; $total = 0; $questions = join(",", $names); $key = ""; // there is no key in the beginning }
That goes into a previously empty branch of the program.$message = "Welcome to a new game, score currently: $correct out of $total.";
This is what we have obtained:
What remains to be done:http://silo.cs.indiana.edu:11350/examples/one.php
These are very simple things.
A. To shuffle the array you need to invoke the following with each initialization:
You can check withshuffle($names);
php.net on the use of shuffle. You need to shuffle the array each time you initialize or re-initialize the code.
B. To add a reset button you do two things:
B.1) Add the button to the form:
B.2) Change the condition in the outer if to:Press <input type="submit" name="reset" value="Reset"> to reset the game.
C. Finally, to switch from client-side state to server-side state:if ($message && ! $reset) {
C.1. All the hidden fields vanish.
C.2. For each hidden field wiped out add a session registration statement.
Each such statement register the name for a state variable.session_register("...");
C.3. Make sure the first statement is
Also, all of these changes should be made to a copy of the original program.session_start();
Overall you should create two programs and post links to them on your website.
You should create symbolic links to them too and post those in protected.
The links in protected should show the source code.
To create the links assume you have one.php in some folder /htdocs/something
Go to protected and establish the link for the source code as follows:
Then make a link available to the newln -s ../something/one.php one.phps
one.phps