Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # Rock-paper-scissors example in perl by Scott Beardwood - This work is licensed under a Creative Commons
- # updated version posted at http://pastebin.com/g7BfBhcs
- use strict;
- use warnings;
- print "\n\nRock-paper-scissors\n\nBest of 3\nPlease Select...\n";
- my $p1 = 0;
- my $p2 = 0;
- my $count = 0;
- while ($count < 3) {
- print "Game ",$count+1,"\n\n1.Rock\n2.Paper\n3.Scissors\n?"; # $count+1 add one as count starts at zero
- my $selection = <>; #get users input
- chomp($selection); #remove newline
- my $computer = int(rand(2)) + 1; #generate random number for computer
- #show computers choice
- if ($computer == 1) {
- print "\nComputer picks $computer Rock\n";
- } elsif ($computer == 2) {
- print "\nComputer picks $computer Paper\n";
- } elsif ($computer == 3) {
- print "\nComputer picks $computer Scissors\n";
- } else {
- print "\nComputer is trying to cheat";
- }
- # Check for non-numeric characters
- if ($selection =~ /\D/){
- print "\nPlease enter a number (1, 2, ,3 ) \n";
- }
- my $x = $selection + $computer; # add users choice to computer to work out winner
- # GAME
- if ($selection == $computer){
- print "Its a draw\n";
- $count ++;
- next;
- }
- elsif ($x == 5) {
- print "Scissors beats paper\n";
- if ($selection eq 2) {print "You win\n";$p1 ++;} else {print "I win\n";$p2 ++;}
- $count ++;
- next;
- }
- elsif ($x == 3) {
- print "Paper beats rock\n";
- if ($selection eq 3) {print "You win\n";$p1 ++;} else {print "I win\n";$p2 ++;}
- $count ++;
- next;
- }
- elsif ($x == 4) {
- print "Rock beats Scissors\n";
- if ($selection eq 1) {print "You win\n";$p1 ++;} else {print "I win\n";$p2 ++;}
- $count ++;
- next;
- } else {
- print "Error did not compute ( debug = selection = $selection computer = $computer added valuer = $x )\n";
- }
- } # end while loop
- print "\n Final score Player 1 = $p1 Computer = $p2\n";
- if ($p1 > $p2) {
- print "Player one wins\n";
- }
- elsif ($p1 == $p2) {
- print "It was a Draw\n";
- }
- else {
- print "Computer Wins\n";
- }
- print "\n\nHit a Key to End";<>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement