#!/usr/bin/perl use CGI; $q = new CGI; $message = $q->param('message'); $n1 = $q->param('n1'); $n2 = $q->param('n2'); $correct = $q->param('correct'); $total = $q->param('total'); $history = $q->param('history'); # state $answer = $q->param('answer'); # user input if ($message) { if ($n1 + $n2 == $answer) { $correct += 1; $message = "Yes, $n1 + $n2 is INDEED $answer."; } else { $message = "No, $n1 + $n2 is NOT $answer."; } $total += 1; $message .= " Score: $correct/$total"; if ($history) { $history .= ";" . $message; } else { $history = $message; } $n1 = int(rand(100)) - 50; $n2 = int(rand(100)) - 50; $question = "What is $n1 + $n2 ? Answer: "; } else { $message = "Welcome to the game."; $correct = 0; $total = 0; $message .= " Score now $correct out of $total "; $history = ""; $n1 = int(rand(100)) - 50; $n2 = int(rand(100)) - 50; $question = "What is $n1 + $n2 ? Answer: "; } $hdisplay = ""; @things = split(/;/, $history); foreach $thing (@things) { ($a, $b) = split(/ Score:/ , $thing); $hdisplay .= "
$a Score: $b "; } $hdisplay .= "
"; print $q->header, $q->start_html; print qq{
$message

$question

Press to move on.

$hdisplay

}; print $q->end_html;