#!/usr/bin/perl -Tw

use strict;
use CGI;

my $qw = new CGI;
my $cgi = $qw->url(-relative=>1);

print $qw->header();

my %images = (
	rock => "rock.jpg",
	paper => "paper.jpg",
	scissors => "scissors.jpg",
);

print <<EOF;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Rock, Paper, Scissors</title>
<style type="text/css">
<!--
img { border:0px; vertical-align:middle; height:80px; }
-->
</style>
</head>
<body>
<h2>Rock, Paper, Scissors</h2>
EOF

if ($qw->param('action')) {
	srand(time());
	my $choice = (qw/rock paper scissors/)[int(rand(3))];

	my $action = $qw->param('action');
	if (! grep { $action eq $_ } qw/r s p/) {
		print "You need to choose Rock Paper or Scissors<br/>";
		$action = "nothing";
	} else {
		$action = join "", map { ($_ =~ /^$action/)?$_:"" } qw/rock paper scissors/;
	}

	my $winner = "No-one";
	my ($happen, $midimg);
	my $wl = win($action,$choice);
	if ($wl eq "p") {
		$winner = "You";
		$midimg = "<img src='arrow_p.gif' alt='<-'><img src='win.gif' alt='Winner'>";
		$happen = make_action($action,$choice);
	} elsif ($wl eq "c") {
		$winner = "I";
		$midimg = "<img src='win.gif' alt='Winner'><img src='arrow_c.gif' alt='->'>";
		$happen = make_action($choice,$action);
	} elsif ($wl eq "d") {
		$winner = "No-one";
		$midimg = "<img src='arrow_p.gif' alt='<-'><img src='arrow_c.gif' alt='->'>";
		$happen = "";
	} elsif ($action eq "nothing") {
		$winner = "Because you didn't pick anything valid, I ";
		$happen = "Nothing loses to ".ucfirst($choice);
	} else {
		print "Something went wrong.<br/>\n";
		$winner = "No idea who";
	}
	
	print "
<table width='80%'>
<tr><td width='30%'>You chose $action<br/>
<img src='$images{$action}'>
</td>
<td>$midimg</td>
<td width='30%'>I chose $choice<br/>
<img src='$images{$choice}'>
</td></tr>
</table>
<br/>
";
	if ($happen) {
		print "$happen.<br/>\n";
	}
	print <<EOF;
$winner won that game.<br/>
<hr/>
<h3>Next game.</h3>
EOF
}

print <<EOF;
<p>Click on one of the images to play.</p>
<table>
<tr style='height:82px'>
<td>
<a href="$cgi?action=r">Rock<br/><img src='$images{rock}' alt="Rock" /></a>
</td><td>
<a href="$cgi?action=p">Paper<br/><img src='$images{paper}' alt="Paper" /></a>
</td><td>
<a href="$cgi?action=s">Scissors<br/><img src='$images{scissors}' alt="Scissors" /></a>
</td></tr></table>

<hr/>
<p><a href="/">Home</a>
<br/>
<br/>
<script type="text/javascript"><!--
google_ad_client = "pub-2355266279262436";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
</body>
</html>
EOF

sub make_action {
	my ($first,$second) = @_;
	my $what = action($first);

	$first = ucfirst($first);
	$second = ucfirst($second);

	return "<b>$first</b> $what <b>$second</b>";
}

sub win {
	my ($player, $comp) = @_;

	if (($player eq "rock") && ($comp eq "paper")) {
		return "c";
	} elsif (($player eq "rock") && ($comp eq "rock")) {
		return "d";
	} elsif (($player eq "rock") && ($comp eq "scissors")) {
		return "p";
	} elsif (($player eq "paper") && ($comp eq "paper")) {
		return "d";
	} elsif (($player eq "paper") && ($comp eq "rock")) {
		return "p";
	} elsif (($player eq "paper") && ($comp eq "scissors")) {
		return "c";
	} elsif (($player eq "scissors") && ($comp eq "paper")) {
		return "p";
	} elsif (($player eq "scissors") && ($comp eq "rock")) {
		return "c";
	} elsif (($player eq "scissors") && ($comp eq "scissors")) {
		return "d";
	} else {
		# Should never happen!
		return "ERROR";
	}
}

sub action {
	my $first = shift;

	if ($first eq "rock") {
		return "smashes";
	} elsif ($first eq "paper") {
		return "wraps";
	} elsif ($first eq "scissors") {
		return "cut";
	}
}

exit 0;
