Traktor/main.py
2024-05-18 17:10:56 +02:00

89 lines
2.5 KiB
Python

import pygame
from board import Board
from constant import width, height, rows, cols
from tractor import Tractor
from kolejka import Stan, Odwiedzone
from queue import Queue
fps = 5
WIN = pygame.display.set_mode((width, height))
pygame.display.set_caption('Inteligenty Traktor')
def goal_test(elem, goaltest, board):
if board.is_rock(goaltest.row, goaltest.col):
return True
if((elem.row == goaltest.row) and (elem.col == goaltest.col)):
return True
else:
return False
def actions(elem, istate):
akcje = []
while (elem.row != istate.row) or (elem.col != istate.col) or (elem.direction != istate.direction):
akcje.append(elem.a)
elem = elem.p[0]
return akcje[::-1] # Odwracamy kolejność, aby akcje były w poprawnej sekwencji
def graphsearch(istate, goaltest, board):
explored = Odwiedzone()
fringe = Queue()
fringe.put(istate)
moves = ["up", "left", "right"]
while not fringe.empty():
elem = fringe.get()
if goal_test(elem, goaltest, board):
return actions(elem, istate)
explored.dodaj_stan(elem)
for action in moves:
stan = elem.succ(action, board)
if stan is not None:
if((not fringe_check(fringe,stan)) and (not explored.check(stan))):
stan.parrent(elem, action)
fringe.put(stan)
return "Brak sciezki"
def fringe_check(fringe, stan): # Funkcja pomocnicza do sprawdzania stanu w kolejce
with fringe.mutex:
for item in fringe.queue:
if stan.direction == item.direction and stan.col == item.col and stan.row == item.row:
return True
return False
def main():
initial_state = Stan(4,4, "down")
goaltest = Stan(1,1, "up")
run = True
clock = pygame.time.Clock()
board = Board()
board.load_images()
akcje = graphsearch(initial_state, goaltest, board)
print("akcje: >",akcje )
tractor = Tractor(4, 4)
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if akcje:
action = akcje.pop(0) # Pobierz kolejną akcję z listy
if action == "left":
tractor.turn_left()
elif action == "right":
tractor.turn_right()
elif action == "up":
tractor.move_forward(board)
board.draw_cubes(WIN)
tractor.draw(WIN)
pygame.display.update()
pygame.quit()
main()