Untitled

 avatar
unknown
plain_text
9 months ago
4.7 kB
11
Indexable
import math

class Robot:
    def __init__(self):
        self.x = 100
        self.y = 100
        self.health = 100
        self.energy = 100

    def move(self, newx, newy):
        if self.energy > 0:
            self.x = newx
            self.y = newy

    def draw(self):
        print(f"(draw) Robot at ({self.x},{self.y})")

    def displayStatus(self):
        print(f"x={self.x}, y={self.y}, health={self.health}, energy={self.energy}")

    def command(self, robotList):
        print("Possible actions: move")
        try:
            newx = int(input("Enter new x coordinate: "))
            newy = int(input("Enter new y coordinate: "))
        except Exception:
            print("Invalid input")
            return
        self.move(newx, newy)
class MedicBot(Robot):
    # no additional properties
    def heal(self, r: Robot):
        dist = math.hypot(self.x - r.x, self.y - r.y)
        if self.energy >= 20 and dist <= 10:
            self.energy -= 20
            r.health = min(100, r.health + 10)
            print(f"Medic healed robot at index: new health={r.health}, medic energy={self.energy}")
        else:
            print("Cannot heal (not enough energy or too far)")

    def command(self, robotList):
        print("Possible actions: move, heal other")
        choice = input("Enter 'm' to move, 'h' to heal another robot: ").strip().lower()
        if choice == 'm':
            super().command(robotList)
        elif choice == 'h':
            if len(robotList) == 0:
                print("No robots")
                return
            for i, rb in enumerate(robotList):
                print(i, type(rb).__name__, f"pos=({rb.x},{rb.y}) health={rb.health}")
            try:
                idx = int(input("Which robot index to heal? "))
                target = robotList[idx]
                self.heal(target)
            except Exception:
                print("Invalid index")
        else:
            print("Unknown action")
class StrikerBot(Robot):
    def __init__(self):
        super().__init__()
        self.missile = 5

    def strike(self, r: Robot):
        dist = math.hypot(self.x - r.x, self.y - r.y)
        if self.energy >= 20 and self.missile > 0 and dist <= 10:
            self.energy -= 20
            self.missile -= 1
            r.health -= 50
            if r.health < 0:
                r.health = 0
            print(f"Strike success! target health={r.health}, striker missiles={self.missile}, energy={self.energy}")
        else:
            print("Cannot strike (insufficient energy/missiles or too far)")

    def displayStatus(self):
        print(f"x={self.x}, y={self.y}, health={self.health}, energy={self.energy}, missile={self.missile}")

    def command(self, robotList):
        print("Possible actions: move, strike other")
        choice = input("Enter 'm' to move, 's' to strike another robot: ").strip().lower()
        if choice == 'm':
            super().command(robotList)
        elif choice == 's':
            if len(robotList) == 0:
                print("No robots")
                return
            for i, rb in enumerate(robotList):
                print(i, type(rb).__name__, f"pos=({rb.x},{rb.y}) health={rb.health}")
            try:
                idx = int(input("Which robot index to strike? "))
                target = robotList[idx]
                self.strike(target)
            except Exception:
                print("Invalid index")
        else:
            print("Unknown action")
def RobotBattle():
    robotList = []
    while True:
        print("\nRobots:")
        for i, r in enumerate(robotList):
            print(i, type(r).__name__, f"pos=({r.x},{r.y}) health={r.health} energy={r.energy}")
        print("Commands: 'c' create new robot, index number to command that robot, 'q' quit")
        choice = input("Enter choice: ").strip().lower()
        if choice == 'q':
            break
        elif choice == 'c':
            rtype = input("Enter robot type 'r' Robot, 'm' MedicBot, 's' StrikerBot: ").strip().lower()
            if rtype == 'r':
                robotList.append(Robot())
            elif rtype == 'm':
                robotList.append(MedicBot())
            elif rtype == 's':
                robotList.append(StrikerBot())
            else:
                print("Unknown type")
        else:
            try:
                idx = int(choice)
                robotList[idx].command(robotList)
            except Exception:
                print("Invalid choice/index")

        robotList = [r for r in robotList if r.health > 0]

RobotBattle()
Editor is loading...
Leave a Comment