Python Forum
Using the robot class - implement robot battles - Python OOP - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Using the robot class - implement robot battles - Python OOP (/thread-33685.html)



Using the robot class - implement robot battles - Python OOP - krnkrnkrn - May-16-2021

In today task I have pretty fun thing to implement, but I have some problems.

Using the robot class - implement robot battles.

Each player has 5 robots. People move the robots alternately, if the robots meet in one field, one takes part of the energy of the other (or all). When the robot's power runs out, a second robot automatically appears.

or implement a fully random battle

What i have so far:
from typing import List, Tuple

class Robot:
    def __init__(self, name: str, place : List[int], start: Tuple[int, int] = (0,0), power: int = 9):
        self._name = name
        self._place = place
        self._start = start
        self._power = power
        
        # further assignments

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if isinstance(value, str):
            self._name = value
        else:
            raise TypeError("must be a string")
    @property
    def place(self):
        return self._place

    @place.setter
    def place(self, value):
        if isinstance(value, list):
            self._start = value
        else:
            raise TypeErorr("must be a list")
        
    @property
    def start(self):
        return self._start

    @start.setter
    def start(self, value):
        if isinstance(value, tuple):
            self._start = value
        else:
            raise TypeErorr("must be a tuple")

    @property
    def power(self):
        return self._power

    @power.setter
    def power(self, value):
        if isinstance(value, int):
            self._start = value
        else:
            raise TypeErorr("must be a int")

    @property
    def check_power(self):
        if self._power <= 0:
            raise ValueError("No power")

    def up(self, value):
    #def left(self, value):
    #def right(self, value):
    #def down(self, value):

    def __str__(self):
        return "{} {} {}".format(self._name, self._place, self._power)
Logic
Quote:You should also take a look at your setters for place and start. You only check the type of the passed argument value, but do not verify if the elements in the list or tuple are of type int.
You could consider using a custom class Position or similiar for handling the positional logic of the robot. That way you can access the different dimensions by their names instead of indices of place (self.place[0] or self.place[1]). You'll also be able to put further logic (like clipping, etc.) into it.
power and check_power(): Depending on your intended functionality you might want to limit the number of steps that can be taken by a robot to the power that it has left. As it is now, a robot can take any number of steps as long as it has power > 0 left.
You should check the functionality left(value) and up(value) for big values, especially values that are > 2 * board_dimension. I suspect the results might be unintended.
I don't really know how to implement it, any help?


RE: Using the robot class - implement robot battles - Python OOP - deanhystad - May-17-2021

What is your question?