Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
9.4 kB
2
Indexable
Never
from asyncio.windows_events import NULL
import sys

from MapNode import MapNode

class PlannerNode:
    
    def __init__(self):
        self.current_obj=MapNode()
        
        # Since we know that the first step the bot will take will be down, we can simply do it here
        self.current_obj.direction_callback("down")  # example 1
        notDir = 'up'
        self.stack = []
        self.dead =[]
        self.directions = ['up', 'down', 'left', 'right']
        
        self.found = False
        self.wall_callback(notDir)
        self.getDirections(self.stack)
        
    @staticmethod  
    def getDirections(stack):
        finalDirections = [0] * len(stack)
        finalDirections[0] = 1
        for i in range(1, len(stack)):
            x = stack[i][0]
            y = stack[i][1]
            if (x < stack[i-1][0]):
                finalDirections[i] = 0
            if (x > stack[i-1][0]):
                finalDirections[i] = 1
            if (y < stack[i-1][1]):
                finalDirections[i] = 2
            if (y > stack[i-1][1]):
                finalDirections[i] = 3
        print(finalDirections)
               



    def wall_callback(self, notDir):
        current_coords = self.current_obj.current
        end_coords = self.current_obj.map.end
        tileNum = self.current_obj.array[current_coords[0]][current_coords[1]]
        print(self.current_obj.current)
        # current_obj has all the attributes to help you in in your path planning !
         
         
           
        self.stack.append(current_coords)
        
        if(current_coords == self.current_obj.map.end):
            self.found = True
            
            return

        # for up and left 
        if (current_coords[0] > end_coords[0] and current_coords[1] > end_coords[1]):
            if (notDir != self.directions[0] and not self.current_obj.map.check_top_wall(current_coords) and ((current_coords[0] - 1, current_coords[1]) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[0])
                self.wall_callback(self.directions[1])
                if(self.found):
                    return
                
            elif (notDir != self.directions[2] and not self.current_obj.map.check_left_wall(current_coords) and ((current_coords[0], current_coords[1] - 1) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[2])
                self.wall_callback(self.directions[3])
                if(self.found):
                    return

        # for down and left
        if (current_coords[1] > end_coords[1] and current_coords[0] < end_coords[0]):
            if (notDir != self.directions[2] and not self.current_obj.map.check_left_wall(current_coords) and ((current_coords[0], current_coords[1] - 1) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[2])
                self.wall_callback(self.directions[3])
                if(self.found):
                    return
            elif (notDir != self.directions[1] and not self.current_obj.map.check_bottom_wall(current_coords) and ((current_coords[0] + 1, current_coords[1]) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[1])
                self.wall_callback(self.directions[0])
                if(self.found):
                    return
        
         #for down only
        if (current_coords[1] == end_coords[1] and current_coords[0] < end_coords[0]):
             if (notDir != self.directions[1] and not self.current_obj.map.check_bottom_wall(current_coords) and ((current_coords[0] + 1, current_coords[1]) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[1])
                self.wall_callback(self.directions[0])
                if(self.found):
                    return
        
        # for up only
        if (current_coords[1] == end_coords[1] and current_coords[0] > end_coords[0]):
            if (notDir != self.directions[0] and not self.current_obj.map.check_top_wall(current_coords) and ((current_coords[0] - 1, current_coords[1]) not in (self.stack + self.dead))):
                 self.current_obj.direction_callback(self.directions[0])
                 self.wall_callback(self.directions[1])
                 if(self.found):
                    return

        # for left only 
        if (current_coords[0] == end_coords[0] and current_coords[1] > end_coords[1]):
            if (notDir != self.directions[2] and not self.current_obj.map.check_left_wall(current_coords) and ((current_coords[0], current_coords[1] - 1) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[2])
                self.wall_callback(self.directions[3])
                if(self.found):
                    return
        
        # for right only 
        if (current_coords[0] == end_coords[0] and current_coords[1] < end_coords[1]):
            if (notDir != self.directions[3] and not self.current_obj.map.check_right_wall(current_coords) and ((current_coords[0], current_coords[1] + 1) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[3])
                self.wall_callback(self.directions[2])
                if(self.found):
                    return
        

           
        
         # for down and right     
        if (current_coords[0] < end_coords[0] and current_coords[1] < end_coords[1]):
            if (notDir != self.directions[1] and not self.current_obj.map.check_bottom_wall(current_coords) and ((current_coords[0] + 1, current_coords[1]) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[1])
                self.wall_callback(self.directions[0])
                if(self.found):
                    return
            elif (notDir != self.directions[3] and not self.current_obj.map.check_right_wall(current_coords) and ((current_coords[0], current_coords[1] + 1) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[3])
                self.wall_callback(self.directions[2])
                if(self.found):
                    return
            

        #for up and right
        if (current_coords[1] < end_coords[1] and current_coords[0] > end_coords[0]):
            if (notDir != self.directions[3] and not self.current_obj.map.check_right_wall(current_coords) and ((current_coords[0], current_coords[1] + 1) not in (self.stack + self.dead))):
                self.current_obj.direction_callback(self.directions[3])
                self.wall_callback(self.directions[2])
                if(self.found):
                    return
            elif (notDir != self.directions[0] and not self.current_obj.map.check_top_wall(current_coords) and ((current_coords[0] - 1, current_coords[1]) not in (self.stack + self.dead))):
                 self.current_obj.direction_callback(self.directions[0])
                 self.wall_callback(self.directions[1])
                 if(self.found):
                    return
            
        else:
                if (notDir != self.directions[0] and not self.current_obj.map.check_top_wall(current_coords) and ((current_coords[0] - 1, current_coords[1]) not in (self.stack + self.dead))):
                    self.current_obj.direction_callback(self.directions[0])
                    self.wall_callback(self.directions[1])
                    if(self.found):
                        return
                

                if (notDir != self.directions[1] and not self.current_obj.map.check_bottom_wall(current_coords) and ((current_coords[0] + 1, current_coords[1]) not in (self.stack + self.dead))):
                    self.current_obj.direction_callback(self.directions[1])
                    self.wall_callback(self.directions[0])
                    if(self.found):
                        return

                if (notDir != self.directions[2] and not self.current_obj.map.check_left_wall(current_coords) and ((current_coords[0], current_coords[1] - 1) not in (self.stack + self.dead))):
                    self.current_obj.direction_callback(self.directions[2])
                    self.wall_callback(self.directions[3])
                    if(self.found):
                        return
                
                if (notDir != self.directions[3] and not self.current_obj.map.check_right_wall(current_coords) and ((current_coords[0], current_coords[1] + 1) not in (self.stack + self.dead))):
                    self.current_obj.direction_callback(self.directions[3])
                    self.wall_callback(self.directions[2])
                    if(self.found):
                        return
        

        self.stack.pop()
        if (tileNum == 0 or tileNum == 2 or tileNum == 4 or tileNum == 8):
            self.dead.append(current_coords)
        
        self.current_obj.direction_callback(notDir)
        
        

if __name__ == '__main__':
    
    start_obj=PlannerNode()
    start_obj.current_obj.print_root.mainloop()