Skip to content

Instantly share code, notes, and snippets.

@leklund
Created October 30, 2011 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leklund/1326607 to your computer and use it in GitHub Desktop.
Save leklund/1326607 to your computer and use it in GitHub Desktop.
Rock, paper, scissors, spock, lizard in Perl Dancer
#!/usr/bin/env perl
use Dancer;
hook 'before'=> sub {
header('Content-Type' => 'text/plain');
my $throwmap= {
rock => { scissors => 'rock smashes scissors',
lizard => 'rock crushes lizard'},
paper => { rock => 'paper covers rock',
spock => 'paper disproves spock'},
scissors => { paper => 'scissors slices paper',
lizard => 'scissors decapitate lizard'},
lizard => { paper => 'lizard eats paper',
spock => 'lizard poisons spock'},
spock => { rock => 'spock vaporizes rock',
scissors => 'spock smashes scissors'}
};
my @throws = keys %{$throwmap};
var throwmap => $throwmap;
var throws => \@throws;
};
get '/throws/:type' => sub {
my $user_throw = params->{type};
unless (grep { $_ eq params->{type} } @{vars->{throws} }) {
status 'not found';
return "You must throw one of the following: @{vars->{throws}}";
}
my $computer_throw = vars->{throws}->[int(rand(5))];
if ($computer_throw eq $user_throw) {
"You tied! Try again";
} elsif (exists vars->{throwmap}->{$user_throw}->{$computer_throw}) {
"WIN! ". vars->{throwmap}->{$user_throw}->{$computer_throw};
} else {
"LOSE! ". vars->{throwmap}->{$computer_throw}->{$user_throw};
}
};
dance;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment