Есть желание сделать код более читаемым и понятным, сократить кол-во знаков при той функциональности, сделать его более оптимальным и быстрым!

import random
from exception import GameOver, EnemyDown
from settings import PLAYER_HEALTH, PLAYER_SCORE, CHOISE_VARIANTS


class Enemy:
    def __init__(self, lvl: int):
        self.lvl = lvl
        self.health = lvl

    def decrease_health(self):
        self.health -= 1
        if self.health < 1:
            raise EnemyDown

    @staticmethod
    def random_choice():
        return CHOISE_VARIANTS[random.randint(0, 2)]


class Player:
    def __init__(self, name: str):
        self.name = name
        self.health = PLAYER_HEALTH
        self.score = PLAYER_SCORE

    def decrease_health(self):
        self.health -= 1
        if self.health < 1:
            raise GameOver

    @staticmethod
    def select_choice():
        choice: int = 0
        while choice not in [1, 2, 3]:
            try:
                choice = int(input("MAKE A FIGHT CHOICE FROM (WARRIOR - 1, ROBBER - 2, WIZARD - 3: "))
            except ValueError:
                pass
        return choice

    @staticmethod
    def fight(attack, defence):
            fight_result = 0
            if attack == 1:
                if defence == 1:
                    fight_result = 1
                if defence == 2:
                    fight_result = 2
                if defence == 3:
                    fight_result = 3

            if attack == 2:
                if defence == 1:
                    fight_result = 2
                if defence == 2:
                    fight_result = 1
                if defence == 3:
                    fight_result = 3

            if attack == 3:
                if defence == 1:
                    fight_result = 3
                if defence == 2:
                    fight_result = 2
                if defence == 3:
                    fight_result = 1
            return fight_result

    def defense(self, enemy_obj: Enemy):
        result = {1: "IT'S A DRAW!",
                  2: "YOUR DEFENSE IS SUCCESSFUL!",
                  3: self.decrease_health}
        attack = self.select_choice()
        defense = enemy_obj.random_choice()
        fight = self.fight(attack, defense)
        if fight == 3:
            print("YOUR DEFENSE HAS BEEN BREACHED!")
            return result[fight]()
        print(result[fight])

    def attack(self, enemy_obj: Enemy):
        result = {1: "IT'S A DRAW!",
                  2: enemy_obj.decrease_health,
                  3: "YOUR ATTACK IS FAILED!"}
        attack = self.select_choice()
        defense = enemy_obj.random_choice()
        fight = self.fight(attack, defense)
        if fight == 2:
            print("YOUR ATTACK IS SUCCESSFUL!")
            self.score += 1
            return result[fight]()
        print(result[fight])

Ответы (1 шт):

Автор решения: splash58

вот вам кусочек кода

def cmp(a, b):
    # возвращает
    # -1 - а проиграл
    #  0 - ничья
    # +1 - a выиграл

    if abs(a-b) < 2:
        return (a > b) - (b > a)
    return (a < b) * 2 - 1


for a in range(1, 4):         # а удобнее от 0 до 2, тоже будет работать
    for b in range(1, 4):
        print(a, b, cmp(a, b))
→ Ссылка