improve limits of succ(), comments cleanup

This commit is contained in:
Szymon Szczubkowski 2023-04-17 16:24:08 +02:00
parent 2cb44dcb01
commit eaf7ed46fe
2 changed files with 25 additions and 25 deletions

View File

@ -6,34 +6,36 @@ class Node:
class Search:
def __init__(self, cell_size):
def __init__(self, cell_size, cell_number):
self.cell_size = cell_size
self.cell_number = cell_number
#this method needs to check the size of the plane - for now, it assumes 15x15, like in main.py
def succ(self, state):
x = state[0]
y = state[1]
angle = state[2]
if angle == 0: # up
if y != 0: #this can stay as 0, negative values are not tolerated
return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]]
else:
return [['left', x, y, 270], ['right', x, y, 90]]
if angle == 90: # right
if x != 700: #this needs to be changed to relative once size of plane is read by succ (parse in Search constructor?)
return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]]
else:
return [['left', x, y, 0], ['right', x, y, 180]]
if angle == 180: # down
if y != 700: #this needs to be changed to relative once size of plane is read by succ (parse in Search constructor?)
return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]]
else:
return [['left', x, y, 90], ['right', x, y, 270]]
if angle == 270: # left
if x != 0: #this can stay as 0, negative values are not tolerated
return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]]
else:
return [['left', x, y, 180], ['right', x, y, 0]]
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
match(angles[angle]):
case 'UP':
if y != 0:
return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]]
else:
return [['left', x, y, 270], ['right', x, y, 90]]
case 'RIGHT':
if x != self.cell_size*(self.cell_number-1):
return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]]
else:
return [['left', x, y, 0], ['right', x, y, 180]]
case 'DOWN':
if y != self.cell_size*(self.cell_number-1):
return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]]
else:
return [['left', x, y, 90], ['right', x, y, 270]]
case 'LEFT':
if x != 0:
return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]]
else:
return [['left', x, y, 180], ['right', x, y, 0]]
def graphsearch(self, istate, goaltest):
x = istate[0]
@ -67,8 +69,6 @@ class Search:
explored.append(elem.state)
for (action, state_x, state_y, state_angle) in self.succ(elem.state):
if state_x < 0 or state_y < 0: # check if any of the values are negative
continue
if [state_x, state_y, state_angle] not in fringe_state and \
[state_x, state_y, state_angle] not in explored:
x = Node([state_x, state_y, state_angle])

View File

@ -69,7 +69,7 @@ class Game:
move_tractor_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_tractor_event, 1000) # tractor moves every 1000 ms
tractor_next_moves = []
graph_search_object = graph_search.Search(self.cell_size)
graph_search_object = graph_search.Search(self.cell_size, self.cell_number)
while running:
clock.tick(60) # manual fps control not to overwork the computer