|
Help with Homework Two
Spring Semester 2004 |
#!/usr/bin/perl
use CGI;
$device = new CGI;
$stamp = $device->param('stamp'); # that's part of retrieving the state
# you need to do the same for $userScore, $computerScore, and even $computer
if ($stamp) { # we are dealing with a user (we can see the stamp)
# you have $computer from when you retrieved the state you just need to
# read input now ($user) to factor it into the change of state, so...
$user = $device->param('user');
if (($user + 1) % 3 == $computer) { # user wins (note the convention though)
# you have the score for the user from when you retrieved the state
$scoreUser += 1;
} else {
# everything else comes here (and there are two more cases)
}
# part of the state is a (new) choice for the computer
$computer = int(rand(3)); # remember that the choices are encoded as numbers
# the order is important in the test above
# 0 for paper, 1 for rock, and 2 for scissors is OK
# any circular permutation of this would be OK also
# now you have updated the state
} else { # no stamp that means you have no ticket. The game starts for you now.
# initialize the state
$scoreUser = 0;
$scoreComputer = 0;
$stamp = "yes";
# part of the state is a (new, fresh) choice for the computer
$computer = int(rand(3));
}
# after this if statement you're ready to store and report the state
print qq{
<form ...>
<input type="hidden" name="stamp" value="$stamp"> <!--this is a watermark for $stamp->
<!--you need watermarks for each of: $computer, $scoreComputer, and $scoreUser as well->
};
# you will need this for the conversion, see below
@choices = ("paper", "rock", "scissors");
print qq{
<--you have now stored the state. Now get ready for reporting it (the state, that is):->
The computer has chosen ($choices[$computer]). <p>
<--you also need to report the score(s) as well... I'll let you do that but don't forget it->
<--and when all is said and done you need to get ready for more input->
Now it's your turn: <input type="select" name="user">
<option value="click me!"> Click me!
<option value="0"> Paper
<option value="1"> Rock
<option value="2"> Scissors
</select>
When ready with your choice please press <input type="submit" value="Proceed">
</form>
};
We worked this out on Wednesday, during office hours. Don't forget that
Wed Feb 4 20:44:55 EST 2004 by Adrian German for A348/A548