refactor code
This commit is contained in:
commit
c4a1c40f4a
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import copy
|
import copy
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
from threading import Event
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ class App:
|
|||||||
self.__clock = None
|
self.__clock = None
|
||||||
self.__board = Board()
|
self.__board = Board()
|
||||||
self.__tractor = Tractor(self.__board)
|
self.__tractor = Tractor(self.__board)
|
||||||
self.__bot_is_running = False
|
self.__bot_is_running = Event()
|
||||||
self.__moves = None
|
self.__moves = None
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
@ -39,16 +40,16 @@ class App:
|
|||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
if keys[pygame.K_UP]:
|
if keys[pygame.K_UP]:
|
||||||
self.__tractor.move_up()
|
self.__tractor.direction_up()
|
||||||
print(self.__tractor)
|
|
||||||
if keys[pygame.K_DOWN]:
|
if keys[pygame.K_DOWN]:
|
||||||
self.__tractor.move_down()
|
self.__tractor.direction_down()
|
||||||
print(self.__tractor)
|
|
||||||
if keys[pygame.K_LEFT]:
|
if keys[pygame.K_LEFT]:
|
||||||
self.__tractor.move_left()
|
self.__tractor.direction_left()
|
||||||
print(self.__tractor)
|
|
||||||
if keys[pygame.K_RIGHT]:
|
if keys[pygame.K_RIGHT]:
|
||||||
self.__tractor.move_right()
|
self.__tractor.direction_right()
|
||||||
|
|
||||||
|
if keys[pygame.K_m]:
|
||||||
|
self.__tractor.move()
|
||||||
print(self.__tractor)
|
print(self.__tractor)
|
||||||
|
|
||||||
if keys[pygame.K_w]:
|
if keys[pygame.K_w]:
|
||||||
@ -79,8 +80,8 @@ class App:
|
|||||||
print(f"Bfs is failed")
|
print(f"Bfs is failed")
|
||||||
else:
|
else:
|
||||||
print(f"Bfs is succeed")
|
print(f"Bfs is succeed")
|
||||||
self.__bot_is_running = True
|
self.__bot_is_running.set()
|
||||||
self.__tractor.move_by_bfs_handler(self.__moves)
|
self.__tractor.move_by_bfs_handler(self.__moves, self.__bot_is_running)
|
||||||
|
|
||||||
|
|
||||||
def update_screen(self):
|
def update_screen(self):
|
||||||
@ -91,7 +92,7 @@ class App:
|
|||||||
|
|
||||||
def get_moves_by_bfs(self):
|
def get_moves_by_bfs(self):
|
||||||
x, y = self.__tractor.get_position()
|
x, y = self.__tractor.get_position()
|
||||||
node = Node(None, x, y, self.__tractor.get_direction(), 0, "initial state", "movement")
|
node = Node(None, x, y, self.__tractor.get_direction(), 0, "movement", "initial state")
|
||||||
board = copy.deepcopy(self.__board)
|
board = copy.deepcopy(self.__board)
|
||||||
self.__moves = Graphsearch.bfs(Queue(), Queue(), node,
|
self.__moves = Graphsearch.bfs(Queue(), Queue(), node,
|
||||||
lambda n=node, b=board: Graphsearch.succ(n, b),
|
lambda n=node, b=board: Graphsearch.succ(n, b),
|
||||||
@ -105,7 +106,7 @@ class App:
|
|||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
self.event_handler(event)
|
self.event_handler(event)
|
||||||
|
|
||||||
if not self.__bot_is_running:
|
if not self.__bot_is_running.is_set():
|
||||||
self.keys_pressed_handler()
|
self.keys_pressed_handler()
|
||||||
|
|
||||||
self.loop_handler()
|
self.loop_handler()
|
||||||
|
@ -12,6 +12,12 @@ class BaseField:
|
|||||||
img = pygame.image.load(self._img_path)
|
img = pygame.image.load(self._img_path)
|
||||||
img = pygame.transform.rotate(img, angle)
|
img = pygame.transform.rotate(img, angle)
|
||||||
|
|
||||||
|
# def draw_field(self, screen: pygame.Surface, pos_x: int, pos_y: int, is_centered: bool = False, size: tuple = None,
|
||||||
|
# angle: int = 0):
|
||||||
|
# pre_img = pygame.image.load(self._img_path)
|
||||||
|
# if angle == 90:
|
||||||
|
# pre_img = pygame.transform.flip(pre_img, True, False)
|
||||||
|
# img = pygame.transform.rotate(pre_img, angle)
|
||||||
scale = pygame.transform.scale(img, (FIELD_SIZE, FIELD_SIZE))
|
scale = pygame.transform.scale(img, (FIELD_SIZE, FIELD_SIZE))
|
||||||
rect = img.get_rect()
|
rect = img.get_rect()
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ class Tractor(BaseField):
|
|||||||
self.__move = FIELD_SIZE
|
self.__move = FIELD_SIZE
|
||||||
self.__board = board
|
self.__board = board
|
||||||
self.__harvested_corps = []
|
self.__harvested_corps = []
|
||||||
|
self.__fuel = 10
|
||||||
|
|
||||||
def draw(self, screen: pygame.Surface):
|
def draw(self, screen: pygame.Surface):
|
||||||
self.draw_field(screen, self.__pos_x + FIELD_SIZE / 2, self.__pos_y + FIELD_SIZE / 2,
|
self.draw_field(screen, self.__pos_x + FIELD_SIZE / 2, self.__pos_y + FIELD_SIZE / 2,
|
||||||
@ -35,11 +36,11 @@ class Tractor(BaseField):
|
|||||||
|
|
||||||
# Key methods handlers
|
# Key methods handlers
|
||||||
def move(self):
|
def move(self):
|
||||||
if self.__direction == 0.0:
|
if self.__direction == D_EAST:
|
||||||
self.move_right()
|
self.move_right()
|
||||||
elif self.__direction == 90.0:
|
elif self.__direction == D_NORTH:
|
||||||
self.move_up()
|
self.move_up()
|
||||||
elif self.__direction == 180.0:
|
elif self.__direction == D_WEST:
|
||||||
self.move_left()
|
self.move_left()
|
||||||
else:
|
else:
|
||||||
self.move_down()
|
self.move_down()
|
||||||
@ -60,6 +61,41 @@ class Tractor(BaseField):
|
|||||||
if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
|
if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
|
||||||
self.__pos_x += self.__move
|
self.__pos_x += self.__move
|
||||||
|
|
||||||
|
def direction_up(self):
|
||||||
|
self.__direction = 0
|
||||||
|
|
||||||
|
def direction_right(self):
|
||||||
|
self.__direction = 270
|
||||||
|
|
||||||
|
def direction_down(self):
|
||||||
|
self.__direction = 180
|
||||||
|
|
||||||
|
def direction_left(self):
|
||||||
|
self.__direction = 90
|
||||||
|
|
||||||
|
# def move(self):
|
||||||
|
# if self.__fuel > 0:
|
||||||
|
# self.__fuel -= 1
|
||||||
|
#
|
||||||
|
# if self.__direction == 0:
|
||||||
|
# if self.__pos_y - self.__move >= 0:
|
||||||
|
# self.__pos_y -= self.__move
|
||||||
|
#
|
||||||
|
# elif self.__direction == 270:
|
||||||
|
# if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
|
||||||
|
# self.__pos_x += self.__move
|
||||||
|
#
|
||||||
|
# elif self.__direction == 180:
|
||||||
|
# if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT:
|
||||||
|
# self.__pos_y += self.__move
|
||||||
|
#
|
||||||
|
# elif self.__direction == 90:
|
||||||
|
# if self.__pos_x - self.__move >= 0:
|
||||||
|
# self.__pos_x -= self.__move
|
||||||
|
|
||||||
|
# else:
|
||||||
|
# print("Run out of fuel!")
|
||||||
|
|
||||||
def rotate_left(self):
|
def rotate_left(self):
|
||||||
self.__direction = (self.__direction - 90.0) % 360.0
|
self.__direction = (self.__direction - 90.0) % 360.0
|
||||||
|
|
||||||
@ -91,6 +127,7 @@ class Tractor(BaseField):
|
|||||||
if not field.is_hydrated:
|
if not field.is_hydrated:
|
||||||
print("Hydrate plant")
|
print("Hydrate plant")
|
||||||
self.irrigate_plants(field)
|
self.irrigate_plants(field)
|
||||||
|
|
||||||
def sow(self):
|
def sow(self):
|
||||||
field = self.get_field_from_board()
|
field = self.get_field_from_board()
|
||||||
if self.check_field(Sand) and not field.is_sowed:
|
if self.check_field(Sand) and not field.is_sowed:
|
||||||
@ -178,11 +215,11 @@ class Tractor(BaseField):
|
|||||||
def get_direction(self):
|
def get_direction(self):
|
||||||
return self.__direction
|
return self.__direction
|
||||||
|
|
||||||
def move_by_bfs_handler(self, moves: list[tuple[str, str]]):
|
def move_by_bfs_handler(self, moves: list[tuple[str, str]], is_running: threading.Event):
|
||||||
thread = threading.Thread(target=self.move_by_bfs, args=(moves,))
|
thread = threading.Thread(target=self.move_by_bfs, args=(moves, is_running), daemon=True)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def move_by_bfs(self, moves: list[tuple[str, str]]):
|
def move_by_bfs(self, moves: list[tuple[str, str]], is_running: threading.Event):
|
||||||
print(moves)
|
print(moves)
|
||||||
print(f"Length of Moves {len(moves)} - {3 ** len(moves)}")
|
print(f"Length of Moves {len(moves)} - {3 ** len(moves)}")
|
||||||
while len(moves) > 0:
|
while len(moves) > 0:
|
||||||
@ -211,6 +248,7 @@ class Tractor(BaseField):
|
|||||||
self.rotate_right()
|
self.rotate_right()
|
||||||
|
|
||||||
time.sleep(TIME_OF_MOVING)
|
time.sleep(TIME_OF_MOVING)
|
||||||
|
is_running.clear()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def move_is_correct(x: int, y: int, direction: float) -> Union[(int, int), None]:
|
def move_is_correct(x: int, y: int, direction: float) -> Union[(int, int), None]:
|
||||||
@ -259,3 +297,4 @@ class Tractor(BaseField):
|
|||||||
obj = get_class("app.fields", choosen_type)
|
obj = get_class("app.fields", choosen_type)
|
||||||
board.get_fields()[x][y] = obj()
|
board.get_fields()[x][y] = obj()
|
||||||
return obj()
|
return obj()
|
||||||
|
# return f"Position: {x}:{y} - {type(self.__board.get_fields()[x][y]).__name__}\nFuel: {self.__fuel}\n"
|
||||||
|
@ -44,10 +44,10 @@ SUNFLOWER = 'sunflower'
|
|||||||
FIELD_TYPES = (SAND, CLAY, GRASS, CORN, SUNFLOWER)
|
FIELD_TYPES = (SAND, CLAY, GRASS, CORN, SUNFLOWER)
|
||||||
|
|
||||||
# Directions
|
# Directions
|
||||||
D_NORTH = 90.0
|
D_NORTH = 0.0
|
||||||
D_EAST = 0.0
|
D_EAST = 270.0
|
||||||
D_SOUTH = 270.0
|
D_SOUTH = 180.0
|
||||||
D_WEST = 180.0
|
D_WEST = 90.0
|
||||||
|
|
||||||
# Goal Test
|
# Goal Test
|
||||||
AMOUNT_OF_CROPS = 5
|
AMOUNT_OF_CROPS = 5
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 516 B After Width: | Height: | Size: 1.4 KiB |
Loading…
Reference in New Issue
Block a user