I have been making this game in python, thanks to the documentation that has been given to me in class, but now they have asked me to modify a series of things, the problem is that when I implement it to my code it stops working since the new thing that indicates is not very clear to me. I have serious problems with this code. Since I have to create a get_winner_action and a get_random_computer_action() , I don't know where to start. I would be grateful if you could help me or explain since it is not clear to me.
```
```
The problem is that I don't know how I can create the get_winner since I don't understand it, how I can create the function.
```
#!/usr/bin/python3
import random
#1.15--------------------------------------------------------------
from enum import IntEnum
class GameAction(IntEnum):
Piedra = 0
Papel = 1
Tijeras = 2
#1.15--------------------------------------------------------------
#1.17------------------------------------------------------------------
def get_user_action():
# Scalable to more options (beyond rock, paper and scissors...)
game_choices = [f"{game_action.name}[{game_action.value}]"
for game_action in GameAction]
game_choices_str = ", ".join(game_choices)
user_selection = int(input(f"\nPick a choice ({game_choices_str}): "))
user_action = GameAction(user_selection)
return user_action
#1.17------------------------------------------------------------------
#1.18------------------------------------------------------------------
def get_computer_action():
computer_selection = random.randint(0, len(GameAction) - 1)
computer_action = GameAction(computer_selection)
print(f"Computer picked {computer_action.name}.")
return computer_action
#1.18------------------------------------------------------------------
#1.19------------------------------------------------------------------
def play_another_round():
another_round = input("\nAnother round? (y/n): ")
return another_round.lower() == 'y'
#1.19------------------------------------------------------------------
#1.16------------------------------------------------------------------
def main():
while True:
try:
user_action = get_user_action()
except ValueError as e:
range_str = f"[0, {len(GameAction) - 1}]"
print(f"Invalid selection. Pick a choice in range {range_str}!")
continue
computer_action = get_computer_action()
assess_game(user_action, computer_action)
if not play_another_round():
break
#1.16------------------------------------------------------------------
def assess_game(user_action, computer_action):
if user_action == computer_action:
print(f"User and computer picked {user_action}. Draw game!")
# You picked PIEDRA
elif user_action == PIEDRA:
if computer_action == TIJERAS:
print("PIEDRA smashes TIJERAS. You won!")
else:
print("PAPEL covers PIEDRA. You lost!")
# You picked PAPEL
elif user_action == PAPEL:
if computer_action == PIEDRA:
print("PAPEL covers PIEDRA. You won!")
else:
print("TIJERAS cuts PAPEL. You lost!")
# You picked TIJERAS
elif user_action == TIJERAS:
if computer_action == PIEDRA:
print("PIEDRA smashes TIJERAS. You lost!")
else:
print("TIJERAS cuts PAPEL. You won!")
if __name__ == "__main__":
main()
#1.20------------------------------------------------------------------
def main():
game_history = []
user_actions_history = []
while True:
try:
user_action = get_user_action()
user_actions_history.append(user_action)
except ValueError as e:
range_str = f"[0, {len(GameAction) - 1}]"
print(f"Invalid selection. Pick a choice in range {range_str}!")
continue
computer_action = get_computer_action(user_actions_history, game_history)
game_result = assess_game(user_action, computer_action)
game_history.append(game_result)
if not play_another_round():
break
#1.20------------------------------------------------------------------
#1.21------------------------------------------------------------------
class GameResult(IntEnum):
Victory = 0
Defeat = 1
Tie = 2
Victories = {
GameAction.Piedra: GameAction.Papel,
GameAction.Papel: GameAction.Tijeras,
GameAction.Tijeras: GameAction.Piedra
}
#1.21------------------------------------------------------------------
NUMBER_RECENT_ACTIONS = 5
def get_computer_action(user_actions_history, game_history):
# No previous user actions =>random computer choice
if not user_actions_history or not game_history:
computer_action = = get_random_computer_action()
# Alternative AI functionality
# Choice that would beat the user’s most frequent recent choice
else:
most_frequent_recent_computer_action = \
GameAction(mode(user_actions_history[-NUMBER_RECENT_ACTIONS:]))
computer_action = get_winner_action(most_frequent_recent_computer_action)
print(f"Computer picked {computer_action.name}.")
return computer_action
```
The problem is that I don't know how I can create the get_winner since I don't understand it, how I can create the function.
[ICODE][ICODE]
[/ICODE][/ICODE]
Last edited: