2021-03-16 10:06:56 +01:00
|
|
|
#!/usr/bin/python3
|
2021-04-13 09:55:19 +02:00
|
|
|
import copy
|
|
|
|
from queue import Queue
|
2021-03-16 10:06:56 +01:00
|
|
|
|
|
|
|
import pygame
|
|
|
|
|
|
|
|
from config import *
|
2021-04-13 09:55:19 +02:00
|
|
|
from app.graphsearch import Node, Graphsearch
|
2021-03-16 10:06:56 +01:00
|
|
|
from app.board import Board
|
2021-03-30 11:24:50 +02:00
|
|
|
from app.tractor import Tractor
|
2021-03-16 10:06:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
class App:
|
|
|
|
def __init__(self):
|
|
|
|
self.__running = True
|
|
|
|
self.__screen = None
|
|
|
|
self.__clock = None
|
|
|
|
self.__board = Board()
|
2021-03-30 11:24:50 +02:00
|
|
|
self.__tractor = Tractor(self.__board)
|
2021-04-13 09:55:19 +02:00
|
|
|
self.__bot_is_running = False
|
|
|
|
self.__moves = None
|
2021-03-16 10:06:56 +01:00
|
|
|
|
|
|
|
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)
|
2021-03-30 11:24:50 +02:00
|
|
|
self.__tractor.draw(self.__screen)
|
2021-03-16 10:06:56 +01:00
|
|
|
|
|
|
|
def keys_pressed_handler(self):
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
|
|
|
|
if keys[pygame.K_UP]:
|
2021-03-30 11:24:50 +02:00
|
|
|
self.__tractor.move_up()
|
|
|
|
print(self.__tractor)
|
2021-03-16 10:06:56 +01:00
|
|
|
if keys[pygame.K_DOWN]:
|
2021-03-30 11:24:50 +02:00
|
|
|
self.__tractor.move_down()
|
|
|
|
print(self.__tractor)
|
2021-03-16 10:06:56 +01:00
|
|
|
if keys[pygame.K_LEFT]:
|
2021-03-30 11:24:50 +02:00
|
|
|
self.__tractor.move_left()
|
|
|
|
print(self.__tractor)
|
2021-03-16 10:06:56 +01:00
|
|
|
if keys[pygame.K_RIGHT]:
|
2021-03-30 11:24:50 +02:00
|
|
|
self.__tractor.move_right()
|
|
|
|
print(self.__tractor)
|
|
|
|
|
2021-04-11 19:48:44 +02:00
|
|
|
if keys[pygame.K_w]:
|
|
|
|
self.__tractor.move()
|
|
|
|
print(self.__tractor)
|
|
|
|
|
2021-03-30 11:24:50 +02:00
|
|
|
if keys[pygame.K_h]:
|
|
|
|
self.__tractor.harvest()
|
|
|
|
|
|
|
|
if keys[pygame.K_v]:
|
|
|
|
self.__tractor.sow()
|
|
|
|
|
|
|
|
if keys[pygame.K_n]:
|
|
|
|
self.__tractor.hydrate()
|
|
|
|
|
|
|
|
if keys[pygame.K_f]:
|
|
|
|
self.__tractor.fertilize()
|
|
|
|
|
2021-04-11 19:48:44 +02:00
|
|
|
if keys[pygame.K_l]:
|
|
|
|
self.__tractor.rotate_left()
|
|
|
|
|
|
|
|
if keys[pygame.K_r]:
|
|
|
|
self.__tractor.rotate_right()
|
|
|
|
|
2021-04-13 09:55:19 +02:00
|
|
|
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 = True
|
|
|
|
self.__tractor.move_by_bfs_handler(self.__moves)
|
|
|
|
|
2021-03-16 10:06:56 +01:00
|
|
|
|
|
|
|
def update_screen(self):
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
pygame.quit()
|
|
|
|
|
2021-04-13 09:55:19 +02:00
|
|
|
def get_moves_by_bfs(self):
|
|
|
|
x, y = self.__tractor.get_position()
|
|
|
|
node = Node(None, x, y, self.__tractor.get_direction(), 0, "initial state", "movement")
|
|
|
|
board = copy.deepcopy(self.__board)
|
|
|
|
self.__moves = Graphsearch.bfs(Queue(), Queue(), node,
|
|
|
|
lambda n=node, b=board: Graphsearch.succ(n, b),
|
|
|
|
lambda n=node: Graphsearch.goaltest(n), board)
|
|
|
|
|
2021-03-16 10:06:56 +01:00
|
|
|
def run(self):
|
|
|
|
self.initialize()
|
|
|
|
|
|
|
|
while self.__running:
|
|
|
|
self.__clock.tick(FPS)
|
|
|
|
for event in pygame.event.get():
|
|
|
|
self.event_handler(event)
|
|
|
|
|
2021-04-13 09:55:19 +02:00
|
|
|
if not self.__bot_is_running:
|
|
|
|
self.keys_pressed_handler()
|
2021-03-16 10:06:56 +01:00
|
|
|
|
|
|
|
self.loop_handler()
|
|
|
|
self.update_screen()
|
|
|
|
|
|
|
|
self.quit()
|