127 lines
3.8 KiB
Python
127 lines
3.8 KiB
Python
#!/usr/bin/python3
|
|
import copy
|
|
from queue import Queue, PriorityQueue
|
|
from threading import Event
|
|
|
|
import pygame
|
|
|
|
from config import *
|
|
from app.cmd_parser import Arg
|
|
from app.graphsearch import Node
|
|
from app.board import Board
|
|
from app.tractor import Tractor
|
|
from app.a_star import AStar
|
|
from app.bfs import Bfs
|
|
|
|
|
|
class App:
|
|
def __init__(self, args: list[Arg]):
|
|
self.__running = True
|
|
self.__screen = None
|
|
self.__clock = None
|
|
self.__board = Board(args=args)
|
|
self.__tractor = Tractor(self.__board)
|
|
self.__bot_is_running = Event()
|
|
self.__moves = None
|
|
|
|
def initialize(self):
|
|
pygame.init()
|
|
pygame.display.set_caption(CAPTION)
|
|
self.__screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
self.__clock = pygame.time.Clock()
|
|
|
|
def event_handler(self, event: pygame.event.Event):
|
|
if event.type == pygame.QUIT:
|
|
self.__running = False
|
|
|
|
def loop_handler(self):
|
|
self.__board.draw(self.__screen)
|
|
self.__tractor.draw(self.__screen)
|
|
|
|
def keys_pressed_handler(self):
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_w]:
|
|
self.__tractor.move()
|
|
self.__tractor.choose_action()
|
|
print(self.__tractor)
|
|
|
|
if keys[pygame.K_n]:
|
|
self.__bot_is_running.set()
|
|
self.__tractor.harvest_checked_fields_handler(self.__bot_is_running)
|
|
|
|
if keys[pygame.K_h]:
|
|
self.__tractor.harvest()
|
|
|
|
if keys[pygame.K_v]:
|
|
self.__tractor.sow()
|
|
|
|
if keys[pygame.K_j]:
|
|
self.__tractor.hydrate()
|
|
|
|
if keys[pygame.K_f]:
|
|
self.__tractor.fertilize()
|
|
|
|
if keys[pygame.K_l]:
|
|
self.__tractor.rotate_left()
|
|
|
|
if keys[pygame.K_r]:
|
|
self.__tractor.rotate_right()
|
|
|
|
if keys[pygame.K_b]:
|
|
self.get_moves_by_bfs()
|
|
if not self.__moves:
|
|
print(f"Bfs is failed")
|
|
else:
|
|
print(f"Bfs is succeed")
|
|
self.__bot_is_running.set()
|
|
self.__tractor.run_bot_handler(self.__moves, self.__bot_is_running)
|
|
|
|
if keys[pygame.K_c]:
|
|
self.get_moves_by_a_star()
|
|
if not self.__moves:
|
|
print(f"A Star is failed")
|
|
else:
|
|
print(f"A Star is succeed")
|
|
self.__bot_is_running.set()
|
|
self.__tractor.run_bot_handler(self.__moves, self.__bot_is_running)
|
|
|
|
def update_screen(self) -> None:
|
|
pygame.display.flip()
|
|
|
|
def quit(self) -> None:
|
|
pygame.quit()
|
|
|
|
def get_moves_by_a_star(self) -> None:
|
|
x, y = self.__tractor.get_position()
|
|
node = Node(None, x, y, self.__tractor.get_direction(), 0, "movement", "initial state")
|
|
board = copy.deepcopy(self.__board)
|
|
self.__moves = AStar.search(PriorityQueue(), Queue(), node,
|
|
lambda n=node, b=board: AStar.succ(n, b),
|
|
lambda n=node: AStar.goaltest(n), board)
|
|
|
|
def get_moves_by_bfs(self) -> None:
|
|
x, y = self.__tractor.get_position()
|
|
node = Node(None, x, y, self.__tractor.get_direction(), 0, "movement", "initial state")
|
|
board = copy.deepcopy(self.__board)
|
|
self.__moves = Bfs.search(Queue(), Queue(), node,
|
|
lambda n=node, b=board: Bfs.succ(n, b),
|
|
lambda n=node: Bfs.goaltest(n), board)
|
|
|
|
def run(self) -> None:
|
|
self.initialize()
|
|
|
|
while self.__running:
|
|
self.__clock.tick(FPS)
|
|
for event in pygame.event.get():
|
|
self.event_handler(event)
|
|
|
|
if not self.__bot_is_running.is_set():
|
|
self.keys_pressed_handler()
|
|
|
|
self.loop_handler()
|
|
self.update_screen()
|
|
|
|
self.quit()
|