BFS added
This commit is contained in:
parent
ceaaa4ca82
commit
1315b33894
88
kolejka.py
Normal file
88
kolejka.py
Normal 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)
|
146
main.py
146
main.py
@ -2,6 +2,7 @@ import pygame
|
|||||||
from board import Board
|
from board import Board
|
||||||
from constant import width, height, rows, cols
|
from constant import width, height, rows, cols
|
||||||
from tractor import Tractor
|
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')
|
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():
|
def main():
|
||||||
|
rotation = ["left", "up", "right", "down"]
|
||||||
|
istate = Stan(4,4, "down")
|
||||||
|
goaltest = Stan(1,1, "up")
|
||||||
run = True
|
run = True
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
board = Board()
|
board = Board()
|
||||||
board.load_images()
|
board.load_images()
|
||||||
|
actions = graphsearch(istate, goaltest, board)
|
||||||
|
print("akcje: >",actions )
|
||||||
tractor = Tractor(4, 4)
|
tractor = Tractor(4, 4)
|
||||||
while run:
|
while run:
|
||||||
clock.tick(fps)
|
clock.tick(fps)
|
||||||
@ -26,65 +63,62 @@ def main():
|
|||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
if keys[pygame.K_UP] and tractor.row > 0 :
|
if keys[pygame.K_UP]:
|
||||||
if board.is_weed(tractor.col, tractor.row - 1):
|
if(tractor.direction == "up" and tractor.row > 0 ):
|
||||||
board.set_grass(tractor.col, tractor.row - 1)
|
if board.is_weed(tractor.col, tractor.row - 1):
|
||||||
tractor.row -= 1
|
board.set_grass(tractor.col, tractor.row - 1)
|
||||||
tractor.direction = "up"
|
tractor.row -= 1
|
||||||
elif board.is_dirt(tractor.col, tractor.row - 1):
|
elif board.is_dirt(tractor.col, tractor.row - 1):
|
||||||
board.set_soil(tractor.col, tractor.row - 1)
|
board.set_soil(tractor.col, tractor.row - 1)
|
||||||
tractor.row -= 1
|
tractor.row -= 1
|
||||||
tractor.direction = "up"
|
elif not board.is_rock(tractor.col, tractor.row - 1):
|
||||||
elif not board.is_rock(tractor.col, tractor.row - 1):
|
tractor.row -= 1
|
||||||
tractor.row -= 1
|
if(tractor.direction == "left" and tractor.col > 0):
|
||||||
tractor.direction = "up"
|
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):
|
||||||
if keys[pygame.K_DOWN] and tractor.row < rows-1 :
|
board.set_soil(tractor.col - 1, tractor.row)
|
||||||
if board.is_weed(tractor.col, tractor.row + 1):
|
tractor.col -= 1
|
||||||
board.set_grass(tractor.col, tractor.row + 1)
|
elif not board.is_rock(tractor.col - 1, tractor.row):
|
||||||
tractor.row += 1
|
tractor.col -= 1
|
||||||
tractor.direction = "down"
|
if(tractor.direction == "down" and tractor.row < rows - 1):
|
||||||
elif board.is_dirt(tractor.col, tractor.row + 1):
|
if board.is_weed(tractor.col, tractor.row + 1):
|
||||||
board.set_soil(tractor.col, tractor.row + 1)
|
board.set_grass(tractor.col, tractor.row + 1)
|
||||||
tractor.row += 1
|
tractor.row += 1
|
||||||
tractor.direction = "down"
|
elif board.is_dirt(tractor.col, tractor.row + 1):
|
||||||
elif not board.is_rock(tractor.col, tractor.row + 1):
|
board.set_soil(tractor.col, tractor.row + 1)
|
||||||
tractor.row += 1
|
tractor.row += 1
|
||||||
tractor.direction = "down"
|
elif not board.is_rock(tractor.col, tractor.row + 1):
|
||||||
|
tractor.row += 1
|
||||||
|
if(tractor.direction == "right" and tractor.col < cols - 1):
|
||||||
if keys[pygame.K_LEFT] and tractor.col > 0:
|
if board.is_weed(tractor.col + 1, tractor.row):
|
||||||
if board.is_weed(tractor.col - 1, tractor.row):
|
board.set_grass(tractor.col + 1, tractor.row)
|
||||||
board.set_grass(tractor.col - 1, tractor.row)
|
tractor.col += 1
|
||||||
tractor.col -= 1
|
elif board.is_dirt(tractor.col + 1, tractor.row):
|
||||||
tractor.direction = "left"
|
board.set_soil(tractor.col + 1, tractor.row)
|
||||||
elif board.is_dirt(tractor.col - 1, tractor.row):
|
tractor.col += 1
|
||||||
board.set_soil(tractor.col - 1, tractor.row)
|
elif not board.is_rock(tractor.col + 1, tractor.row):
|
||||||
tractor.col -= 1
|
tractor.col += 1
|
||||||
tractor.direction = "left"
|
if keys[pygame.K_LEFT]:
|
||||||
elif not board.is_rock(tractor.col - 1, tractor.row):
|
for i in range(0, 4):
|
||||||
tractor.col -= 1
|
if(tractor.direction == rotation[i]):
|
||||||
tractor.direction = "left"
|
if(i == 0):
|
||||||
|
i = 4
|
||||||
|
tractor.direction = rotation[i-1]
|
||||||
if keys[pygame.K_RIGHT] and tractor.col < cols - 1:
|
break
|
||||||
if board.is_weed(tractor.col + 1, tractor.row):
|
if keys[pygame.K_RIGHT]:
|
||||||
board.set_grass(tractor.col + 1, tractor.row)
|
for i in range(0, 4):
|
||||||
tractor.col += 1
|
if(tractor.direction == rotation[i]):
|
||||||
tractor.direction = "right"
|
if(i == 3):
|
||||||
elif board.is_dirt(tractor.col + 1, tractor.row):
|
i = -1
|
||||||
board.set_soil(tractor.col + 1, tractor.row)
|
tractor.direction = rotation[i+1]
|
||||||
tractor.col += 1
|
break
|
||||||
tractor.direction = "right"
|
|
||||||
elif not board.is_rock(tractor.col + 1, tractor.row):
|
|
||||||
tractor.col += 1
|
|
||||||
tractor.direction = "right"
|
|
||||||
|
|
||||||
board.draw_cubes(WIN)
|
board.draw_cubes(WIN)
|
||||||
tractor.draw(WIN)
|
tractor.draw(WIN)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user