Python Forum
Using the robot class - implement robot battles - Python OOP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the robot class - implement robot battles - Python OOP
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

    def name(self):
        return self._name

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

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

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

    def power(self):
        return self._power

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

    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)
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 ([0] or[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?
What is your question?

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help with Create Custom class Robot(): TheRealOne 5 1,400 Nov-05-2018, 03:41 AM
Last Post: ichabod801
  Implement Python Class jill1978 1 1,067 Oct-08-2018, 06:17 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020