115 lines
3.2 KiB
Python
115 lines
3.2 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
|
|
import pandas as pd
|
|
|
|
|
|
data = pd.read_csv('dane.csv')
|
|
|
|
|
|
from decisiontree import train_decision_tree
|
|
|
|
|
|
model, feature_columns = train_decision_tree(data)
|
|
|
|
fps = 5
|
|
WIN = pygame.display.set_mode((width, height))
|
|
pygame.display.set_caption('Inteligentny Traktor')
|
|
|
|
def goal_test(elem, board):
|
|
return board.is_dirt(elem.row, elem.col)
|
|
|
|
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]
|
|
|
|
def graphsearch(istate, board):
|
|
explored = Odwiedzone()
|
|
fringe = Queue()
|
|
fringe.put(istate)
|
|
moves = ["up", "left", "right"]
|
|
while not fringe.empty():
|
|
elem = fringe.get()
|
|
if goal_test(elem, board):
|
|
return actions(elem, istate), elem
|
|
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 [], None
|
|
|
|
def fringe_check(fringe, stan):
|
|
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")
|
|
run = True
|
|
clock = pygame.time.Clock()
|
|
board = Board()
|
|
board.load_images()
|
|
tractor = Tractor(4, 4, model, feature_columns)
|
|
|
|
while run:
|
|
clock.tick(fps)
|
|
|
|
if all(not board.is_dirt(row, col) for row in range(rows) for col in range(cols)):
|
|
print("Traktor odwiedził wszystkie pola.")
|
|
break
|
|
|
|
akcje, nowy_stan = graphsearch(initial_state, board)
|
|
|
|
if not akcje:
|
|
print("Nie znaleziono ścieżki do najbliższego pola dirt.")
|
|
board.generate_board()
|
|
initial_state = Stan(4, 4, "down")
|
|
tractor = Tractor(4, 4, model, feature_columns)
|
|
while board.is_rock(initial_state.row, initial_state.col):
|
|
board.generate_board()
|
|
continue
|
|
|
|
print("akcje: >", akcje)
|
|
|
|
while akcje:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
run = False
|
|
|
|
akcja = akcje.pop(0)
|
|
if akcja == "left":
|
|
tractor.turn_left()
|
|
elif akcja == "right":
|
|
tractor.turn_right()
|
|
elif akcja == "up":
|
|
tractor.move_forward(board)
|
|
|
|
board.draw_cubes(WIN)
|
|
tractor.draw(WIN)
|
|
pygame.display.update()
|
|
pygame.time.delay(500)
|
|
|
|
initial_state = nowy_stan
|
|
initial_state.direction = tractor.direction
|
|
|
|
while True:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
pygame.quit()
|
|
return
|
|
|
|
pygame.quit()
|
|
|
|
main()
|