python Description For this program you will be implementing one attack in a tur
ID: 3594112 • Letter: P
Question
python
Description
For this program you will be implementing one attack in a turn based battle game.
Each character has the following stats
Attacks deal damage between their strength and half their strength rounded down.
For example if a characters strength was 9 they could do between 4 and 9 points of damage
The game works as follows
The attacker has a critical hit chance. If they score a critical hit their attack cannot miss and they do their strength + 1 damage
If the attacker does not score a critical hit they make a normal attack based on their accuracry
If the attacker would successfully land a hit the defender attempts to dodge the attack based on their dodge stat
If the dodge is successful the defender receives no damage
If the dodge fails the defender takes a random amount of damage between the attacker's strength and half the attacker's strength rounded down
Damage is dealt in integer units
If the attacker fails to hit the defender because they missed or the defender dodged the attack the defender makes a counter attack
The attacker can dodge this attack but not counter attack back
Additionaly the defender could have been in a guarded stance
If the defender was in a guarded stance then they will counter attack regardless of whether they dodge or not
If however the attacker manages to kill the defender, reduce their hp to 0, the defender does not get to counter attack because they are dead
Your program should report crits, hits, misses, dodges, and how much health both the attacker and defender have at the end.
Your program should be able to handle both upper and lower case letters for the guard command
Example 1
Enter the seed to run the fight with: 2
Enter the attacker's hp: 10
Enter the attacker's strength: 10
Enter the attacker's accuracy: 80
Enter the attacker's crit chance: 20
Enter the attacker's dodge rate: 50
Enter the defender's hp: 10
Enter the defender's strength: 10
Enter the defender's accuracy: 80
Enter the defender's crit chance: 20
Enter the defender's dodge rate: 50
Is the defender guarding? Y for yes, n for no: n
attacker missed defender
defender crit attacker for 11 points of damage
After fighting the attacker has 0 hp left and the defender has 10 hp left
Attacks deal damage between their strength and half their strength rounded down.
For example if a characters strength was 9 they could do between 4 and 9 points of damage
Accuracry The odds of landing an attack Dodge The odds of avoiding an attack Crit Chance The odds of landing a critical hit. A critical hit never misses and does a strength + 1 damageExplanation / Answer
import random def main(): """Main function that will welcome the player to the game.""" print(" Welcome to Battle Sim! This is a turn based combat simulator where") print(" there can only be one winner.") print(" How to play. Players take turn to choose a move. Moves can either deal moderate damage") print("with a low range, deal high damage but over a wide") print("range, or they can heal. (Note: Moves can miss, including Heal!)") print(" Each player starts with 100 health, and the first") print("player to reduce their opponent to 0 is the winner.") print(" That's it! Good luck") play_again = True # Set up the play again loop while play_again: winner = None player_health = 100 computer_health = 100 # determine whose turn it is turn = random.randint(1,2) # heads or tails if turn == 1: player_turn = True computer_turn = False print(" Player will go first.") else: player_turn = False computer_turn = True print(" Computer will go first.") print(" Player health: ", player_health, "Computer health: ", computer_health) # set up the main game loop while (player_health != 0 or computer_health != 0): heal_up = False # determine if heal has been used by the player. Resets false each loop. miss = False # determine if the chosen move will miss. # create a dictionary of the possible moves and randomly select the damage it does when selected moves = {"Punch": random.randint(18, 25), "Mega Punch": random.randint(10, 35), "Heal": random.randint(20, 25)} if player_turn: print(" Please select a move: 1. Punch (Deal damage between 18-25) 2. Mega Punch (Deal damage between 10-35) 3. Heal (Restore between 20-25 health) ") player_move = input("> ").lower() move_miss = random.randint(1,5) # 20% of missing if move_miss == 1: miss = True else: miss = False if miss: player_move = 0 # player misses and deals no damage print("You missed!") else: if player_move in ("1", "punch"): player_move = moves["Punch"] print(" You used Punch. It dealt ", player_move, " damage.") elif player_move in ("2", "mega punch"): player_move = moves["Mega Punch"] print(" You used Mega Punch. It dealt ", player_move, " damage.") elif player_move in ("3", "heal"): heal_up = True # heal activated player_move = moves["Heal"] print(" You used Heal. It healed for ", player_move, " health.") else: print(" That is not a valid move. Please try again.") continue else: # computer turn move_miss = random.randint(1,5) if move_miss == 1: miss = True else: miss = False if miss: computer_move = 0 # the computer misses and deals no damage print("The computer missed!") else: if computer_health > 30: if player_health > 75: computer_move = moves["Punch"] print(" The computer used Punch. It dealt ", computer_move, " damage.") elif player_health > 35 and player_health 35 and player_health 100: computer_health = 100 else: if player_turn: computer_health -= player_move if computer_health < 0: computer_health = 0 # cap minimum health at 0 winner = "Player" break else: player_health -= computer_move if player_health < 0: player_health = 0 winner = "Computer" break print(" Player health: ", player_health, "Computer health: ", computer_health) # switch turns player_turn = not player_turn computer_turn = not computer_turn # once main game while loop breaks, determine winner and congratulate if winner == "Player": print(" Player health: ", player_health, "Computer health: ", computer_health) print(" Congratulations! You have won. You're an animal!!") else: print(" Player health: ", player_health, "Computer health: ", computer_health) print(" Sorry, but your opponent wiped the floor with you. Better luck next time.") print(" Would you like to play again?") answer = input("> ").lower() if answer not in ("yes", "y"): play_again = False main()Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.