본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 83

반응형

Q>

...the clashes between different soldiers occurred here and there, and the new troops kept coming.

(... 여러 군인 들간의 충돌이 여기저기서 발생했고 새로운 군대가 계속오고있었습니다.)

The conflict gradually was starting to look more like a small war. 

(갈등은 점점 더 작은 전쟁처럼 보이기 시작했습니다. )
"Knights, hear my command! Take your shields! Strengthen the armor! We are taking too much beating,"

("기사 들아, 내 명령을 들으 라! 방패를 잡아라! 갑옷을 강화하라! 너무 많이 때리고있다.")

- Sir Ronald shouted. 

(- 로널드 경이 소리 쳤다. )
Nobody’s expected that Umbert's soldiers could compete with the well-trained knights, so at the beginning of the battle the knights used exclusively two-handed swords - no one even thought of being on the defensive.

(Umbert의 병사들이 잘 훈련 된 기사들과 경쟁 할 수있을 것으로 기대 한 사람은 아무도 없으므로 전투가 시작될 때 기사들은 양손의 검을 사용했습니다. 아무도 방어 중이라고 생각하지 않았습니다.)

But it seems that it's time to back down and take one-handed swords and shields instead of the former deadly weapons.

(그러나 전 치명적인 무기 대신에 한 손으로 든 칼과 방패를 쓰러 뜨릴 때가 된 것 같습니다.)

This will slightly reduce the assault capacity of knights, but will allow them to better defend themselves against the dangerous attacks of enemy soldiers.

(이것은 기사단의 공격 능력을 약간 감소 시키지만, 적군 병사들의 위험한 공격으로부터 더 잘 방어 할 수있게합니다.)

In the previous mission - Army battles, you've learned how to make a battle between 2 armies.

(이전의 미션에서 - 군대와의 전투에서 두 군대 간의 전투 방법을 배웠습니다.)

But we have only 2 types of units - the Warriors and Knights. Let's add another one - the Defender.

(그러나 우리는 전사와 기사의 2 종류 만 있습니다. Defender라고하는 또 다른 것을 추가합시다.

It should be the subclass of the Warrior class and have an additional defense parameter, which helps him to survive longer.

(Warrior 클래스의 하위 클래스 여야하며 추가 방어 매개 변수가 있어야합니다.이 매개 변수를 사용하면 더 오래 생존 할 수 있습니다.)

When another unit hits the defender, he loses a certain amount of his health according to the next formula: enemy attack - self defense (if enemy attack > self defense). Otherwise, the defender doesn't lose his health.

(다른 유닛이 수비수를 때릴 때, 그는 다음 수식에 따라 일정량의 건강을 잃습니다 : 적의 공격 - 자기 방어 (적의 공격> 자기 방어의 경우). 그렇지 않으면 수비수가 건강을 잃지 않습니다. )
The basic parameters of the Defender:

(Defender의 기본 매개 변수 :)

health = 60
attack = 3
defense = 2

 

Example:

chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
bob = Defender()
mike = Knight()
rog = Warrior()
lancelot = Defender()

fight(chuck, bruce) == True
fight(dave, carl) == False
chuck.is_alive == True
bruce.is_alive == False
carl.is_alive == True
dave.is_alive == False
fight(carl, mark) == False
carl.is_alive == False
fight(bob, mike) == False
fight(lancelot, rog) == True

my_army = Army()
my_army.add_units(Defender, 1)
    
enemy_army = Army()
enemy_army.add_units(Warrior, 2)

army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Defender, 1)

army_4 = Army()
army_4.add_units(Warrior, 2)

battle = Battle()

battle.fight(my_army, enemy_army) == False
battle.fight(army_3, army_4) == True

Input: The warriors and armies.

         (전사와 군대.)

 

Output: The result of the battle (True or False).

           (전투의 결과 (참 또는 거짓).)

 

How it is used: For the computer games development.

                     (컴퓨터 게임 개발 용.)

 

Note: From now on, the tests from "check" part will use another type of warrior: the rookie. Its code is

        (이제부터는 "확인"부분의 테스트에서 다른 유형의 전사 인 신인이 사용됩니다. 그 코드는)

class Rookie(Warrior):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.health = 50
        self.attack = 1

 

Precondition: 3 types of units

 

A>

class Warrior:
    def __init__(self, health=50, attack=5):
        self.health = health
        self.attack = attack

    @property
    def is_alive(self):
        return self.health > 0

    def hit(self, other):
        other.loss(self.attack)

    def loss(self, attack):
        self.health -= attack

class Knight(Warrior):
    def __init__(self):
        super().__init__(attack=7)

class Defender(Warrior):
    def __init__(self):
        super().__init__(health=60, attack=3)
        self.defense = 2

    def loss(self, attack):
        self.health -= max(0, attack - self.defense)

def fight(unit_1, unit_2):
    while 1:
        unit_1.hit(unit_2)
        if unit_2.health <= 0:
            return True
        unit_2.hit(unit_1)
        if unit_1.health <= 0:
            return False

class Army:
    def __init__(self):
        self.units = []
    def add_units(self, unit_class, count):
        for _ in range(count):
            self.units.append(unit_class())

    @property
    def first_alive_unit(self):
        for unit in self.units:
            if unit.is_alive:
                return unit

    @property
    def is_alive(self):
        return self.first_alive_unit is not None

'''
@staticmethod
정적 메소드 / 인스턴스에서도 접근이 가능
'''
class Battle:
    @staticmethod
    def fight(army_1, army_2):
        while army_1.is_alive and army_2.is_alive:
            fight(army_1.first_alive_unit, army_2.first_alive_unit)

        return army_1.is_alive

 

O>

unit_1 = Defender() unit_2 = Rookie() unit_3 = Warrior() fight(unit_1, unit_2) fight(unit_1, unit_3)

Your result:
true

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 85  (0) 2019.06.21
Python Learn the basics Quiz 84  (0) 2019.06.21
Python Learn the basics Quiz 82  (0) 2019.06.21
Python Learn the basics Quiz 81  (0) 2019.06.20
Python Learn the basics Quiz 80  (0) 2019.06.20