2024-03-10 02:46:14 +01:00
|
|
|
import pygame
|
|
|
|
from board import Board
|
|
|
|
from constant import width, height, rows, cols
|
|
|
|
from tractor import Tractor
|
2024-04-14 14:37:32 +02:00
|
|
|
from kolejka import Stan, Kolejka, Odwiedzone
|
2024-03-10 02:46:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
fps = 5
|
|
|
|
WIN = pygame.display.set_mode((width, height))
|
|
|
|
|
|
|
|
pygame.display.set_caption('Inteligenty Traktor')
|
|
|
|
|
2024-04-14 14:37:32 +02:00
|
|
|
def goal_test(elem, goaltest, board):
|
|
|
|
if board.is_rock(goaltest.col, goaltest.row):
|
|
|
|
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
|
2024-04-27 00:39:38 +02:00
|
|
|
|
2024-04-14 14:37:32 +02:00
|
|
|
def graphsearch(istate, goaltest, board):
|
|
|
|
explored = Odwiedzone()
|
|
|
|
fringe = Kolejka()
|
|
|
|
fringe.dodaj_stan(istate)
|
|
|
|
moves = ["up", "left", "right"]
|
|
|
|
while not fringe.czy_pusta():
|
|
|
|
elem = fringe.usun_stan()
|
|
|
|
if goal_test(elem, goaltest, board):
|
|
|
|
return actions(elem, istate)
|
|
|
|
explored.dodaj_stan(elem)
|
|
|
|
for action in moves:
|
|
|
|
stan = elem.succ(action, board)
|
|
|
|
if((not fringe.check(stan)) and (not explored.check(stan))):
|
|
|
|
stan.parrent(elem, action)
|
|
|
|
fringe.dodaj_stan(stan)
|
|
|
|
return "Brak sciezki"
|
|
|
|
|
2024-03-10 02:46:14 +01:00
|
|
|
def main():
|
2024-04-14 14:37:32 +02:00
|
|
|
rotation = ["left", "up", "right", "down"]
|
|
|
|
istate = Stan(4,4, "down")
|
2024-04-27 00:39:38 +02:00
|
|
|
goaltest = Stan(2,3, "up")
|
2024-03-10 02:46:14 +01:00
|
|
|
run = True
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
board = Board()
|
|
|
|
board.load_images()
|
2024-04-14 14:37:32 +02:00
|
|
|
actions = graphsearch(istate, goaltest, board)
|
|
|
|
print("akcje: >",actions )
|
2024-04-27 00:39:38 +02:00
|
|
|
tractor = Tractor(2, 3)
|
2024-03-10 02:46:14 +01:00
|
|
|
while run:
|
|
|
|
clock.tick(fps)
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
2024-03-24 19:49:08 +01:00
|
|
|
|
2024-04-14 14:37:32 +02:00
|
|
|
if keys[pygame.K_UP]:
|
2024-04-27 00:39:38 +02:00
|
|
|
if keys[pygame.K_UP]:
|
2024-04-14 14:37:32 +02:00
|
|
|
if(tractor.direction == "up" and tractor.row > 0 ):
|
|
|
|
if board.is_weed(tractor.col, tractor.row - 1):
|
|
|
|
board.set_grass(tractor.col, tractor.row - 1)
|
|
|
|
tractor.row -= 1
|
|
|
|
elif board.is_dirt(tractor.col, tractor.row - 1):
|
|
|
|
board.set_soil(tractor.col, tractor.row - 1)
|
|
|
|
tractor.row -= 1
|
2024-04-27 00:39:38 +02:00
|
|
|
elif board.is_soil(tractor.col, tractor.row - 1):
|
|
|
|
board.set_carrot(tractor.col, tractor.row - 1)
|
|
|
|
tractor.row -= 1
|
2024-04-14 14:37:32 +02:00
|
|
|
elif not board.is_rock(tractor.col, tractor.row - 1):
|
|
|
|
tractor.row -= 1
|
2024-04-27 00:39:38 +02:00
|
|
|
|
2024-04-14 14:37:32 +02:00
|
|
|
if(tractor.direction == "left" and tractor.col > 0):
|
|
|
|
if board.is_weed(tractor.col - 1, tractor.row):
|
|
|
|
board.set_grass(tractor.col - 1, tractor.row)
|
|
|
|
tractor.col -= 1
|
|
|
|
elif board.is_dirt(tractor.col - 1, tractor.row):
|
|
|
|
board.set_soil(tractor.col - 1, tractor.row)
|
|
|
|
tractor.col -= 1
|
2024-04-27 00:39:38 +02:00
|
|
|
elif board.is_soil(tractor.col - 1, tractor.row):
|
|
|
|
board.set_carrot(tractor.col - 1, tractor.row)
|
|
|
|
tractor.col -= 1
|
2024-04-14 14:37:32 +02:00
|
|
|
elif not board.is_rock(tractor.col - 1, tractor.row):
|
|
|
|
tractor.col -= 1
|
|
|
|
if(tractor.direction == "down" and tractor.row < rows - 1):
|
|
|
|
if board.is_weed(tractor.col, tractor.row + 1):
|
|
|
|
board.set_grass(tractor.col, tractor.row + 1)
|
|
|
|
tractor.row += 1
|
|
|
|
elif board.is_dirt(tractor.col, tractor.row + 1):
|
|
|
|
board.set_soil(tractor.col, tractor.row + 1)
|
|
|
|
tractor.row += 1
|
2024-04-27 00:39:38 +02:00
|
|
|
elif board.is_soil(tractor.col, tractor.row + 1):
|
|
|
|
board.set_carrot(tractor.col, tractor.row + 1)
|
|
|
|
tractor.row += 1
|
2024-04-14 14:37:32 +02:00
|
|
|
elif not board.is_rock(tractor.col, tractor.row + 1):
|
|
|
|
tractor.row += 1
|
|
|
|
if(tractor.direction == "right" and tractor.col < cols - 1):
|
|
|
|
if board.is_weed(tractor.col + 1, tractor.row):
|
|
|
|
board.set_grass(tractor.col + 1, tractor.row)
|
|
|
|
tractor.col += 1
|
|
|
|
elif board.is_dirt(tractor.col + 1, tractor.row):
|
|
|
|
board.set_soil(tractor.col + 1, tractor.row)
|
|
|
|
tractor.col += 1
|
2024-04-27 00:39:38 +02:00
|
|
|
elif board.is_soil(tractor.col + 1, tractor.row ):
|
|
|
|
board.set_carrot(tractor.col + 1, tractor.row )
|
|
|
|
tractor.col += 1
|
2024-04-14 14:37:32 +02:00
|
|
|
elif not board.is_rock(tractor.col + 1, tractor.row):
|
|
|
|
tractor.col += 1
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
|
|
for i in range(0, 4):
|
|
|
|
if(tractor.direction == rotation[i]):
|
|
|
|
if(i == 0):
|
|
|
|
i = 4
|
|
|
|
tractor.direction = rotation[i-1]
|
|
|
|
break
|
|
|
|
if keys[pygame.K_RIGHT]:
|
|
|
|
for i in range(0, 4):
|
|
|
|
if(tractor.direction == rotation[i]):
|
|
|
|
if(i == 3):
|
|
|
|
i = -1
|
|
|
|
tractor.direction = rotation[i+1]
|
|
|
|
break
|
2024-03-10 02:46:14 +01:00
|
|
|
|
|
|
|
board.draw_cubes(WIN)
|
|
|
|
tractor.draw(WIN)
|
|
|
|
pygame.display.update()
|
|
|
|
pygame.quit()
|
|
|
|
|
2024-04-14 14:37:32 +02:00
|
|
|
main()
|
|
|
|
|