Question: PYTHON CODE WITH COMMENTING Requirements - Player Class The requirements for the Player class are given below. Please ensure that your methods have the EXACT

PYTHON CODE WITH COMMENTING

Requirements - Player Class

The requirements for the Player class are given below. Please ensure that your methods have the EXACT naming as specified! Failure to do so will result in lost marks.

Note: you must include a try and except statement in at least one function in your code.

  1. Create an __init__(name) method that takes the following arguments:

    • name: A string representing the name of the player. The variable is assigned to an instance variable. If name is an empty string, the instance variable is set to the string "Default".

    The method also initializes the following instance variables:

    a. An int representing the players health. The players health is initially set to 100. b. An int representing the players level. The players initial level is 0. c. An int representing the players current score. The players initial score is 0. d. An int representing the score required to reach the next level. The initial value is 50. e. A list of lists representing the current set of attacks. Each sub-list has a length of 2. The first element is a string that represents the name of the attack and the second element is an int representing the strength of the attack. A player has 3 different attacks. The players initial attacks are provided below:

[["Fast attack",5],["Slow attack",15],["Default Special Attack",20]]

  1. Create the following accessor methods:

    a. get_name: Returns the players name. b. get_health: Returns the players health. c. get_level: Returns the players level. d. get_score: Returns the players current score e. get_next_lvl_score: Returns the score required to reach the next level. f. get_attacks: Returns the players attack set.

  1. Create the mutator method raise_health(val).

    • val: An int.
    • Method description: The method does not return anything, but increases the players health by val. If the val is negative, the method does nothing. The player's health cannot exceed 100 so if the sum of their current health and val exceeds 100, then set health to 100.
  1. Create the mutator method replace_attack(i, name, strength).
    • i: An int representing an index (either 0, 1, or 2).
    • name: A string representing the name of an attack.
    • strength: An int representing the strength of the attack associated with name.
    • Method description: The method replaces the list at index i in the attack list instance variable with a list of length 2 where the first element is name and the second element is strength.
  1. Create the mutator method take_damage(quantity).
    • quantity: An int.
    • Method description: The method deducts quantity from the instance variable that stores the players health. The players health is set to 0 if quantity is greater than or equal to the players current health.
  1. Create the mutator method perform_attack(i, player_2).

    • i: An int representing an index. Assume that the value must be either 0,1, or 2.

    • player_2: A Player object.

    • Method description: The method simulates a user attack on player_2 using the attack at index i from the attack instance variable list of lists. The method performs the following actions:

      • player_2s health is calculated and stored in a variable.
      • The object calling the method uses the attack associated with index i from the current set of attacks to inflict damage on player_2 (i.e player_2 will take damage equal to attack is strength).
      • The difference between player_2s current health and player_2s health before the attack is calculated and stored in the variable damage_given.
      • The value of damage_given is added to the players current score if the sum of damage_given and current score will result in a value less than the instance variable containing the score required to reach the next level.
      • If the sum of damage_given and player score results in a value equal to or higher than the score required to reach the next level, then the following actions are performed
        • a. The players level is increased by 1.
        • b. The players current score is set to 0.
        • c. The score required to reach the next level is increased by 20.
        • d. Each of the player's attack strengths are increased by 5.

Requirements - Battle Simulation

In addition to the Player class, you are required to implement a function to simulate a battle. The function definition is given below:

  1. Create the function simulate_battle(player_1, player_2, player_1_moves, player_2_moves).

    • player_1: A Player object.

    • player_2: A Player object.

    • player_1_moves: A list of integers where each element is between 0 and 2 (inclusive).

    • player_2_moves: A list of integers where each element is between 0 and 2 (inclusive).

    • Method Description: The function performs the following algorithm:

      • For each element i in player_1_moves, player_1 attacks player_2 using the perform_attack method with attack i. If player_2's health becomes 0 at any point, player_1 wins the battle.
      • Then, for each element i in player_2_moves, player_2 attacks player_1 using the perform_attack method with attack i. If player_1's health becomes 0 at any point, player_2 wins the battle.

      Note: All Player 1 attacks should occur first before all Player 2 attacks

    • Return: A string representing the name of the player object that won the battle. If there is no winner, an empty string is provided.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!