#!/usr/bin/perl

use CGI; 
$q = new CGI; # basic readparse 
print $q->header, $q->start_html;

$com = $q->param('com'); 
$use = $q->param('use'); 
$cho = $q->param('cho'); 
$tic = $q->param('tic'); 

if ($tic && ! $q->param('reset')) {
  $inp = $q->param('inp'); 
  if (($cho + 1) % 3 == $inp) {
    $com += 1; 
    $message = "Computer wins."; 
  } elsif (($inp + 1) % 3 == $cho) {
    $use += 1; 
    $message = "User wins.";
  } else {
    $message = "Tie."; 
  }
  $cho = int(rand(3)); 
} else {
    $message = "Welcome to the game."; 
  $com = 0; 
  $use = 0; 
  $cho = int(rand(3)); 
  $tic = "in use";  
}

@choices = ("paper", "rock", "scissors"); 

$choice = $choices[$cho]; 

print $q->startform, qq{
  $message <p> 
  Score: $com - $use <p> 
  Computer has chosen: $choice <p> 
  Please choose: <select name="inp">
    <option value="0"> Paper 
    <option value="1"> Rock 
    <option value="2"> Scissors  
  </select> <p>
  Press <input type="submit" value="Proceed"> 
  <input type="hidden" name="cho" value="$cho">
  <input type="hidden" name="com" value="$com"> 
  <input type="hidden" name="use" value="$use">
  <input type="hidden" name="tic" value="$tic"> <hr> 
  Press here to <input type="submit" value="Reset" name="reset"> the game.  
}, $q->end_form; 

print $q->end_html;