resolve endregion problem

This commit is contained in:
Mateusz Dokowicz 2023-03-30 18:24:41 +02:00
parent 047ef87bab
commit 291e028a8a

View File

@ -9,7 +9,6 @@ from domain.world import World
class RandomCatMoveCommand(Command): class RandomCatMoveCommand(Command):
def __init__(self, world: World, cat: Cat) -> None: def __init__(self, world: World, cat: Cat) -> None:
super().__init__() super().__init__()
self.world = world self.world = world
@ -24,15 +23,17 @@ class RandomCatMoveCommand(Command):
if not cat.busy: if not cat.busy:
while True: while True:
cat.direction = randint(0, 3) cat.direction = randint(0, 3)
if not ((cat.direction == 0 and cat.y == 0) if not (
or (cat.direction == 1 and cat.x == self.world.width - 1) (cat.direction == 0 and cat.y == 0)
or (cat.direction == 2 and cat.y == self.world.height - 1) or (cat.direction == 1 and cat.x == self.world.width - 1)
or (cat.direction == 3 and cat.x == 0)): or (cat.direction == 2 and cat.y == self.world.height - 1)
or (cat.direction == 3 and cat.x == 0)
):
break break
if cat.direction == 0: # up if cat.direction == 0: # up
if cat.busy: if cat.busy:
move_vector = (0, - 1) move_vector = (0, -1)
cat.busy = not cat.busy cat.busy = not cat.busy
if cat.direction == 1: # right if cat.direction == 1: # right
if cat.busy: if cat.busy:
@ -54,10 +55,16 @@ class RandomCatMoveCommand(Command):
end_x = cat.x + move_vector[0] end_x = cat.x + move_vector[0]
end_y = cat.y + move_vector[1] end_y = cat.y + move_vector[1]
if end_x > self.world.width - 1 or end_y > self.world.height - 1 or end_x < 0 or end_y < 0: if (
end_x > self.world.width - 1
or end_y > self.world.height - 1
or end_x < 0
or end_y < 0
):
return return
self.world.obstacles[cat.x][cat.y].remove(cat) self.world.obstacles[cat.x][cat.y].remove(cat)
cat.x = end_x cat.x = end_x
cat.y = end_y cat.y = end_y
self.world.obstacles[end_x][end_y].append(cat) self.world.obstacles[end_x][end_y].append(cat)
# endregion cat random movement