BFS added

This commit is contained in:
Pmik13 2024-04-14 14:37:32 +02:00
parent ceaaa4ca82
commit 1315b33894
2 changed files with 178 additions and 56 deletions

88
kolejka.py Normal file
View File

@ -0,0 +1,88 @@
from constant import width, height, rows, cols
class Stan:
def __init__(self, row, col, dir):
self.p = []
self.a = ""
self.row = row
self.col = col
self.direction = dir
def __str__(self):
return f"row: {self.row}, col: {self.col}, direction: {self.direction}"
def parrent(self, stan, action):
if(len(self.p) == 0):
self.p.append(stan)
self.a = action
def succ(self, action, board):
if(action == "up"):
if(self.direction == "left"):
if not board.is_rock(max(self.col - 1, 0), self.row):
return Stan( self.row , max(self.col - 1, 0), self.direction)
return Stan(self.row, self.col, self.direction)
if(self.direction == "up"):
if not board.is_rock(self.col, max(self.row - 1, 0)):
return Stan(max(self.row - 1,0) , self.col, self.direction)
return Stan(self.row, self.col, self.direction)
if(self.direction == "right"):
if not board.is_rock(min(self.col + 1, cols - 1), self.row):
return Stan(self.row, min(self.col + 1, cols - 1) , self.direction)
return Stan(self.row, self.col, self.direction)
if(self.direction == "down"):
if not board.is_rock(self.col, min(self.row + 1,rows - 1 )):
return Stan(min(self.row + 1,rows - 1 ) , self.col, self.direction)
return Stan(self.row, self.col, self.direction)
if(action == "left"):
if(self.direction == "left"):
return Stan(self.row , self.col, "down")
if(self.direction == "up"):
return Stan(self.row, self.col, "left")
if(self.direction == "right"):
return Stan(self.row, self.col, "up")
if(self.direction == "down"):
return Stan(self.row, self.col , "right")
if(action == "right"):
if(self.direction == "left"):
return Stan(self.row, self.col, "up")
if(self.direction == "up"):
return Stan(self.row, self.col, "right")
if(self.direction == "right"):
return Stan(self.row, self.col, "down")
if(self.direction == "down"):
return Stan(self.row, self.col,"left")
class Kolejka:
def __init__(self):
self.id = 0
self.len = 0
self.stany = []
def dodaj_stan(self, stan):
self.stany.append(stan)
self.len += 1
def usun_stan(self):
if not self.czy_pusta():
self.id += 1
return self.stany[self.id - 1]
else:
raise IndexError("Kolejka stanów jest pusta")
def czy_pusta(self):
return self.len <= self.id
def check(self, stan):
indeks = 0
for i in self.stany:
if(stan.direction == i.direction and stan.col == i.col and stan.row == i.row):
return True
return False
class Odwiedzone:
def __init__(self):
self.stany = []
def check(self, stan):
for i in self.stany:
if(stan.direction == i.direction and stan.col == i.col and stan.row == i.row):
return True
return False
def dodaj_stan(self, stan):
self.stany.append(stan)

144
main.py
View File

@ -2,6 +2,7 @@ import pygame
from board import Board
from constant import width, height, rows, cols
from tractor import Tractor
from kolejka import Stan, Kolejka, Odwiedzone
@ -11,11 +12,47 @@ WIN = pygame.display.set_mode((width, height))
pygame.display.set_caption('Inteligenty Traktor')
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
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"
def main():
rotation = ["left", "up", "right", "down"]
istate = Stan(4,4, "down")
goaltest = Stan(1,1, "up")
run = True
clock = pygame.time.Clock()
board = Board()
board.load_images()
actions = graphsearch(istate, goaltest, board)
print("akcje: >",actions )
tractor = Tractor(4, 4)
while run:
clock.tick(fps)
@ -26,61 +63,57 @@ def main():
keys = pygame.key.get_pressed()
if keys[pygame.K_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
tractor.direction = "up"
elif board.is_dirt(tractor.col, tractor.row - 1):
board.set_soil(tractor.col, tractor.row - 1)
tractor.row -= 1
tractor.direction = "up"
elif not board.is_rock(tractor.col, tractor.row - 1):
tractor.row -= 1
tractor.direction = "up"
if keys[pygame.K_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
tractor.direction = "down"
elif board.is_dirt(tractor.col, tractor.row + 1):
board.set_soil(tractor.col, tractor.row + 1)
tractor.row += 1
tractor.direction = "down"
elif not board.is_rock(tractor.col, tractor.row + 1):
tractor.row += 1
tractor.direction = "down"
if keys[pygame.K_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
tractor.direction = "left"
elif board.is_dirt(tractor.col - 1, tractor.row):
board.set_soil(tractor.col - 1, tractor.row)
tractor.col -= 1
tractor.direction = "left"
elif not board.is_rock(tractor.col - 1, tractor.row):
tractor.col -= 1
tractor.direction = "left"
if keys[pygame.K_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
tractor.direction = "right"
elif board.is_dirt(tractor.col + 1, tractor.row):
board.set_soil(tractor.col + 1, tractor.row)
tractor.col += 1
tractor.direction = "right"
elif not board.is_rock(tractor.col + 1, tractor.row):
tractor.col += 1
tractor.direction = "right"
if keys[pygame.K_UP]:
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
elif not board.is_rock(tractor.col, tractor.row - 1):
tractor.row -= 1
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
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
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
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
board.draw_cubes(WIN)
tractor.draw(WIN)
@ -88,3 +121,4 @@ def main():
pygame.quit()
main()