In: Computer Science
PYTHON
The game of Morra is a hand game that dates back to ancient Greece and Rome. In the real game, any number of players can play, but for this question we will assume two players only. The game is played in rounds. Each round, both players throw out one hand showing between 0 and 5 fingers and simultaneously call out what they think the sum of the fingers on the hands of all players will be. Any player who guesses correctly gets a point. The first player to 3 points wins. You will write a program that simulates a two-player game of Morra. Your program must do the following for each round of play until the game is over: • At the beginning of each round, print out the round number. The first round is round 1. • Read from the console the number of fingers shown by player 1, the number of fingers shown by player 2, player 1’s guess at the sum, and player 2’s guess at the sum. • Print to the console whether any players made a correct guess, and if so, that player’s new point total. If neither player guessed correctly, print a message indicating this. Note that BOTH players might guess correctly, in which case they both earn a point! Once the game is over, print the final outcome of the game. There are a few possibilities: • Print Player X wins! where X is either 1 or 2 depending on which player won. • If, however, the winning player won by a score of 3 to 0, instead print Player X wins a glorious victory!, again where X is either 1 or 2 as appropriate. • It is possible that the game is a tie. For example, if the score is 2 to 2, and both players guess correctly in the next round, both players will have three points when the game ends. In such a case, instead of printing either of the above messages, print It’s a tie!.
(a) Remember: you don’t have to generate player moves randomly. You are reading them from the console each round. Think of your program as the referee — it asks for the players moves each round using console input, then reports on the outcome of each round using console output, and finally prints the outcome of the game. (b) Your program only has to play one full game. To referee another game, run the program again! (c) You may assume that the user enters only valid data. That is, you do not have to actually check whether player moves are between 0 and 5 and that their guesses are between 0 and 10. Just assume that valid values are always entered.
Hi,
Hope you are doing fine. I have coded the above question in python and made sure that all the conditions mentioned in the questions are met. The code is clearly explained using comments which have been highlighted in bold. Even the assumptions made in the question are taken to be true about the input and valid entries.
Program:
#round_num stores the round number
round_num=0
#p1_points stores the points of player 1
p1_points=0
#p2_points stores the points of player 2
p2_points=0
#this loop is executed as long as p1_points and
p1_points are not equal to 3. It break when either of them is
3
while(p1_points!=3 and p2_points!=3):
#Incrementing the round_num at the start of each
iteration
round_num+=1
#printing round number
print("Round {}".format(round_num))
#Taking inputs from user and converting them into
integer.
p1_num=int(input("Enter the number of fingers shown by player 1:
"))
p2_num=int(input("Enter the number of fingers shown by player 2: "))
p1_guess=int(input("Enter the player 1's guess at sum: "))
p2_guess=int(input("Enter the player 2's guess at sum: "))
#if both players guess the sum correctly
if p1_num+p2_num==p1_guess and p1_num+p2_num==p2_guess:
#We increase the points of both players and print out their
new points
p1_points+=1
p2_points+=1
print("Both player 1 and player 2 made a correct guess!")
print("Player 1 new points: {}".format(p1_points))
print("Player 2 new points: {}".format(p2_points))
#if player 1 guesses the sum correctly
elif p1_num+p2_num==p1_guess:
#We increase the points of player 1 and print out his new
points
p1_points+=1
print("Player 1 made a correct guess!")
print("Player 1 new points: {}".format(p1_points))
#if player 2 guesses the sum correctly
elif p1_num+p2_num==p2_guess:
#We increase the points of player 2 and print out his new
points
p2_points+=1
print("Player 2 made a correct guess!")
print("Player 2 new points: {}".format(p2_points))
#if both the players do not guess correctly
else:
print("Neither of the players made a correct guess.")
print("")
#if player 1 wins over player 2 with a score of
3-0
if (p1_points==3 and p2_points==0):
print('\nPlayer 1 wins a glorious victory!')
#if player 2 wins over player 1 with a score of
3-0
elif (p1_points==0 and p2_points==3):
print('\nPlayer 2 wins a glorious victory!')
#if both players have same points
elif p1_points==p2_points:
print("\nIt’s a tie!")
#if player 1 points are 3
elif (p1_points==3):
print('\nPlayer 1 wins the game!')
#if player 2 points are 3
else:
print('\nPlayer 2 wins the game!')
print('Player 1 points: {}'.format(p1_points))
print('Player 2 points: {}'.format(p2_points))
Executable code snippet of the program from jupyter notebook:
Output: