Skip to content

Instantly share code, notes, and snippets.

@jgrar
Last active April 3, 2018 02:37
Show Gist options
  • Save jgrar/a856a3bb0157e127b2c285dfb13e1e6d to your computer and use it in GitHub Desktop.
Save jgrar/a856a3bb0157e127b2c285dfb13e1e6d to your computer and use it in GitHub Desktop.
Rock paper scissors game
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use constant SYM => qw(rock scissors paper);
my $msg = "Enter 0 for rock, 1 for scissors, 2 for paper, or enter 'quit' to exit\n";
for (print $msg; my $player = <STDIN>; print $msg) {
chomp $player;
last if $player =~ /^quit$/i;
next if $player !~ /^(0|1|2)$/;
my $computer = int rand (SYM);
print "Player: ", (SYM)[$player], " Computer: ", (SYM)[$computer], "\n";
if ($player == $computer) {
print "Result is a draw!\n";
} else {
if (($player + 1) % (SYM) == $computer) {
print((SYM)[$player], " beats ", (SYM)[$computer], ", you won!\n");
} else {
print((SYM)[$computer], " beats ", (SYM)[$player], ", I won (^:\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment