#!/usr/bin/perl -wT # licence: GPL # author: Ian Hickson # initialise system use strict; # be anal about stuff use diagnostics; # enable fuller diagnostics of warnings use CGI; # Initialize Parameters my $query = new CGI; my %moves = ( 'R' => 'Rock', 'P' => 'Paper', 'S' => 'Scissors', ); my %table = ( 'R' => { 'R' => 0, 'P' => -1, 'S' => 1, }, 'P' => { 'R' => 1, 'P' => 0, 'S' => -1, }, 'S' => { 'R' => -1, 'P' => 1, 'S' => 0, }, ); my %winningMove = ( 'R' => 'P', 'P' => 'S', 'S' => 'R', ); my %src = ( 'R' => 'http://www.worldrps.com/images/rock.gif', 'P' => 'http://www.worldrps.com/images/paper.gif', 'S' => 'http://www.worldrps.com/images/scissors.gif', ); my %gambits = ( 'PSR' => 'You last played: The Crescendo', 'RSP' => 'You last played: The Denouement', 'RPP' => 'You last played: A Fistful o\' Dollars', 'RRR' => 'You last played: An Avalanche', 'PSP' => 'You last played: A Scissor Sandwich', 'PSS' => 'You last played: Paper Dolls', ); # print page print < Rock Paper Scissors

Rock Paper Scissors

EOF my $move; if ($query->param('R') or $query->param('R.x')) { $move = 'R'; } elsif ($query->param('S') or $query->param('S.x')) { $move = 'S'; } elsif ($query->param('P') or $query->param('P.x')) { $move = 'P'; } else { $move = $query->param('move'); } &play($query->param('myWins'), $query->param('yourWins'), $query->param('myHistory'), $query->param('yourHistory'), $query->param('strategy'), $move); print <

Reset Scores

See also: The Official Rock Paper Scissors Strategy Guide

EOF # end # routines sub play { my($myWins, $yourWins, $myHistory, $yourHistory, $strategy, $yourMove) = @_; my $myMove; my $winner; my $youClass = ''; my $meClass = ''; my $comment = ''; if (defined($myWins) and defined($yourWins) and defined($myHistory) and defined($yourHistory) and $yourMove) { $myWins = ($myWins or 0); $yourWins = ($yourWins or 0); $myMove = &getMove($myWins, $yourWins, $myHistory, $yourHistory, $yourMove, $strategy); $myHistory .= $myMove; $yourHistory .= $yourMove; if ($table{$myMove}->{$yourMove} > 0) { $myWins++; $winner = 'I win!'; } elsif ($table{$myMove}->{$yourMove} < 0) { $yourWins++; $winner = 'You win.'; } else { $winner = 'Tie.'; } if (length($yourHistory) >= 3) { $yourHistory =~ /(...)$/o; if (exists($gambits{$1})) { $comment = $gambits{$1}; } else { $comment = "You last played: $moves{$yourMove}"; } } print <Last Round:
Your Move My Move
$moves{$yourMove} $moves{$myMove}

$winner

Scores:

You:$yourWins
Me: $myWins

$comment

Next Round:

EOF } else { $myWins = '' unless defined($myWins); $yourWins = '' unless defined($yourWins); $myHistory = '' unless defined($myHistory); $yourHistory = '' unless defined($yourHistory); print <Start The Game:

What will your move be?

EOF } my %selectedStrategies = ( 'cheater' => '', 'random' => '', ); if (defined($strategy)) { $selectedStrategies{$strategy} = ' selected="selected"'; } print <

Select My Strategy:

EOF } sub getMove { my($myWins, $yourWins, $myHistory, $yourHistory, $yourMove, $strategy) = @_; if ($strategy eq 'random') { return getMoveRandom($myWins, $yourWins, $myHistory, $yourHistory, $yourMove); } else { return getMoveCheater($myWins, $yourWins, $myHistory, $yourHistory, $yourMove); } } sub getMoveRandom { my($myWins, $yourWins, $myHistory, $yourHistory, $yourMove) = @_; my $random = rand(3); if ($random < 1) { return 'R'; } elsif ($random < 2) { return 'S'; } else { return 'P'; } } sub getMoveCheater { my($myWins, $yourWins, $myHistory, $yourHistory, $yourMove) = @_; return $winningMove{$yourMove}; }