Good morning guys!
I've just got done fixing my previous version of this code, and here we are with the "final" version. How would I improve it further?
I wanted to create the board, and the players, but my skillset is severely limited. But I'm trying to improve everyday!
from typing import List, Tuple
from random import randint
class Robot:
def __init__(self, name: str, place : List[int], start: Tuple[int, int] = (0,0), power: int = 100):
self._name = name
self._place = place
self._start = start
self._power = power
@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 TypeError("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 TypeError("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 TypeError("must be a int")
@property
def check_power(self):
if self._power <= 0:
raise ValueError("No power")
def left(self, value):
# self.sprawdz_power()
self.power -= value
if self.place[0] - value < 0:
self.place[0] = self.place[0] - value + 8
else:
self.place[0] = self.place[0] - value
def up(self, value):
# self.sprawdz_power()
self.power -= value
if self.place[1] + value > 7:
self.place[1] = self.place[1] + value - 8
else:
self.place[1] = self.place[1] + value
if self.place[1] == 5:
self.power += 2
def __str__(self):
return "{} {} {}".format(self._name, self._place, self._power)
def check_position():
to_delete = []
for r_a_nr in range(len(robots)):
for r_b_nr in range(r_a_nr + 1, len(robots)):
if robots[r_a_nr].place == robots[r_b_nr].place:
if robots[r_a_nr].power >= robots[r_b_nr].power:
robots[r_a_nr].power = robots[r_a_nr].power + robots[r_b_nr].power
to_delete.append(r_b_nr)
print(f'Robot {robots[r_b_nr].name} has been slain by {robots[r_a_nr].name}')
elif robots[r_a_nr].power < robots[r_b_nr].power:
robots[r_b_nr].power = robots[r_b_nr].power + robots[r_a_nr].power
to_delete.append(r_a_nr)
print(f'Robot {robots[r_a_nr].name} has been slain by {robots[r_b_nr].name}')
for i in sorted(list(set(to_delete)), reverse=True):
del robots[i]
def game(number):
for _ in range(number):
if len(robots) == 1:
print(robots[0].name, "is victorious")
break
to_delete = []
for r_nr in range(len(robots)):
strona_rand = randint(0,1)
ruch_value = randint(0,4)
if strona_rand == 0:
try:
robots[r_nr].up(ruch_value)
except:
to_delete.append(r_nr)
elif strona_rand == 1:
try:
robots[r_nr].left(ruch_value)
except:
to_delete.append(r_nr)
for r_nr in sorted(to_delete, reverse=True):
print(f'Robot {robots[r_nr].name} lost all the power')
del robots[r_nr]
check_position()
robots = [
Robot(name = "A", place = [3,4], start = (0,0), power = 100),
Robot(name = "B", place = [1,2], start = (0,0), power = 100),
Robot(name = "C", place = [1,2], start = (0,0), power = 100),
Robot(name = "D", place = [1,2], start = (0,0), power = 100),
Robot(name = "E", place = [1,2], start = (0,0), power = 100),
]
game(100)
The logic:
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