The game \"rock-paper-scissors-lizard-spock\" is an extension of the commonly kn
ID: 3784919 • Letter: T
Question
The game "rock-paper-scissors-lizard-spock" is an extension of the commonly known game "rock-paperscissors". Rock-paper-scissors-lizard-spock adds two additional moves to the basic game. The arrows indicate which move beats which. For example, the arrow from paper to Spock indicates that paper disproves Spock (if one player plays paper, and the other plays Spock,
the one that played paper wins
There are 10 possible pairs of moves, so sometimes it’s hard to remember them. You will write a computer
program that will act as a referee. The program will accept the moves made by the two players via console
input and report which player won. To solve this problem, we’ll separate the console input and output from
the task of determining who won by writing a function that accepts the two moves made by the players
as parameters, and returns the winner. The main program will perform the console input, call the function
to determine the winner, and output the result to the console. Note that the computer is not one of the
players, the computer only determines whowon given themoves that the two human players made. Here’s
what you should do:
(a) Write a function called rock_paper_scissors_lizard_spock that has two parameters. These arguments
for these parameters must be one of the following ve strings: ’rock’, ’paper’, ’scissors’,
’lizard’, ’spock’. The rst parameter is the move made by player 1, the second is the move made
by player 2. The function must determine which player won, and return the integer 1 if player 1 was
victorious, and return the integer 2 if player 2 was victorious. If the game was a tie (both players made
the same move) then return the integer 0. You’ll need to use conditionals (if, elif, else) to determine
which player wins.
Hint: There’s a hard way and an easy way to do this question. You’ll know you’re doing it the hard way
if your program is very long. The hard way looks at the problem from the point of view of the moves;
the easy way looks at it from the point of view of the result (win, lose, draw).
(b) Write an appropriate docstring for your function in part (a).
(c) In the main program (after the function denition) write code to prompt for, and read the players moves
from the console. You may assume that the user always enters one of the ve strings listed in part (a)
that indicate legal moves.
(d) Pass the players’moves to the rock_paper_scissors_lizard_spock function and obtain its return value.
(e) Use the return value to print a message to the console indicating which player won, or whether it was
a tie. You’ll need to use conditionals to determine what message to print out.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
/*
* Function take argument as two players move. Legal moves are 'rock', 'scissors', 'paper'
* 'spock' and 'lizard'
*
* it return s0 if tie. 1 if player 1 won, 2 if player 2 won
*/
int rock_paper_scissors_lizard_spock (string player1, string player2)
{
if (player1 == player2)
{
return 0;
}
if(player1 == "scissors")
{
if (player2 == "paper" || player2 == "lizard")
return 1;
else
return 2;
}
if(player1 == "rock")
{
if (player2 == "scissors" || player2 == "lizard")
return 1;
else
return 2;
}
if(player1 == "paper")
{
if (player2 == "spock" || player2 == "rock")
return 1;
else
return 2;
}
if(player1 == "spock")
{
if (player2 == "scissors" || player2 == "rock")
return 1;
else
return 2;
}
if(player1 == "lizard")
{
if (player2 == "spock" || player2 == "paper")
return 1;
else
return 2;
}
}
int main()
{
string player1, player2;
cout << "Enter your move player 1: ";
cin >> player1;
cout << "Enter your move player 2: ";
cin >> player2;
int result = rock_paper_scissors_lizard_spock (player1, player2);
if (result == 1)
{
cout << "Player 1 won"<< endl;
}
else
{
if (result == 2)
{
cout << "Player 2 won"<< endl;
}
else
{
cout << "Tie"<< endl;
}
}
return 0;
}
$ g++ game.cpp
$ ./a.out
Enter your move player 1: spock
Enter your move player 2: rock
Player 1 won
# here is the python version
"""
Function take argument as two players move. Legal moves are 'rock', 'scissors', 'paper'
'spock' and 'lizard'
it return s0 if tie. 1 if player 1 won, 2 if player 2 won
"""
def rock_paper_scissors_lizard_spock (player1,player2):
if (player1 == player2):
return 0;
if(player1 == "scissors"):
if (player2 == "paper" or player2 == "lizard"):
return 1;
else:
return 2;
if(player1 == "rock") :
if (player2 == "scissors" or player2 == "lizard"):
return 1;
else:
return 2;
if(player1 == "paper") :
if (player2 == "spock" or player2 == "rock"):
return 1;
else:
return 2;
if(player1 == "spock") :
if (player2 == "scissors" or player2 == "rock"):
return 1;
else:
return 2;
if(player1 == "lizard") :
if (player2 == "spock" or player2 == "paper"):
return 1;
else:
return 2;
player1 = raw_input("Enter your move player 1: ");
player2 = raw_input("Enter your move player 2: ");
result = rock_paper_scissors_lizard_spock (player1, player2);
if (result == 1):
print("Player 1 won");
else:
if (result == 2):
print("Player 2 won");
else:
print("Tie");
'''
Sample run
$ python game.py
Enter your move player 1: spock
Enter your move player 2: rock
Player 1 won
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.