Type annotations
Using type annotations will make your program a lot more readable and easier to debug. For example:
def __init__(self, name, place, start = (0,0), power = 9):
# assignments
becomes
def __init__(self, name: str, place : List[int], start: Tuple[int, int] = (0,0), power: int = 9):
# assignments
I imported List, Tuple from the typing module from the standard library:
from typing import List, Tuple
Here's some further information on using type annotations.
Getter and Setter
Using getters and setters is rather unpythonic. You can instead modify the getting and setting behaviours by using the @property decorator like this:
def __init__(self, name: str, place : List[int], start: Tuple[int, int] = (0,0), power: int = 9):
self._name = name
# 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")
This will allow you to use the properties without explicitly calling getter and setter methods:
my_robot.name = 'foo'
print(my_robot.name)
This question on StackOverflow contains some more detailed explanations.
Logic
- You should also take a look at your setters for
placeandstart. You only check the type of the passed argumentvalue, but do not verify if the elements in thelistortupleare of typeint. - You could consider using a custom class
Positionor similiar for handling the positional logic of the robot. That way you can access the different dimensions by their names instead of indices ofplace(self.place[0]orself.place[1]). You'll also be able to put further logic (like clipping, etc.) into it. powerandcheck_power(): Depending on your intended functionality you might want to limit the number of steps that can be taken by a robot to thepowerthat it has left. As it is now, a robot can take any number of steps as long as it haspower > 0left.- You should check the functionality
left(value)andup(value)for big values, especially values that are> 2 * board_dimension. I suspect the results might be unintended.