Last active
February 28, 2017 22:35
-
-
Save hugot/72f8c37a04b5906e9071da7b05e0b667 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# This is a short script to play rock paper scissors | |
use strict; | |
use warnings; | |
my @rps = qw(rock paper scissors); | |
my %weakness = (rock => "paper", paper => "scissors", scissors => "rock"); | |
my $score = 0; | |
my $npcscore = 0; | |
print "\nWelcome, how many rounds would you like to play?\nI would like to play best of:"; | |
chomp(my $rounds = <>); | |
for (my $i=1; $i<=$rounds; $i++) { | |
print "\n-------ROUND $i\-----\n"; | |
GETVALUE: print "Do you pick Rock, Paper or scissors?\nI pick:"; | |
chomp(my $pick = <>); | |
unless($pick =~ /^rock$|^paper$|^scissors$/i){ | |
print "$pick is not a valid argument\n"; | |
goto GETVALUE; | |
} | |
my $npcpick=$rps[rand(3)]; | |
if($pick =~ /$npcpick/i){ | |
print "You both picked $pick"; | |
} | |
elsif($weakness{lc($pick)} !~ /$npcpick/i){ | |
$score++; | |
print "Enemy picks: $npcpick You win this round! Your score: $score\n"; | |
} | |
else { | |
$npcscore++; | |
print "Enemy picks: $npcpick You lose this round! Npc score: $npcscore\n"; | |
} | |
} | |
if ($score > $npcscore){ | |
print "\nNpc score: $npcscore Your score: $score\n","-"x5,"YOU ARE VICTORIOUS!","-"x5,"\n"; | |
} | |
else { | |
print "\nNpc score: $npcscore, Your score: $score\n","-"x5,"YOU WERE UTTERLY CRUSHED","-"x5,"\n"; | |
} |
Wow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perl version of http://www.practicepython.org/exercise/2014/03/26/08-rock-paper-scissors.html :)