|
Spring Semester 2007 |
Let's start from the basic CGI.pm-based web script.
| Sure enough; here it is. |
#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header, $q->start_html;
print qq{
Yo, baby!
};
print $q->end_html;
| Today we will program a simple, fictitious card game. | It's called... Phrphre-b. |
| You need to pronounce that in a special way. | We won't spend too much time on that though. |
| For this game we need a deck of cards. | Things of this kind? |
| Precisely. | Well, OK; you know where to get them now. |
| How do you play Phrphre-b? | Here's a link to get an idea. |
| What are the rules, then? | If you follow the link you will see a picture of Dilbert and one of Ratbert. |
| Ratbert is the simpleminded optimist (you). | Dilbert is the computer itself. You are playing against the computer. |
| Dilbert loves technology more than people. | You are playing against the computer. |
| The game starts with your opponent (Dilbert) picking up a card. | Then you (Ratbert) pick up a card. |
| You take turns until you both have 5 cards. | That's when the one spanning the largest range wins the round. |
| Make sure you keep track of score across rounds. | There is a way to reset the entire game. |
| Within the same round you can keep track of the current ranges. | This (apart from speaking its name out loud) is a pretty simple program. |
| Let's develop a part of it, if not the entire program. | Sure, let's first develop this program. |
#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header, $q->start_html;
$message=$q->param('message'); # done automatically by PHP
if ($message) {
@a = split(' ', $message);
$first = splice(@a, 0, 1);
$message = join(' ', @a);
$anotherMessage = "You are looking at: " . $first;
} else {
@a = ("one", "two", "three", "four", "five", "six");
@b = ();
while ($#a >= 0) {
$i = int(rand($#a+1)); # randomly choose one element in @a
splice(@b, 0, 0, $a[$i]); # put it on the left of @b
splice(@a, $i, 1); # ... and take it out from @a
}
$anotherMessage = "The game has just (re)started. The deck is shuffled.";
$message = join(' ', @b);
}
print qq{
<form>
$anotherMessage <p>
Current list: ($message) <p>
<input type="submit" value="Proceed">
<input type="hidden" name="message" value="$message">
</form>
};
print $q->end_html;
| The entire reading assignment for today boils down to this. | This is the basic framework for developing programs like the ones we're working on. |
| In our case we store the state in hidden fields, on the client side. | Remember we always code with a plan in mind. |