Question

Help needed in Perl language program! Note : Perl Language

Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock paper scissors) is an extreme

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is commented - explaining all the functions and operations used:

Edit: code is updated and so is the output

Code ( image ) :

1 use strict; 2 use warnings; 4 sub eval-winner #Defining our eval-winner function 6 #storing player! and player2s inputs 831 32 suser-weapon- <STDIN; #Here we take the users input chomp suser-weapon; #clean the user input 34 #lie, 11 quit the infi

Output:

Enter your choice: Player1 won! Enter your choice Player1 won! Enter your choice Its a tie Enter your choice Turning offPres

Code (as plain text) :

use strict;
use warnings;

sub eval_winner #Defining our eval_winner function
{
#Storing player1 and player2's inputs
#The lc command changes the input to lowercase, so we can take care of an uppercase or lowercase input here
my $p1 = lc $_[0]; #catching the first arguement passed in the function
my $p2 = lc $_[1]; #catching the second arguement passed in the function

#The conditional statements that check for the winner
if ($p1 eq $p2) #If both player1 and player2 have the same input
{
#We check this condition first because the condition's are shorter and it makes the program faster
return 0;
}
elsif (($p1 eq "r" and $p2 eq "s") or ($p1 eq "s" and $p2 eq"p") or ($p1 eq "p" and $p2 eq "r")) #Here we check all three cases for player1, if any of them are true - player1 wins
{
return 1;
}
else #Since we have covered all the cases for a tie and player1 winning above, a simple else statement will catch the rest of the conditions where player2 wins
{
return 2;
}
}

while () #a loop with no parameter inside would be interpreted as true and become an infinite loop till we break it using "last"
{
print "Enter your choice: \n";
#Since we don't need global variables, we use my to declare local variables
my $user_weapon = <STDIN>; #Here we take the users input
chomp $user_weapon; #Clean the user input

#We'll quit the infinite loop here if the user types a q (Quit command)
if ($user_weapon eq "q")
{
print "Turning off";
last; #last breaks out of the loop
}

#Now we'll write the code that makes player2 (cpu) randomly choose a weapon
my @weapons = ("r","p","s"); #We declare an array with the three options, to make it even more redundant and hence have a higher variation in the weapon the cpu chooses
#we can repeat the elements of the array eg. ("r","p","s","r","p","s","r","p","s") and so on
my $cpu_weapon = $weapons[rand @weapons]; #The cpu chooses a random weapon from the array
#How does this work? "rand @weapons" returns a random index from the weapons array (i.e. from 0,1,2) and we pass that into the $weapons array again to retreive that random choice

my $answer = eval_winner($user_weapon,$cpu_weapon); #we call our function and store it in $answer to print the winner text on the screen
if ($answer eq 0)
{
print "It's a tie\n";
}
elsif ($answer eq 1)
{
print "Player1 won!\n";
}
else
{
print "Player2 won!\n";
}
}

1 use strict; 2 use warnings; 4 sub eval-winner #Defining our eval-winner function 6 #storing player! and player2's inputs 8 : my spl Ic s_[0]; #catching the first arguement passed in the function The 1c command changes the input to lowercase, so we can take care of an uppercase or lowercase input here 9 my Sp2 lc s [1]; catching the second arguement passed in the function The conditional statements that check for the winner : if (Spl eq Sp2) 12 #If both play er1 and player2 have the same input ie check this condition first because the condition's are shorter and it makes the progran faster return e; 17 elsif ((Spi eq-r-and Sp2 eq-s") or (S01 eq-s" and sp2 eq-p.) or (S01 eq-p" and Sp2 eq "r")) #Here we check all three cases for player1, if any of them are true-player1 wins return 1; 20 21 else #Since we have covered all the cases for a tie and player1 winning above, a sinple else statement will catch the rest of the conditions where player2 wins return 2 25 27 while () ·a loop with no parameter inside would be interpreted as true and become an infinite loop till we break it using "last" 29 print "Enter your choice: In 38 #Since we don't need global variables, we use my to declare local variables :

31 32 suser-weapon- <STDIN; #Here we take the users input chomp suser-weapon; #clean the user input 34 #lie, 11 quit the infinite loop here if the user types a q (Quit command) 35 if (Suser_weapon eq "q") 36 37 print "Turning off" 38 39 48 41 42 43 44 last; ast breaks out of the loop #Now we'll write the code that makes player2 (cpu) randomly choose a weapon my @weapons ("r","p", "s"); swe declare an array with the three options, to make it even more redundant and hence have a higher variation in the weapon the cpu chooses we can repeat the elements of the array eg. ("r,"p,s"r"P","r"p"s") and so on my $cpu-weapon : Sweapons[rand @weapons); #The cpu chooses a random weapon from the array #How does this work? "rand @weapons" r eturns a random index from the weapons array (i.e. from e, 1,2) and we pass that into the Sweapons array again to retreive that randon choice 47 my Sanswer eval_winner(Suser_weapon, scpu_weapon); #we call our function and store it in Sanswer to print the winner text on the screen print "It's a tiein 5e 51 52 elsif (Sanswer eq 1) 53 54 print "Player1 won! In" 56 57 58 59 6e print "player2 won! In"s

Sperl main.pl Enter your choice: q Player2 won! Enter your choice: p Turning off

Add a comment
Know the answer?
Add Answer to:
Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock p...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT