Exemplo n.º 1
0
class Demo( Thread ):
    """manages an escape machine game
    """
    LEVELS = [ {"r":3, "c":4, "ge":1, "rs":80, "comp":30, "con":60} ] # 5

    def __init__( self, gui, assembly_graph, level=0, rank_window=True ):
        Thread.__init__( self )
        
        self.gui = gui
        self.assembly_graph = assembly_graph
        self.level = level

        self.maze = None

        # determines color ranking from assembly graph
        self.color_demon = None
        if rank_window:
            self.color_demon = Color_Demon( assembly_graph, self.gui )
            self.color_demon.show()
        else:
            self.color_demon = Color_Demon( assembly_graph )

        self.next_queue = Queue()

        # start first maze
        self.next( True, 0, 0 )

    def run( self ):
        while self.next_queue is not None:
            game_over, escaped, eaten = self.next_queue.get()
            self._next_maze( game_over, escaped, eaten )

    def next( self, game_over, escaped, eaten ):
        self.next_queue.put( (game_over, escaped, eaten) )
        
    def _next_maze( self, game_over, escaped, eaten ):
        """build next maze when previous maze is closed
        """
        # if last level wasn't lost go to next level
        if not game_over:
            self.level += 1
            if self.level > (len(self.LEVELS) - 1):
                self.level = len(self.LEVELS) - 1

        print "***level %d***" % self.level
        
        self.maze = Maze( gui=self.gui,
                          next=self.next,
                          rows=self.LEVELS[self.level]["r"],
                          columns=self.LEVELS[self.level]["c"],
                          ghost_eaters=self.LEVELS[self.level]["ge"],
                          room_size=self.LEVELS[self.level]["rs"],
                          completeness=self.LEVELS[self.level]["comp"],
                          connectedness=self.LEVELS[self.level]["con"],
                          escaped=escaped,
                          eaten=eaten )

        self.maze.show()

        self.color_demon.maze = self.maze
Exemplo n.º 2
0
 def test_robot_move_updates_maze(self):
     maze = Maze(7)
     brain = Brain(maze)
     robot_pos = [1, 4]
     bat = RoboBat(maze, robot_pos, brain)
     bat.move_in_direction("left")
     self.assertFalse(maze.value_at_point(robot_pos) == 3)
     self.assertTrue(maze.value_at_point(bat.pos) == 3)
Exemplo n.º 3
0
 def __generate_map(self):
     _maze = Maze(self.lvl_length, self.lvl_width)
     self.map = _maze.gen_map()
     self.lvl_length = _maze.length
     self.lvl_width = _maze.width
     
     # Add a few open spaces
     _tf = TerrainFactory()
     for j in range(randrange(1,4)):
         _row = randrange(4, self.lvl_length - 4)
         _col = randrange(4, self.lvl_width - 4)
         for _r in (-1, 0, 1):
             for _c in (-1, 0 , 1):
                 self.map[_row + _r][_col + _c] = _tf.get_terrain_tile(CYBERSPACE_FLOOR)
Exemplo n.º 4
0
    def _next_maze( self, game_over, escaped, eaten ):
        """build next maze when previous maze is closed
        """
        # if last level wasn't lost go to next level
        if not game_over:
            self.level += 1
            if self.level > (len(self.LEVELS) - 1):
                self.level = len(self.LEVELS) - 1

        print "***level %d***" % self.level
        
        self.maze = Maze( gui=self.gui,
                          next=self.next,
                          rows=self.LEVELS[self.level]["r"],
                          columns=self.LEVELS[self.level]["c"],
                          ghost_eaters=self.LEVELS[self.level]["ge"],
                          room_size=self.LEVELS[self.level]["rs"],
                          completeness=self.LEVELS[self.level]["comp"],
                          connectedness=self.LEVELS[self.level]["con"],
                          escaped=escaped,
                          eaten=eaten )

        self.maze.show()

        self.color_demon.maze = self.maze
Exemplo n.º 5
0
    def __init__(self):
        self.mode=0#mapping mode
        '''
        Constructor
        mode=0 er mapping
        mode=1 er goToPath
        '''
        self.logger=logging.getLogger("robot.Mapping")
        self.logger.info("Mapping initialised")
        self.mode=0#mapping
        self.maze=Maze()
        self.globalCompass=[0,1,2,3]#north/east/south/west
        self.startDirection=self.globalCompass[2]
        self.direction=self.startDirection#south
                
        self.startPosition =[0,0]
        self.currentPosition=self.startPosition#x,y
        
        self.lastWas180=False
        self.lastPosition=self.currentPosition
        self.funcDict={
                       0:self.subY,
                       1:self.addX, 
                       2:self.addY, 
                       3:self.subX}

        self.stack=[]
        '''
Exemplo n.º 6
0
 def loadMaze(self,jsonfile):
     with open(jsonfile) as jsondata:
         data = json.load(jsondata)
         maze = data["layout"]
         heading = data["robot_orientation"]
         start = Point(data["start_pos"]["x"],data["start_pos"]["y"])
         location = Point(data["robot_pos"]["x"],data["robot_pos"]["y"])
         finish = Point(data["finish_pos"]["x"],data["finish_pos"]["y"])
         m = Maze(maze)
         m.setStart(start)
         m.setFinish(finish)
         m.setLocation(location)
         m.setHeading(heading)
     return m
Exemplo n.º 7
0
    def createMaze():
        "Original Design"
        maze = Maze()
        r1 = Room(1)
        r2 = Room(2)
        door = Door(r1, r2)

        r1.setSide('North', Wall())
        r1.setSide('West', Wall())
        r1.setSide('South', Wall())
        r1.setSide('East', door)

        r2.setSide('North', Wall())
        r2.setSide('West', door)
        r2.setSide('South', Wall())
        r2.setSide('East', Wall())

        maze.addRoom(r1)
        maze.addRoom(r2)
        
        return maze
Exemplo n.º 8
0
	def __init__(self):
		self._tw_pub = rospy.Publisher(TWIST_PUB, Twist)
		self._rate = rospy.Rate(RATE)
		self._checks = {
			Direction.FORWARD: None,\
			Direction.LEFT: None,\
			Direction.RIGHT: None,\
			"ARRAY": None\
		}
		self._sense_subs = [\
			rospy.Subscriber(BUMPER_IO, TurtlebotSensorState, self._collision),\
			rospy.Subscriber(SCAN_IO, Float64MultiArray, self._scan)\
		]
		self.maze = Maze()
		self._last_collision = None
Exemplo n.º 9
0
    def __init__(self,gridSize=32,frameRate=60):
        super(Game,self).__init__('TheGame.You Lost It.')

        pygame.init()

        self.framerate = frameRate
        self.maze = Maze(gridSize)
        self.maze.read("theMaze")
        self.screen = pygame.display.set_mode(self.maze.rect.size,0,32)
        self.clock = pygame.time.Clock()
        self.paused = False

        self.add(AttractMode(self))
        self.add(SetupLevelMode(self))
        self.add(PlayingMode(self))
        self.add(IntermissionMode(self))
        self.add(GameOverMode(self))
        self.setStateByName(PlayingMode.name)
Exemplo n.º 10
0
from Maze import Maze
from astar_search import astar_search


# null heuristic, useful for testing astar search without heuristic (uniform cost search).
def null_heuristic(state):
    return 0


# Test problems

# several test on different mazes, some given and some created. Uncomment to run.
# 3 different heuristics are also available
if __name__ == "__main__":

    test_maze3 = Maze("maze3.maz")
    test_mpA = MazeworldProblem(test_maze3, (1, 4, 1, 3, 1, 2))
    test_maze4 = Maze("maze4.maz")
    test_mpB = MazeworldProblem(test_maze4, (8, 8, 8, 9, 8, 10))
    test_maze2 = Maze("maze2.maz")
    test_mpC = MazeworldProblem(test_maze2, (3, 0))
    test_maze5 = Maze("maze5.maz")
    test_mpD = MazeworldProblem(test_maze5, (4, 8, 3, 8, 2, 8))
    test_maze6 = Maze("maze6.maz")
    test_mpE = MazeworldProblem(test_maze6, (6, 4))
    test_maze7 = Maze("maze7.maz")
    test_mpF = MazeworldProblem(test_maze7, (8, 7, 1, 7))

    # this should explore a lot of nodes; it's just uniform-cost search
    result = astar_search(test_mpA, null_heuristic)
    print(result)
Exemplo n.º 11
0
from MazeworldProblem import MazeworldProblem
from Maze import Maze

from astar_search import bfs_search
from astar_search import astar_search


# null heuristic, useful for testing astar search without heuristic (uniform cost search).
def null_heuristic(state):
    return 0


# Test problems

test_maze1 = Maze("maze1.maz")
print(test_maze1)
test_mp = MazeworldProblem(test_maze1, (32, 4))

# this should explore a lot of nodes; it's just uniform-cost search
result = astar_search(test_mp, null_heuristic)
print(result)

# this should do a bit better:
result = astar_search(test_mp, test_mp.manhattan_heuristic)
print(result)
# test_mp.animate_path(result.path)

result = astar_search(test_mp, test_mp.bfs_heuristic)
print(result)
Exemplo n.º 12
0
class Strategy_HuntAndKill(IMazeGenerator):
    def __init__(self, width, height):
        self.__maze = Maze(width, height)

    def generate(self):
        current_cell = self.__maze.getRandomOrigin()
        self.__maze.markVisitedCell(current_cell)
        have_more_cells_to_hunt = True

        while have_more_cells_to_hunt:
            while self.__maze.hasUnvisitedNeightbours(current_cell) == True:
                unvisited_cell = self.__maze.getRandomUnvisitedNeightbour(current_cell)
                self.__maze.removeWallBettweCells(current_cell, unvisited_cell)
                current_cell = unvisited_cell
                self.__maze.markVisitedCell(current_cell)

            current_cell = self.huntForAvailableCell()

            if current_cell is None:
                have_more_cells_to_hunt = False

        self.__maze.addEntryAndExitPoints()

    def huntForAvailableCell(self):
        for cell in self.__maze.cells():
            if self.__maze.hasUnvisitedNeightbours(cell):
                print (cell.getX(), cell.getY())
                return cell

        return None


    def getRaw(self):
        return self.__maze.getRaw()

    def getJSON(self):
        return self.__maze.getJSON()

    def __str__(self):
        return 'Strategy_HuntAndKill'
Exemplo n.º 13
0
#APPLICATION:

print(
    "Bienvenue! \nVous allez devoir atteindre la sortie du labyrinthe! Votre personnage est symbolisé par (X) et la sortie par (O).\nVous devez ramasser les trois objets pour endormir le garde et vous enfuir.\nVous pouvez quitter le jeu a tout moment en tappant q."
)

while UserName == "":
    #loop of name demand, if nothing is written it asks name again
    UserName = User.GetName()

User.Greetings(UserName)

print(
    "\nPour vous déplacer utilisez les touches: \n(u) pour aller en haut, (d) pour aller en bas, (l) pour aller à gauche et (r) pour aller à droite.\n"
)

#Loading of the maze from text file:
Maze.LoadFromTextFile(MazeMap)
#Display objects inside the maze:
Object.PutObjectsInMaze(MazeMap, ObjectsList)
#Maze display:
Maze.DrawOnScreen(MazeMap)

while ContinueGame == True:
    #Game loop, while the user don't get out, don't win or loose it asks him/her to enter commands and it redraw the maze on screen.
    ContinueGame = Game.GetAndExecuteCommands(MazeMap, UserName)
    Maze.DrawOnScreen(MazeMap)

os.system("pause")
Exemplo n.º 14
0
 def __init__(self):
     self._maze = Maze()
     self._player = Player(self._maze)
     pass
Exemplo n.º 15
0
 def __init__(self,maze):
     self.maze = Maze(maze)
     self.current_position = self.maze.current_position
     self.visited_positions = {self.current_position}
     self.path_queue = deque()
Exemplo n.º 16
0
class BFSSolver:
    def __init__(self,maze):
        self.maze = Maze(maze)
        self.current_position = self.maze.current_position
        self.visited_positions = {self.current_position}
        self.path_queue = deque()

    def solve(self):
        x,y = self.current_position
        self.visited_positions.add((x,y))
        path = [self.current_position]
        while not self.maze.is_end(x,y):
            self.maze.mark_checked(x,y)
            if (x,y+1) not in self.visited_positions and self.maze.is_open(x,y+1):
                self.visited_positions.add((x, y + 1))
                new_path = list(path)
                new_path.append((x, y + 1))
                self.path_queue.append(new_path)
            if (x,y-1) not in self.visited_positions and self.maze.is_open(x,y-1):
                self.visited_positions.add((x, y - 1))
                new_path = list(path)
                new_path.append((x, y - 1))
                self.path_queue.append(new_path)
            if (x+1,y) not in self.visited_positions and self.maze.is_open(x+1,y):
                self.visited_positions.add((x + 1, y))
                new_path = list(path)
                new_path.append((x + 1, y))
                self.path_queue.append(new_path)
            if (x-1,y) not in self.visited_positions and self.maze.is_open(x-1,y):
                self.visited_positions.add((x - 1, y))
                new_path = list(path)
                new_path.append((x - 1, y))
                self.path_queue.append(new_path)
            path = self.path_queue.popleft()
            x,y = path[len(path)-1]

        prevX, prevY = self.current_position
        for (solX, solY) in path:
            if solY > prevY:
                if not self.maze.move('s'):
                    logging.error("Failed move south")
            if solY < prevY:
                if not self.maze.move('n'):
                    logging.error("Failed move north")
            if solX > prevX:
                if not self.maze.move('e'):
                    logging.error("Failed move east")
            if solX < prevX:
                if not self.maze.move('w'):
                    logging.error("Failed move west")
            prevX = solX
            prevY = solY
        if self.maze.at_end():
            logging.info("Solved in " + str(len(path)) + " moves!")
            self.maze.print_solution()
        else:
            logging.error("Failed! End position: " + str(self.maze.END_POSITION) + " Current position: "  + str(self.maze.current_position))
Exemplo n.º 17
0
moves = 0
turns = 0

running = True
display = pygame.display.set_mode((700,700))
myfont = pygame.font.SysFont("monospace", 30)

# render text
title = myfont.render("Run-In-A-Maze Game!", 15, (255,255,0))

display.blit(title, (10, 10))



# SURFACES ARE PASSED BY REFERENCE
current = Maze(display, 18, 20)
current.mazify()

#current.place(display, random.randint(0, current.getrow()), random.randint(0, current.getcol()))
current.draw(40, 60)


while running:
    display.blit(myfont.render("Turns: " + str(turns), 15, (255, 255, 0)), (10, 30))
    pygame.display.update()

    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
Exemplo n.º 18
0
    def GenerateMaze(self, width, height):
        theMaze = Maze()
        theMaze.SetDimensions(width, height)

        # treat the maze generation as a depth first search
        # start with the top corner as the first cell
        maze_cells = []
        adjacent_cells = {}

        # choose a start cell
        current_cell = [np.random.randint(0, theMaze.height), np.random.randint(0, theMaze.width)]
        maze_cells.append("%d-%d" % (current_cell[0], current_cell[1]))

        # create adjacency list
        borders = theMaze.GetBorders(current_cell)
        for index in xrange(len(borders)):
            if borders[index] == 0:
                new_cell = deepcopy(current_cell)
                if index == 0:
                    new_cell[0] -= 1
                elif index == 1:
                    new_cell[1] += 1
                elif index == 2:
                    new_cell[0] += 1
                elif index == 3:
                    new_cell[1] -= 1
                cell_key = "%d-%d" % (new_cell[0], new_cell[1])
                adjacent_cells[cell_key] = []
                adjacent_cells[cell_key].append([current_cell, index])

        # print "FIRST SET", adjacent_cells, maze_cells
        while len(adjacent_cells) > 0:
            # choose a cell to add to the maze at random
            cell_key = np.random.randint(0, len(adjacent_cells))
            cell_key = adjacent_cells.keys()[cell_key]
            cell = adjacent_cells[cell_key]
            del (adjacent_cells[cell_key])
            maze_cells.append(cell_key)
            cell_key = map(int, cell_key.split("-"))

            if self.mVerbosity > 0:
                print "active cell: ", cell_key, "|", cell, len(cell)

            # choose a random edge from the set of edges connecting me
            connectedCell = np.random.randint(0, len(cell))
            connectedCell = cell[connectedCell]
            wallToOpen = connectedCell[1]
            rWallToOpen = (wallToOpen + 2) % 4
            connectedCell = connectedCell[0]
            if self.mVerbosity > 0:
                print "    connect to", connectedCell, "by", wallToOpen, "-", rWallToOpen

            theMaze.cells[connectedCell[0], connectedCell[1], wallToOpen] = 1
            theMaze.cells[cell_key[0], cell_key[1], rWallToOpen] = 1

            borders = theMaze.GetBorders(cell_key)
            for index in xrange(len(borders)):
                if borders[index] == 0:
                    new_cell = deepcopy(cell_key)
                    if index == 0:
                        new_cell[0] -= 1
                    elif index == 1:
                        new_cell[1] += 1
                    elif index == 2:
                        new_cell[0] += 1
                    elif index == 3:
                        new_cell[1] -= 1
                    new_cell_key = "%d-%d" % (new_cell[0], new_cell[1])
                    if new_cell_key not in maze_cells:
                        if new_cell_key not in adjacent_cells:
                            adjacent_cells[new_cell_key] = []
                        adjacent_cells[new_cell_key].append([cell_key, index])

        theMaze.end_cell = [0, 0]
        theMaze.start_cell = [theMaze.height - 1, theMaze.width - 1]
        return theMaze
Exemplo n.º 19
0
    def GenerateMaze(self, width, height):
        theMaze = Maze()
        theMaze.SetDimensions(width, height)

        # treat the maze generation as a depth first search
        # start with the top corner as the first cell
        active_cells = []
        dead_cells = []
        active_cells.append([0, 0])

        while len(active_cells) > 0:
            thisCell = active_cells[-1]

            if self.mVerbosity > 0:
                print "active cell", thisCell,

            # remove a wall from this cell
            exits = theMaze.GetBorders(thisCell)

            # choose a random exit to open
            walls = exits == 0
            wallToOpen = None

            choices = list(np.arange(4)[walls])
            if self.mVerbosity > 0:
                print "walls", choices

            while wallToOpen is None and len(choices) > 0:
                wallIndex = np.random.randint(len(choices))
                wallToOpen = choices[wallIndex]
                choices.pop(wallIndex)
                newCell = deepcopy(thisCell)
                # wallToOpen = np.random.choice(choices)
                # choices.remove(wallToOpen)

                if wallToOpen == 0:
                    newCell[0] -= 1
                elif wallToOpen == 1:
                    newCell[1] += 1
                elif wallToOpen == 2:
                    newCell[0] += 1
                else:
                    newCell[1] -= 1

                if self.mVerbosity > 0:
                    print "    choose: ", wallToOpen, newCell, (wallToOpen + 2) % 4

                if newCell in active_cells or newCell in dead_cells:
                    if self.mVerbosity > 0:
                        print "        Bad choice", "Active" if newCell in active_cells else "Dead"
                    wallToOpen = None

            # if choice is none, we ran out of options
            if wallToOpen is None:
                active_cells.pop(-1)
                dead_cells.append(thisCell)
            else:
                # alright! neighbbor!
                rWallToOpen = (wallToOpen + 2) % 4
                # print "        open %d and %d" % (wallToOpen, rWallToOpen)
                theMaze.cells[thisCell[0], thisCell[1], wallToOpen] = 1
                theMaze.cells[newCell[0], newCell[1], rWallToOpen] = 1

                active_cells.append(newCell)

            # if np.count_nonzero(exits > 0) >= 2:
            #    active_cells.pop(-1)
            #    dead_cells.append(thisCell)

        # theMaze.end_cell = [0, 0]
        # theMaze.start_cell = [theMaze.height -1, theMaze.width -1]
        if self.mVerbosity > 0:
            print
        return theMaze
Exemplo n.º 20
0
class Mapping():
    '''
    classdocs
    '''
    stepsPrCell=6000

    def __init__(self):
        self.mode=0#mapping mode
        '''
        Constructor
        mode=0 er mapping
        mode=1 er goToPath
        '''
        self.logger=logging.getLogger("robot.Mapping")
        self.logger.info("Mapping initialised")
        self.mode=0#mapping
        self.maze=Maze()
        self.globalCompass=[0,1,2,3]#north/east/south/west
        self.startDirection=self.globalCompass[2]
        self.direction=self.startDirection#south
                
        self.startPosition =[0,0]
        self.currentPosition=self.startPosition#x,y
        
        self.lastWas180=False
        self.lastPosition=self.currentPosition
        self.funcDict={
                       0:self.subY,
                       1:self.addX, 
                       2:self.addY, 
                       3:self.subX}

        self.stack=[]
        '''
                        1 : self.goStraight,
                       2 : self.turnRight,
                       3 : self.turn180,
                       4 : self.turnLeft
                       }
                       
        table:
        0,0 -------------- x,0
        |
        |
        |
        |
        |
        |
        0,y
        '''
    def receiveStack(self,path):        
        self.mode=1#path finding go to mode
        self.stack=path#self.pathToStack()
        print self.stack
        
    def getLocalDirection(self,lastS,s):
        #print lastS
        left=lastS-1
        if left<0:
            left=3
        right=lastS+1
        if right>3:
            right=0
        
        direction180=lastS-2
        if direction180 < 0:
            direction180+=4        
        
        if left==s:
            return 2
        elif right==s:
            return 4
        elif direction180==s:
            return 3
        return 1
    
    def getCurrentPosition(self):
        print "mapping getCurrentPosition"
        print"currentPos="+str(self.currentPosition)
        return self.currentPosition
    
    def stepsToCells(self,steps):
        #print steps
        cells=(steps*1.0)/(self.stepsPrCell*1.0)
        decimals=cells % 1
        cells=int(cells)
        if decimals > 0.68 and cells<1:
            cells+=1
        elif decimals > 0.68:
            cells+=1
        self.logger.info("cells/"+str(cells))
        return cells
    
    def wallsToGlobalWalls(self,walls):
        north=0
        east=0
        south=0
        west=0
        if(self.startPosition == self.currentPosition):#south
            north=1
            east=walls[0]
            south=walls[2]
            west=walls[1]
        else:
            if(self.direction==0):#north
                west=walls[0]
                east=walls[1]
                south=0
                north=walls[2]
            elif(self.direction==1):#east
                west=0
                east=walls[2]
                south=walls[1]
                north=walls[0]
            elif(self.direction==2):#south
                north=0
                east=walls[0]
                south=walls[2]
                west=walls[1]
            else:#west
                west=walls[2]
                east=0
                south=walls[0]
                north=walls[1]
        return [north,east,south,west]
    
    def wallsToInt(self,walls):
        value=(((walls[0]<<3) | (walls[1]<<2)) | (walls[2]<<1)) | (walls[3])        
        return value
                
    def getChoice(self,steps=None,walls=None):
        if not self.mode:
            return self.mappingChoice(steps, walls)
        else:
            return self.gotoChoice()
        
    def gotoChoice(self):
        returnChoice=[0,0]#steps,local direction
        cells=0
        if self.stack:
            choice=self.stack.pop()
            cells=choice[1]
            returnChoice[0]=cells*self.stepsPrCell
            calcChoice=self.makeChoice([choice[0]])
            returnChoice[1]=calcChoice[3]
            self.direction=calcChoice[1]
            self.currentPosition=[choice[2],choice[3]]
        print(
              "dir="+str(self.direction)
              +"\tpoos"+str(self.currentPosition)
              +"\tchoice"+str(returnChoice)
              )
        return returnChoice

    def mappingChoice(self,steps,walls):
        self.logger.info("directionIn/"+str(self.direction))

        func=self.funcDict[self.direction]
        
        tmpWalls=self.wallsToGlobalWalls([1,1,0]) 
        cells=self.stepsToCells(steps)+1
        print "cells="+str(cells)
        for i in range(cells):
            tmpWalls=self.wallsToInt(self.wallsToGlobalWalls([1,1,0]))
            tmp=self.maze.get(self.currentPosition[0], self.currentPosition[1])
            if not tmp:
                self.maze.set(self.currentPosition[0], self.currentPosition[1], tmpWalls)
            self.currentPosition=func(self.currentPosition)
         
        tmp=self.maze.get(self.currentPosition[0], self.currentPosition[1])
        globalWalls=self.wallsToGlobalWalls(walls)            

        if not tmp:
            self.maze.set(self.currentPosition[0], self.currentPosition[1], self.wallsToInt(globalWalls))
        #self.currentPosition=self.funcDict[self.direction](self.currentPosition)
        print "after incrementation current pos="+str(self.currentPosition)+" dir="+str(self.direction)

        missingWalls=self.findMissingWalls(self.currentPosition,globalWalls)
        unexploredCells=self.findUnexploredCells(self.currentPosition,missingWalls)
        
        returnChoice=0

        if len(missingWalls)==1:#180
            if self.stack:#still unexplored nodes
                self.logger.info("180")
                self.stack.pop()

                self.logger.info("stack/"+str(self.stack))
                choice=self.makeChoice(missingWalls)
                returnChoice=3      
                self.direction=choice[1]
            else:
                pass
        else:
            if unexploredCells:
                self.logger.info("exploring")
                choice=[self.makeChoice(unexploredCells),self.currentPosition]
                self.stack.append(choice)
                self.logger.info("stack/"+str(self.stack))
                returnChoice=choice[0][3]
                self.direction=choice[0][1]
            elif self.stack:
                self.logger.info("backtracking")
                choice=self.stack.pop()
                choice=self.makeChoice([choice[0][0]])
                self.currentPosition=choice[1]

                self.logger.info("stack/"+str(self.stack))
                returnChoice=choice[0][3]
                self.direction=choice[0][1]
            else:
                print "finnished mapping"
                #func=self.funcDict[self.direction]
                #self.currentPosition=func(self.currentPosition)
                return 0
        print(
              "dir="+str(self.direction)
              +"\tpos"+str(self.currentPosition)
              +"\tchoice"+str(returnChoice)
              )
        self.logger.info("returnChoice/"+str(returnChoice))
        self.logger.info("currentpos/"+str(self.currentPosition))
        self.logger.info("directionOut/"+str(self.direction))

        self.lastPosition=self.currentPosition

        return returnChoice
        
    def findMissingWalls(self,pos,globalWalls):
        posibilities=[]
        for d in range(4):
            if not globalWalls[d]:
                posibilities.append(d)
        return posibilities
    
    def findUnexploredCells(self,pos,missingWalls):
        posibilities=[]
        for d in missingWalls:
            xy=self.funcDict[d](pos)
            if xy[0]>=0 and xy[1]>=0:
                tmp=self.maze.get(xy[0], xy[1])
                if not tmp:
                    posibilities.append(d)
        return posibilities
        
    def makeChoice(self,posibilities):
        left=self.direction-1
        if left<0:
            left=3
        right=self.direction+1
        if right>3:
            right=0
        
        back=self.direction-2
        if back < 0:
            back+=4
            #print "direction="+str(self.direction)+"back before="+str(back)+" back after"+str(back2)
        if self.direction in posibilities:
            return [back,self.direction,1,1] # som i [til brug, til stack, til turnthread]
        elif right in posibilities:
            return [left,right,2,4]
        elif left in posibilities:
            return [right,left,4,2]
        lol=[self.direction,back,1,3]
        #self.stack.append(lol)
        #self.stack.append(lol)
        return lol

    def addX(self,xy):
        return [xy[0]+1,xy[1]]
    
    def subX(self,xy):
        return [xy[0]-1,xy[1]]
    
    def addY(self,xy):
        return [xy[0],xy[1]+1]
    
    def subY(self,xy):
        return [xy[0],xy[1]-1]
        
    def getMaze(self):
        return self.maze
Exemplo n.º 21
0
    print("")

node1 = Node(False, False, False, False, (0, 0), False)
node2 = Node(False, False, False, False, (0, 1), False)
node3 = Node(False, False, False, False, (1, 1), False)
node4 = Node(False, False, False, False, (-1, 1), False)
node1.set_up(node2)
node2.set_down(node1)
node2.set_left(node4)
node2.set_right(node3)
node3.set_left(node2)
node4.set_right(node2)

nodes = [node1, node2, node3, node4]

foo = Maze(nodes)
agent1 = Agent(node1, foo)
agent2 = Agent(node4, foo)
agents = [agent1, agent2]

print("Begin maze")
print("Agent 1 location: ", agent1.current_pos)
print("Agent 2 location: ", agent2.current_pos)

while (len(agent1.maze.undiscovered) > 0):
    input(">>> Press enter to continue")
    PPSOCycle(agents)
    PrintAgent(agents[0], 1)
    PrintAgent(agents[1], 2)

print("Maze fully Discovered!")
Exemplo n.º 22
0
class DFSSolver:
    def __init__(self, maze):
        self.maze = Maze(maze)
        self.current_position = self.maze.current_position
        self.visited_positions = {self.current_position}
        self.position_stack = [self.current_position]

    def solve(self):
        x, y = self.current_position
        while not self.maze.is_end(x, y):
            self.maze.mark_checked(x, y)
            self.visited_positions.add((x, y))
            if (x, y + 1) not in self.visited_positions and self.maze.is_open(x, y + 1):
                self.position_stack.append((x, y + 1))
                y += 1
            elif (x, y - 1) not in self.visited_positions and self.maze.is_open(x, y - 1):
                self.position_stack.append((x, y - 1))
                y -= 1
            elif (x + 1, y) not in self.visited_positions and self.maze.is_open(x + 1, y):
                self.position_stack.append((x + 1, y))
                x += 1
            elif (x - 1, y) not in self.visited_positions and self.maze.is_open(x - 1, y):
                self.position_stack.append((x - 1, y))
                x -= 1
            else:
                prevX, prevY = self.position_stack.pop()
                x, y = self.position_stack[len(self.position_stack) - 1]
        prevX, prevY = self.current_position
        for (solX, solY) in self.position_stack:
            if solY > prevY:
                if not self.maze.move("s"):
                    logging.error("Failed move south")
            if solY < prevY:
                if not self.maze.move("n"):
                    logging.error("Failed move north")
            if solX > prevX:
                if not self.maze.move("e"):
                    logging.error("Failed move east")
            if solX < prevX:
                if not self.maze.move("w"):
                    logging.error("Failed move west")
            prevX = solX
            prevY = solY
        if self.maze.at_end():
            logging.info("Solved in " + str(len(self.position_stack)) + " moves!")
            self.maze.print_solution()
        else:
            logging.error(
                "Failed! End position: "
                + str(self.maze.END_POSITION)
                + " Current position: "
                + str(self.maze.current_position)
            )
Exemplo n.º 23
0
 def setUp(self):
     self.m = Maze()
Exemplo n.º 24
0
 def __init__(self, maze):
     self.maze = Maze(maze)
     self.current_position = self.maze.current_position
     self.visited_positions = {self.current_position}
     self.position_stack = [self.current_position]
Exemplo n.º 25
0
            y.add(state[2])
        return len(x) + len(y)

    def __str__(self):
        string = "Blind robot problem: "
        return string

    # given a sequence of states (including robot turn), modify the maze and print it out.
    #  (Be careful, this does modify the maze!)
    def animate_path(self, path):
        # reset the robot locations in the maze
        self.maze.robotloc = tuple(self.start_state)

        for state in path:
            print(str(self))
            self.maze.robotloc = tuple(state)
            sleep(1)

            print(str(self.maze))


## A bit of test code

if __name__ == "__main__":
    test_maze3 = Maze("maze3.maz")
    test_problem = SensorlessProblem(test_maze3)
    print(test_problem.start_state)
    for successor_belief_state in test_problem.get_successors(
            test_problem.start_state):
        print(successor_belief_state)
Exemplo n.º 26
0
from l33tC4D.gui.Gui import Gui
from Maze import Maze
from time import sleep

gui = Gui()
gui.start()

maze = Maze(gui, rows=4, columns=7, ghost_eaters=3, room_size=80, completeness=0, connectedness=50)
maze.show()

sleep(1.0)
maze.rank_colors({0: [2, 1, 3], 1: [2, 0], 2: [1, 0], 3: [0]})
Exemplo n.º 27
0
import turtle
from time import sleep
from Maze import Maze

window = turtle.Screen()
mazePen = turtle.Turtle()
mazePen.speed(0)

maze = Maze(15)
maze.loadMaze('maze.txt')

delay = turtle.delay()
tracer = turtle.tracer()

turtle.tracer(0, 0)
maze.drawMaze(mazePen)
turtle.update()
turtle.tracer(tracer, delay)

sleep(5)

solPen = turtle.Turtle()
solPen.color('red', 'green')
maze.solveMaze(solPen)

window.exitonclick()
Exemplo n.º 28
0
class Move_Manager(object):
	def __init__(self):
		self._tw_pub = rospy.Publisher(TWIST_PUB, Twist)
		self._rate = rospy.Rate(RATE)
		self._checks = {
			Direction.FORWARD: None,\
			Direction.LEFT: None,\
			Direction.RIGHT: None,\
			"ARRAY": None\
		}
		self._sense_subs = [\
			rospy.Subscriber(BUMPER_IO, TurtlebotSensorState, self._collision),\
			rospy.Subscriber(SCAN_IO, Float64MultiArray, self._scan)\
		]
		self.maze = Maze()
		self._last_collision = None
	
	def debug(self, a=1):
		print(self._checks["ARRAY"])
	
	# 1: right
	# 2: left
	# 3: both
	# just record that we hit something
	def _collision(self, data):
		# TODO
		return
		collisions = data.bumps_wheeldrops
		if collisions == 0 or self._last_collision is not None:
			return
		self.stop()
		self._send_twist(-COLLISION_X, 0)
		self._last_collision = collisions
	
	# executed when we have the chance
	def _handle_collision(self):
		self.maze.collision()
		self._last_collision = None

	# LaserScan callback
	def _scan(self, data):
		arr = data.data
		self._checks[Direction.RIGHT] = arr[0]
		self._checks[Direction.FORWARD] = arr[1]
		self._checks[Direction.LEFT] = arr[2]
		self._checks["ARRAY"] = arr
	
	# TODO: buggy
	# try to adjust the left and right side to be equidistant from an imaginary wall
	def center(self, count=1):
		if  self._checks["ARRAY"] is None or \
			self._checks[Direction.FORWARD] > MIN_DIST or \
			count > CENTER_MAX_COUNT or\
			not DO_CENTER:
			return

		num = count + 1
		diff = self._checks[Direction.LEFT] - self._checks[Direction.RIGHT]

		# avoid adjustements that are too big (possibly just bad detection)
		# avoid minute adjustements
		if  abs(diff) > CENTER_MAX_COUNT * CENTER_INC or \
			abs(diff) < CENTER_TURN_DIST:
			return

		# HACK
		# guessing here, but we need to make a better decision here
		# should the robot turn towards or away from a wall?
		coeff = 1
		if abs(diff) > CENTER_MAX_COUNT * CENTER_INC / 2:
			coeff = -1

		self._send_twist(CENTER_FORWARD, 0)
		self._send_twist(0, coeff * CENTER_INC)
		self.center(count=num)
	
	# TODO
	# we don't want to be too close to a wall, we want to be in the center of a "cell"
	def not_too_close(self, count=1):
		if	self._checks[Direction.FORWARD] < TOO_CLOSE and \
			count < CLOSE_MAX_COUNT and \
			DO_NOT_GET_CLOSE:

			self._send_twist(-BACKWARDS_X(CLOSE_INC), 0)
			c = count + 1
			self.not_too_close(count=c)

	# turning is left to the callee!
	# if True, then the robot has enough room to perform the directional movement
	def check(self, direction, num=1):
		if direction == Direction.FORWARD:
			self.center()
		# check that we have room
		if self._checks[Direction.FORWARD] > MIN_DIST:
			# ok but are the sides confident?
			if not DO_GET_EDGE or self._checks[direction] > MIN_DIST:
				rospy.loginfo(self._checks)
				return True
			else:
				## need to correct, 
				## lets nudge forward since we might be looking at a corner
				# if we are trying to go forward then this check is irrelevant 
				if direction == Direction.FORWARD:
					rospy.loginfo(self._checks)
					return False
				# when middle says we are good
				# but the sides have something in front!
				elif direction == Direction.LEFT:
					if self._checks[direction] < MIN_DIST:
						self.move(Direction.RIGHT)
						self.move(Direction.FORWARD, scale=CHECK_SCALE)
						self.move(Direction.LEFT)
					else:
						self.move(Direction.LEFT)
						self.move(Direction.FORWARD, scale=CHECK_SCALE)
						self.move(Direction.RIGHT)
					return self.check(direction)
				# ^^
				elif direction == Direction.RIGHT:
					if self._checks[direction] < MIN_DIST:
						self.move(Direction.LEFT)
						self.move(Direction.FORWARD, scale=CHECK_SCALE)
						self.move(Direction.RIGHT)
					else:
						self.move(Direction.RIGHT)
						self.move(Direction.FORWARD, scale=CHECK_SCALE)
						self.move(Direction.LEFT)
					return self.check(direction)
			rospy.loginfo(self._checks)
			return True
		else:
			rospy.loginfo(self._checks)
			return False
	
	# we always turn orthogonally so we won't ask for z input
	def move(self, direction, scale=1):
		if self._last_collision is not None:
			self._handle_collision()

		if direction == Turn.CLOCKWISE:
			direction = Direction.RIGHT
		if direction == Turn.COUNTER:
			direction = Direction.LEFT

		# NOTE this should be the only place where we delay after movement
		if direction == Direction.FORWARD:
			self._send_twist(TWIST_X/scale, 0)
			self.maze.step()
			self.center()
			self.not_too_close()
		elif direction == Direction.BACKWARD:
			# NOTE: backward is not just -TWIST_X
			self._send_twist(-BACKWARD(MAX_DIST)/scale, 0)
		elif direction == Direction.RIGHT or direction == Direction.LEFT:
			self._turn(direction)
			self.maze.turn(direction)
			self.center()
		else:
			rospy.loginfo("invalid direction to move() " + str(direction))

	# halt movement of the turtlebot immediately
	def stop(self):
		twist = Twist()
		self._tw_pub.publish(twist)

	def _turn(self, direction):
		# an orthogonal turn on a flat surface
		# HACK right turns suck
		val = -(TWIST_Z*RIGHT_SCALE) if direction == Direction.RIGHT else (LEFT_SCALE) * TWIST_Z
		self._send_twist(0, val)

	# actually send message
	# similar to what is found in turtlebot min_max tutorial
	def _send_twist(self, x, z):
		for _ in range(TWIST_NUM):
			twist = Twist()
			twist.linear.x = x
			twist.angular.z = z
			self._tw_pub.publish(twist)
			self._rate.sleep()
		self.stop()
		rospy.sleep(DELAY)
Exemplo n.º 29
0
from PIL import Image
import StringIO
import random
import base64
import cgi
import cgitb

cgitb.enable()

form = cgi.FieldStorage()

rows = 55
cols = 110
iterations = 50

m = Maze(rows, cols)

pattern = form['initial'].value
if pattern == 'rand':
    for i in range(0, rows):
        for j in range(0, cols):
            m.set_cells(i, j, random.random() <= 0.5)
elif pattern == 'line1':
    for i in range(0, rows):
        m.set_cells(i, cols/2, True)
elif pattern == 'line2':
    for i in range(0, cols):
        m.set_cells(rows/2, i, True)

for i in range(0, iterations):
    m.simulate()
Exemplo n.º 30
0
# set up assembly graph
sensor_queue = Queue()
event_queue = Queue()

sensor_demon = Sensor_Demon( sensor_queue, serial_port="/dev/ttyUSB0" )
assembly_demon = Assembly_Demon( sensor_queue, event_queue )
assembly_graph = Assembly_Graph( event_queue=event_queue,
                                 part_library=part_library,
                                 orient=True )

# start demon threads
sensor_demon.start()
assembly_demon.start()
assembly_graph.start()

# start gtk gui
gui = Gui()
gui.start()

# build maze
maze = Maze( gui, rows=3, columns=6, ghost_eaters=2, room_size=80,
             completeness=20, connectedness=80 )
maze.show()


# assign color demon to update color ranking on assembly graph event
color_demon = Color_Demon( assembly_graph, gui )
color_demon.show()

color_demon.maze = maze
Exemplo n.º 31
0
        self.memory.add(state, self.valid_action.index(action), reward, next_state, is_terminal)

        """间隔一段时间更新target network权重"""
        if self.step % self.EveryUpdate == 0:
            for _ in range(2):
                self._learn(batch=32)

        """update the step and epsilon"""
        self.step += 1
        self.epsilon = max(0.01, self.epsilon * 0.995)

        return action, reward

    def test_update(self):
        state = self.sense_state()
        state = np.array(state, dtype=np.int32)
        state = np.expand_dims(state, axis=0)
        q_value = self.eval_model.predict(state)
        action_index = np.argmin(q_value, axis=1).item()
        action = self.valid_action[action_index]
        reward = self.maze.move_robot(action)

        return action, reward


if __name__ == "__main__":
    maze_ = Maze(maze_size=5)

    robot = MinDQNRobot(maze=maze_)
Exemplo n.º 32
0
 def test_hardcoded_creation(self):
     robot_pos = [1, 4]
     maze = Maze(7)
     brain = Brain(maze)
     bat = RoboBat(maze, robot_pos, brain)
     self.assertEqual(3, maze.value_at_point(robot_pos))
Exemplo n.º 33
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  5 14:12:30 2018

@author: htaiwan
"""

from Maze import Maze
from Robot import Robot
from Runner import Runner

epoch = 20

epsilon0 = 0.7
alpha = 0.5
gamma = 0.9

maze_size = (6,6)
trap_number = 1

maze = Maze(maze_size=maze_size, trap_number=trap_number)
robot = Robot(maze, alpha=alpha, epsilon0=epsilon0, gamma=gamma)
robot.set_status(learning=True)

runner = Runner(robot, maze)
runner.run_training(epoch, display_direction=True)
#runner.generate_movie(filename = "final.avi") # 你可以注释该行代码,加快运行速度,不过你就无法观察到视频了。

runner.plot_results()
Exemplo n.º 34
0
class Strategy_RecursiveBacktracker(IMazeGenerator):
    def __init__(self, width, height):
        self.__maze = Maze(width, height)

    def generate(self):
        cell_stack = list()
        current_cell = self.__maze.getRandomOrigin()
        self.__maze.markVisitedCell(current_cell)

        while self.__maze.hasUnvisitedCells() == True:
            if self.__maze.hasUnvisitedNeightbours(current_cell) == True:
                unvisited_cell = self.__maze.getRandomUnvisitedNeightbour(current_cell)
                cell_stack.append(current_cell)
                self.__maze.removeWallBettweCells(current_cell, unvisited_cell)
                current_cell = unvisited_cell
                self.__maze.markVisitedCell(current_cell)
            elif len(cell_stack) > 0:
                current_cell = cell_stack.pop()
            else:
                current_cell = self.__maze.getRandomUnvisitedCellr()
                self.__maze.markVisitedCell(current_cell)

        self.__maze.addEntryAndExitPoints()

    def getRaw(self):
        return self.__maze.getRaw()

    def getJSON(self):
        return self.__maze.getJSON()

    def __str__(self):
        return 'Strategy_RecursiveBacktracker'
Exemplo n.º 35
0
from Maze import Maze
from Ant import Ant
from Bug import Bug
import numpy as np
from Viewer import draw
import cv2


if __name__ == '__main__':
    np.random.seed(60
                   )
    maze = Maze(10, 10)
    maze.reset()
    for _ in range(50):
        ant = Ant(np.random.randint(10), np.random.randint(10))
        maze.add(ant)
    for _ in range(5):
        bug = Bug(np.random.randint(10), np.random.randint(10))
        maze.add(bug)

    for step in range(100):
        print('step: ', step)
        all_agents = maze.get_all_agents()
        for agent in all_agents:
            obs = agent.get_obs(maze)
            action = agent.step(maze, obs)
        print(maze)
        all_agents = maze.get_all_agents()
        print('%d ants, %d bugs' % (sum([agent.__class__.__name__ == 'Ant' for agent in all_agents]),
                                    sum([agent.__class__.__name__ == 'Bug' for agent in all_agents])))
        img = draw(maze)
Exemplo n.º 36
0
        x = x + path[i - 1][0]
        y = y + path[i - 1][1]

        if not maze.is_floor(x, y):
            x = x - path[i - 1][0]
            y = y - path[i - 1][1]

        p.append((x, y))
    return p


#############################################################################
# I recommend ignoring everything up there. They are just helper functions to make live easier.

goal = (2, 0)
test_maze = Maze("maze4.maz")
print(test_maze)
test_mp = SensorlessProblem(test_maze, goal)
result = astar_search(test_mp, test_mp.num_heuristic)
result.path = get_path(result)
print(result)
test_all(test_maze, test_mp, result.path, goal)
#test_mp.animate_path(create_path(test_maze, (2, 0), result.path))
result = astar_search(test_mp, test_mp.spam_heuristic)
result.path = get_path(result)
print(result)

test_all(test_maze, test_mp, result.path, goal)

goal = (0, 4)
test_maze = Maze("maze3.maz")
Exemplo n.º 37
0
            successors_list.append(
                frozenset(successor_belief_set
                          ))  # add the generated successors to the list

        return successors_list

    def animate_path(self, path):
        # reset the robot locations in the maze
        self.maze.robotloc = tuple(self.start_state)
        print(self.maze.robotloc)

        for state in path:
            print(str(self))
            temp = [i for tup in state for i in tup]
            self.maze.robotloc = tuple(temp)
            sleep(1)

            print(str(self.maze))


## A bit of test code

if __name__ == "__main__":
    test_maze1 = Maze("maze1.maz")
    test_problem = SensorlessProblem(test_maze1)
    # testing my methods
    intial = test_problem.generate_intial()
    test_problem.get_successors(frozenset({(3, 1), (1, 1),
                                           (2, 2)}))  # just some test code
Exemplo n.º 38
0
class Game:
    """ Game representation
        New turn with every player move

    Attributes:
        map: Maze object which is based on np.array
        hero: Hero object; player
        hero_map: Maze object; copy of a game map with hero and trolls marked
        trolls: list of Troll objects
        top_layer_on: boolean; Tell if there is a message on the screen
        top_layer_map: Text_map object; it is for writing a message in the middle of the screen
        turn: int; actual turn
        winning_turn: int; turn upon winning
        status: 0,1,2,3  - pre_start, in_progress, win, lost
        tkinter variables:
        root
        view
        label
    """

    directions = {'up': 3, 'down': 4, 'left': 5, 'right': 6}
    grid_elements = {i: j for i, j in enumerate([' ', '#', 'X', '^', 'v', '<', '>', 't'])}

    def __init__(self, map_width, map_height, restarted=False):
        self.map = Maze(map_width, map_height)
        self.map.fill_borders()
        self.map.make_aisles()
        self.map.set_exit()
        self.hero_map = self.map.copy()
        self.hero = Hero()
        self.hero.appear(self)
        self.hero_map.add_obj(3, self.hero.coor[0], self.hero.coor[1])
        self.trolls = []
        self.top_layer_on = False
        self.top_layer_map = Text_map()
        self.turn = 0
        self.winning_turn = -1
        self.status = 0
        self.spawn_trolls(15)
        self.refresh_hero_map()
        if not restarted:
            self.root = tk.Tk()
            self.view = tk.StringVar()
            self.label = tk.Label(self.root,
                             font=("Lucida Console", 14),
                             textvariable=self.view)  # takefocus=1 may be useful

    def __str__(self):
        """
            Main "graphics" engine as the game is built without real graphics - just text
            Interprets nparrays from Maze objects (base labirynth is made with integers 0,1,2), 
            Text_Map and final array are made of string signs 
        """
        def decode(str_number):
            # function to map arrays
            try:
                return Game.grid_elements[int(float(str_number))]
            except KeyError:
                return str(str_number)[0] # if not found in Game.grid_elements
            except ValueError:
                return str_number  # top layer signs ' ' etc
        res = ''
        res_array = np.empty(self.map.shape, dtype='<U11')
        res_array[:, :] = self.hero_map.grid
        # Checking if needed and calculating top_layer with message for player
        if self.top_layer_on:
            top_layer_x_start = (self.map.shape[1] - self.top_layer_map.shape[1]) // 2
            top_layer_y_start = (self.map.shape[0] - self.top_layer_map.shape[0]) // 2
            res_array[top_layer_y_start : top_layer_y_start + self.top_layer_map.shape[0],
                      top_layer_x_start: top_layer_x_start + self.top_layer_map.shape[1]] = self.top_layer_map.grid
        # 'printing' results 
        for i in range(self.map.shape[0]):
            str_array_line = np.array(list(map(decode, res_array[i, :])))
            res += ''.join(str_array_line)
            res += '\n'
        return res

    def play(self):
        """ Main game function. Captures key presses, launches tk.root.mainloop() to start the app
        """
        self.status = 1
        a = str(self)
        self.view.set(a)

        self.root.bind('<Up>', self.hero_up)
        self.root.bind('<Down>', self.hero_down)
        self.root.bind('<Left>', self.hero_left)
        self.root.bind('<Right>', self.hero_right)
        self.root.bind('<r>', self.restart)
        self.label.pack()
        self.root.mainloop()

    def hero_up(self, key_pressed):
        self.hero_action('up')
        self.new_turn()

    def hero_down(self, key_pressed):
        self.hero_action('down')
        self.new_turn()

    def hero_left(self, key_pressed):
        self.hero_action('left')
        self.new_turn()

    def hero_right(self, key_pressed):
        self.hero_action('right')
        self.new_turn()

    def restart(self, key_pressed):
        """ Restarts the game
        """
        if self.status > 1:
            shape = self.map.shape
            self.__init__(self.map.shape[1], self.map.shape[0], restarted=True)
            self.play()

    def new_turn(self):
        """ Controls every new turn
            calculates trolls moves, refreshes map, checks if game is lost
            Sets new viewport
        """
        self.trolls_action()
        self.refresh_hero_map()
        self.turn += 1
        if self.is_lost():
            self.lose()
        self.view.set(str(self))

    def hero_action(self, direction):
        """ Controls hero move after given key press
            Checks if hero should turn himself or go in given direction if he's already turned there
            Win/Lose is also checked at the end
        """
        if self.status == 3:
            pass
        elif not self.hero.turn(direction):
            if self.check_space(self.hero.coor, direction) == 0:
                if self.hero.move(direction):
                    return True
            elif self.check_space(self.hero.coor, direction) == 1:
                if self.check_wall(direction) == 0:
                    self.push_wall(direction)
                    self.hero.move(direction)
            elif self.check_space(self.hero.coor, direction) == 2:
                self.win()
            elif self.check_space(self.hero.coor, direction) == 7:  # 7 means trolls
                self.lose()

    def spawn_trolls(self, n):
        """ Iterates through self.trolls list and spawns them.
            n: number of trolls
        """
        for i in range(n):
            self.trolls.append(Troll(self))
            self.trolls[i].appear(self)

    def trolls_action(self):
        """ Iterates through self.trolls list and controlls them.
            Every troll finds his path to hero and moves towards him.
        """
        for troll in self.trolls:
            troll.copy_game_map()
            troll.find_path()
            direction = troll.path[1]['direction']
            if not troll.turn(direction):
                troll.move(direction)

    def clear_trolls(self):
        """ Deletes all trolls from the game.
        """
        self.trolls.clear()

    def check_space(self, coor, direction, length=1):
        """ Checks space in given direction from the given coordinates

        length: int; tells how far we are checking
        returns value of a grid: int: 0-7"""
        assert direction in list(Game.directions.keys())
        try:
            if direction == 'up':
                return self.hero_map.grid[coor[0]-length, coor[1]]
            elif direction == 'down':
                return self.hero_map.grid[(coor[0]+length, coor[1])]
            elif direction == 'left':
                return self.hero_map.grid[(coor[0], coor[1]-length)]
            elif direction == 'right':
                return self.hero_map.grid[(coor[0], coor[1]+length)]
        except KeyError: # looking outside the map
            return 1

    def check_wall(self, direction):
        """ Checks a space in given direction from the hero
            returns value of a grid: int: 0-7
        """
        return self.check_space(self.hero.coor, direction, 2)

    def push_wall(self, direction):
        """ Moves a wall near hero in given direction
        """
        if direction == 'up':
            self.map.grid[self.hero.coor[0]-1, self.hero.coor[1]] = 0
            self.map.grid[self.hero.coor[0]-2, self.hero.coor[1]] = 1
        elif direction == 'down':
            self.map.grid[self.hero.coor[0]+1, self.hero.coor[1]] = 0
            self.map.grid[self.hero.coor[0]+2, self.hero.coor[1]] = 1
        elif direction == 'left':
            self.map.grid[self.hero.coor[0], self.hero.coor[1] - 1] = 0
            self.map.grid[self.hero.coor[0], self.hero.coor[1] - 2] = 1
        elif direction == 'right':
            self.map.grid[self.hero.coor[0], self.hero.coor[1] + 1] = 0
            self.map.grid[self.hero.coor[0], self.hero.coor[1] + 2] = 1

    def refresh_hero_map(self):
        self.hero_map = self.map.copy()
        if self.status != 3:
            self.hero_map.grid[self.hero.coor] = Game.directions[self.hero.direction]
        for troll in self.trolls:
            self.hero_map.grid[troll.coor] = 7

    def win(self):
        """ After winning procedures:
            set Game.status, give a message to the player, set winning turn, clear trolls
        """
        self.top_layer_on = True
        self.status = 2
        # self.top_layer_map.clear_all()
        if self.turn - self.winning_turn == 1:
            self.change_top_layer(text="Seriously...You've already won\nR for restart")
        else:
            self.winning_turn = self.turn
            self.change_top_layer(text='You won!!!\nR for restart')
        self.clear_trolls()

    def is_lost(self):
        """ Check if game is lost: does any trolls's coordinates equals to hero coors
        """
        for troll in self.trolls:
            if troll.coor == self.hero.coor:
                return True
        return False

    def lose(self):
        """ After losing procedures:
            set Game.status, give a message to player
        """
        self.top_layer_on = True
        self.status = 3
        self.change_top_layer(text="You've been eaten\nR for restart")

    def change_top_layer(self, text=None):
        """ Changes a message to player
        """
        self.top_layer_map = Text_map(text=text)
        if text:
            self.top_layer_map.make_frame()
            self.top_layer_map.add_text()

    @staticmethod
    def new_coor(coor, direction, length=1):
        """Returns new coordinates based on given by
        coor and shifted by direction and length

        length: int; tells how far we are checking
        returns coor; (letter, number)"""
        assert direction in list(Game.directions.keys())
        if direction == 'up':
            return (coor[0]-length, coor[1])
        elif direction == 'down':
            return (coor[0]+length, coor[1])
        elif direction == 'left':
            return (coor[0], coor[1] - length)
        elif direction == 'right':
            return (coor[0], coor[1] + length)

    @staticmethod
    def coors_dist(coor1, coor2):
        y_dist = abs(coor1[0] - coor2[0])
        x_dist = abs(coor1[1] - coor2[1])
        return y_dist + x_dist

    @staticmethod
    def objects_dist(object1, object2):
        y_dist = abs(object1.coor[0] - object2.coor[0])
        x_dist = abs(object1.coor[1] - object2.coor[1])
        return y_dist + x_dist
Exemplo n.º 39
0
    def showCost(self):
        plt.plot(np.arange(len(self.cost)), self.cost)
        plt.ylabel('Cost')
        plt.xlabel('training steps')
        plt.show()


def update():
    stepCounter = 0
    for e in range(para.epoch):
        state = maze.reset()  # 初始化
        while True:
            maze.fresh()  # 刷新环境(相当于显示)
            action = para.chooseAction(state)  # 基于策略选择一个行动
            newState, reward, isDone = maze.step(action)  # 获得新棋盘状态,奖励和游戏是否结束
            para.getInMemory(state, action, reward, newState)  # 将过程存入缓存以便离线学习
            if stepCounter > 200 and stepCounter % para.lsn == 0:  # 每隔几步学习以此,抵消参数间的相关性
                para.learn()
            if isDone:
                break
            state = newState
            stepCounter += 1
    maze.destroy()


if __name__ == "__main__":
    maze = Maze()  # 建立迷宫环境
    para = QParam(maze.n_actions, maze.n_features)  # 初始化QL参数缺省值参数采用默认值
    maze.after(100, update)  # 循环100次
    maze.mainloop()
    para.showCost()
Exemplo n.º 40
0
        if newState != 'terminal':
            QReal = reward + self.gamma * self.QTable.loc[newState, :].max()
        else:
            QReal = reward
        self.QTable.loc[state, action] += self.lr * (QReal - QPredict)

    def checkState(self, state):
        """
        若Q表中不存在该状态则将其添加进Q表,Q值初始化为0
        """
        if state not in self.QTable.index:
            self.QTable = self.QTable.append(pd.Series([0]*len(self.actions), index=self.actions, name=state))

def update():
    for e in range(para.epoch):
        state = maze.reset()  # 初始化
        while True:
            maze.fresh()                                  # 刷新环境(相当于显示)
            action = para.chooseAction(str(state))        # 基于策略选择一个行动
            newState, reward, isDone = maze.step(action)  # 获得新棋盘状态,奖励和游戏是否结束
            para.learn(str(state), str(newState), action, reward)  # 以字符串形式传递与tkinter有关,暂时还没了解
            if isDone:
                break
            state = newState
    maze.destory()

if __name__ == "__main__":
    maze = Maze()  # 建立迷宫环境
    para = QParam(actions=list(range(maze.n_actions)))  # 初始化QL参数缺省值参数采用默认值
    maze.after(para.epoch, update)  # 循环100次
    maze.mainloop()
Exemplo n.º 41
0
 def __init__(self, polygons, width, height, granularity, start, end):
     Maze.__init__(self, polygons, width, height, granularity, start, end)
Exemplo n.º 42
0
class Game:
    def __init__(self):
        self._maze = Maze()
        self._player = Player(self._maze)
        pass

    def start(self):
        self.notify_first_player()
        while not self.game_ended():
            self.draw()
            while self._player.steps_left > 0:
                self.wait_player_step()
                self.do_room_action()
                self.draw()
            self.notify_next_player()

    def load_maze(self):
        pass

    def draw(self):
        pass

    def wait_player_step(self):
        step_done = False
        while not step_done:
            direction = input('where move [(U)p, (L)eft, (R)ight, (D)own)? ').lower()
            if direction in ['u', 'up']:
                delta = (0, -1)
            elif direction in ['l', 'left']:
                delta = (-1, 0)
            elif direction in ['r', 'right']:
                delta = (1, 0)
            elif direction in ['d', 'down']:
                delta = (0, 1)
            else:
                print('wrong direction "{}"'.format(direction))
                continue
            if self._player.move(delta):
                step_done = True
            else:
                print('cannot move in this direction.')

    def do_room_action(self):
        room = self._maze[self._player.position]
        if room.has_treasure():
            self._player._treasure = True
            room.reset_feature()
        elif room.has_ammo():
            self._player._ammo += 2
        elif room.has_plant():
            self._player._steps = -3
        elif room.has_mine():
            if self._player.has_treasure():
                old_x, old_y = self._player.position
                treasure_placed = False
                while not treasure_placed:
                    new_treasure_x = max(min(old_x + random.randint(-2, 2), 9), 0)
                    new_treasure_y = max(min(old_y + random.randint(-2, 2), 9), 0)
                    new_treasure_pos = (new_treasure_x, new_treasure_y)
                    new_room = self._maze[new_treasure_pos]
                    if not new_room.has_feature():
                        new_room._type = Room.TYPE_TREASURE
                        treasure_placed = True

            new_x = random.randint(0, 9)
            new_y = random.randint(0, 9)
            self._player._pos = (new_x, new_y)
        elif room.has_exit():
            pass
        room.reset_feature()

    def game_ended(self):
        return self._player.has_treasure() and \
               self._player.position == self._maze.get_exit_point()

    def notify_next_player(self):
        if self._player._steps <= 0:
            self._player._steps += 3

    def notify_first_player(self):
        self._player._steps = 3
Exemplo n.º 43
0
class Move_Manager(object):
    def __init__(self):
        self._tw_pub = rospy.Publisher(TWIST_PUB, Twist)
        self._rate = rospy.Rate(RATE)
        self._checks = {
         Direction.FORWARD: None,\
         Direction.LEFT: None,\
         Direction.RIGHT: None,\
         "ARRAY": None\
        }
        self._sense_subs = [\
         rospy.Subscriber(BUMPER_IO, TurtlebotSensorState, self._collision),\
         rospy.Subscriber(SCAN_IO, Float64MultiArray, self._scan)\
        ]
        self.maze = Maze()
        self._last_collision = None

    def debug(self, a=1):
        print(self._checks["ARRAY"])

    # 1: right
    # 2: left
    # 3: both
    # just record that we hit something
    def _collision(self, data):
        # TODO
        return
        collisions = data.bumps_wheeldrops
        if collisions == 0 or self._last_collision is not None:
            return
        self.stop()
        self._send_twist(-COLLISION_X, 0)
        self._last_collision = collisions

    # executed when we have the chance
    def _handle_collision(self):
        self.maze.collision()
        self._last_collision = None

    # LaserScan callback
    def _scan(self, data):
        arr = data.data
        self._checks[Direction.RIGHT] = arr[0]
        self._checks[Direction.FORWARD] = arr[1]
        self._checks[Direction.LEFT] = arr[2]
        self._checks["ARRAY"] = arr

    # TODO: buggy
    # try to adjust the left and right side to be equidistant from an imaginary wall
    def center(self, count=1):
        if  self._checks["ARRAY"] is None or \
         self._checks[Direction.FORWARD] > MIN_DIST or \
         count > CENTER_MAX_COUNT or\
         not DO_CENTER:
            return

        num = count + 1
        diff = self._checks[Direction.LEFT] - self._checks[Direction.RIGHT]

        # avoid adjustements that are too big (possibly just bad detection)
        # avoid minute adjustements
        if  abs(diff) > CENTER_MAX_COUNT * CENTER_INC or \
         abs(diff) < CENTER_TURN_DIST:
            return

        # HACK
        # guessing here, but we need to make a better decision here
        # should the robot turn towards or away from a wall?
        coeff = 1
        if abs(diff) > CENTER_MAX_COUNT * CENTER_INC / 2:
            coeff = -1

        self._send_twist(CENTER_FORWARD, 0)
        self._send_twist(0, coeff * CENTER_INC)
        self.center(count=num)

    # TODO
    # we don't want to be too close to a wall, we want to be in the center of a "cell"
    def not_too_close(self, count=1):
        if self._checks[Direction.FORWARD] < TOO_CLOSE and \
         count < CLOSE_MAX_COUNT and \
         DO_NOT_GET_CLOSE:

            self._send_twist(-BACKWARDS_X(CLOSE_INC), 0)
            c = count + 1
            self.not_too_close(count=c)

    # turning is left to the callee!
    # if True, then the robot has enough room to perform the directional movement
    def check(self, direction, num=1):
        if direction == Direction.FORWARD:
            self.center()
        # check that we have room
        if self._checks[Direction.FORWARD] > MIN_DIST:
            # ok but are the sides confident?
            if not DO_GET_EDGE or self._checks[direction] > MIN_DIST:
                rospy.loginfo(self._checks)
                return True
            else:
                ## need to correct,
                ## lets nudge forward since we might be looking at a corner
                # if we are trying to go forward then this check is irrelevant
                if direction == Direction.FORWARD:
                    rospy.loginfo(self._checks)
                    return False
                # when middle says we are good
                # but the sides have something in front!
                elif direction == Direction.LEFT:
                    if self._checks[direction] < MIN_DIST:
                        self.move(Direction.RIGHT)
                        self.move(Direction.FORWARD, scale=CHECK_SCALE)
                        self.move(Direction.LEFT)
                    else:
                        self.move(Direction.LEFT)
                        self.move(Direction.FORWARD, scale=CHECK_SCALE)
                        self.move(Direction.RIGHT)
                    return self.check(direction)
                # ^^
                elif direction == Direction.RIGHT:
                    if self._checks[direction] < MIN_DIST:
                        self.move(Direction.LEFT)
                        self.move(Direction.FORWARD, scale=CHECK_SCALE)
                        self.move(Direction.RIGHT)
                    else:
                        self.move(Direction.RIGHT)
                        self.move(Direction.FORWARD, scale=CHECK_SCALE)
                        self.move(Direction.LEFT)
                    return self.check(direction)
            rospy.loginfo(self._checks)
            return True
        else:
            rospy.loginfo(self._checks)
            return False

    # we always turn orthogonally so we won't ask for z input
    def move(self, direction, scale=1):
        if self._last_collision is not None:
            self._handle_collision()

        if direction == Turn.CLOCKWISE:
            direction = Direction.RIGHT
        if direction == Turn.COUNTER:
            direction = Direction.LEFT

        # NOTE this should be the only place where we delay after movement
        if direction == Direction.FORWARD:
            self._send_twist(TWIST_X / scale, 0)
            self.maze.step()
            self.center()
            self.not_too_close()
        elif direction == Direction.BACKWARD:
            # NOTE: backward is not just -TWIST_X
            self._send_twist(-BACKWARD(MAX_DIST) / scale, 0)
        elif direction == Direction.RIGHT or direction == Direction.LEFT:
            self._turn(direction)
            self.maze.turn(direction)
            self.center()
        else:
            rospy.loginfo("invalid direction to move() " + str(direction))

    # halt movement of the turtlebot immediately
    def stop(self):
        twist = Twist()
        self._tw_pub.publish(twist)

    def _turn(self, direction):
        # an orthogonal turn on a flat surface
        # HACK right turns suck
        val = -(TWIST_Z * RIGHT_SCALE) if direction == Direction.RIGHT else (
            LEFT_SCALE) * TWIST_Z
        self._send_twist(0, val)

    # actually send message
    # similar to what is found in turtlebot min_max tutorial
    def _send_twist(self, x, z):
        for _ in range(TWIST_NUM):
            twist = Twist()
            twist.linear.x = x
            twist.angular.z = z
            self._tw_pub.publish(twist)
            self._rate.sleep()
        self.stop()
        rospy.sleep(DELAY)
Exemplo n.º 44
0
    else:
        print('Incorrect run option\nTerminating')
        exit(0)

    print("Input file: ", image_path)
    print("Start coordinate: (" + str(x_start) + ', ' + str(y_start) + ')')
    print("End coordinate: (" + str(x_end) + ', ' + str(y_end) + ')')
    file_check = Path(image_path)

    # check that file exits
    if not file_check.is_file():
        print("File does not exist, terminating")
        exit()

    print('-----------------------------')
    user_maze = Maze(image_path, (x_start, y_start), (x_end, y_end),
                     show_history)

    if run_BFS:
        bfs_solution_image, bfs_duration, bfs_number_nodes = user_maze.solve_bfs(
        )
        if bfs_number_nodes > 0:
            print(
                f'BFS algorithm found a solution in {bfs_duration:.3f} seconds'
            )
            print(f'By visiting {bfs_number_nodes} nodes')
            if not save:
                bfs_solution_image.show()
            else:
                # save image
                save_path = 'maze_images/generated_solutions/' + image_path.split(
                    '/')[-1].split('.')[0] + '_solutionBFS.png'
Exemplo n.º 45
0
from BlindrobotProblem import BlindrobotProblem
from Maze import Maze
from astar_search import astar_search


# null heuristic, useful for testing astar search without heuristic (uniform cost search).
def null_heuristic(state):
    return 0

# test_maze2 = Maze("maze2.maz")
# print test_maze2
# test_mp = BlindrobotProblem(test_maze2, (2, 2))
# result = astar_search(test_mp, test_mp.setsize_heuristic)
# print result
# test_mp.animate_path(result.path)

test_maze07 = Maze("maze07.maz")
print test_maze07
test_mp = BlindrobotProblem(test_maze07, (1, 0))
result = astar_search(test_mp, test_mp.setsize_heuristic)
print result
test_mp.animate_path(result.path)

Exemplo n.º 46
0
    def GenerateMaze(self, width, height, weights=None):
        # force weights to null for now

        if weights is None:
            use_weights = False
            weights = [1, 1, 1, 1]
        else:
            use_weights = True

        theMaze = Maze()
        theMaze.SetDimensions(width, height)

        # REWRITTEN AS OF 6/23
        used_cells = np.zeros([height, width])
        walls = []

        # depth first starts at top left
        current_cell = [0, 0]
        used_cells[current_cell[0], current_cell[1]] = 1

        # create adjacency list
        next_walls = []
        borders = theMaze.GetBorders(current_cell)
        for index in xrange(len(borders)):
            if borders[index] == 0:
                new_cell = deepcopy(current_cell)
                if index == 0:
                    new_cell[0] -= 1
                elif index == 1:
                    new_cell[1] += 1
                elif index == 2:
                    new_cell[0] += 1
                elif index == 3:
                    new_cell[1] -= 1
                next_walls.append(
                    [weights[index], current_cell, new_cell, index])

        if not use_weights:
            while len(next_walls) > 0:
                wall_idx = randint(0, len(next_walls) - 1)
                wall = next_walls[wall_idx]
                next_walls.pop(wall_idx)
                walls.append(wall)
        else:
            next_walls_heaped = []
            while len(next_walls) > 0:
                index, wall = ValueFromHeap(next_walls)
                next_walls_heaped.append(wall)
            next_walls_heaped.reverse()
            walls += next_walls_heaped
        # print "FIRST SET", adjacent_cells, maze_cells
        while len(walls) > 0:
            wall = walls[-1]
            walls = walls[:-1]

            if self.mVerbosity > 0:
                print "active wall (weight, from, to, direction): ", wall

            # ensure the cell this wall refers to is not in the maze
            prior_cell = wall[1]
            current_cell = wall[2]

            if used_cells[current_cell[0], current_cell[1]] == 1:
                if self.mVerbosity > 0:
                    print "    to cell in maze"
                continue

            used_cells[current_cell[0], current_cell[1]] = 1

            # open the wall
            wallToOpen = wall[3]
            rWallToOpen = (wallToOpen + 2) % 4
            theMaze.cells[prior_cell[0], prior_cell[1], wallToOpen] = 1
            theMaze.cells[current_cell[0], current_cell[1], rWallToOpen] = 1

            # create adjacency list
            next_walls = []
            borders = theMaze.GetBorders(current_cell)
            for index in xrange(len(borders)):
                if borders[index] == 0:
                    new_cell = deepcopy(current_cell)
                    if index == 0:
                        new_cell[0] -= 1
                    elif index == 1:
                        new_cell[1] += 1
                    elif index == 2:
                        new_cell[0] += 1
                    elif index == 3:
                        new_cell[1] -= 1
                    next_walls.append(
                        [weights[index], current_cell, new_cell, index])

            if not use_weights:
                while len(next_walls) > 0:
                    wall_idx = randint(0, len(next_walls) - 1)
                    wall = next_walls[wall_idx]
                    next_walls.pop(wall_idx)
                    walls.append(wall)
            else:
                next_walls_heaped = []
                while len(next_walls) > 0:
                    index, wall = ValueFromHeap(next_walls)
                    next_walls_heaped.append(wall)
                next_walls_heaped.reverse()
                walls += next_walls_heaped

        theMaze.end_cell = [0, 0]
        theMaze.start_cell = [theMaze.height - 1, theMaze.width - 1]
        return theMaze
Exemplo n.º 47
0
    if maze[startRow][startCol] == TRIED:
        return False
    # 3. Success, an outside edge not occupied by an obstacle
    if maze.isExit(startRow, startCol):
        maze.updatePosition(startRow, startCol, PART_OF_PATH)
        return True
    maze.updatePosition(startRow, startCol, TRIED)

    # Otherwise, use logical short circuiting to try each direction in turn
    # if needed.
    found = searchFrom(maze, startRow - 1, startCol) or \
            searchFrom(maze, startRow + 1, startCol) or \
            searchFrom(maze, startRow, startCol - 1) or \
            searchFrom(maze, startRow, startCol + 1)

    if found:
        maze.updatePosition(startRow, startCol, PART_OF_PATH)
    else:
        maze.updatePosition(startRow, startCol, DEAD_END)
    return found


mazeFile = "text_files/maze.txt"
myMaze = Maze(mazeFile)
myMaze.drawMaze()
# Set turtle object to start point when executing search function.
myMaze.updatePosition(myMaze.startRow, myMaze.startCol)
searchFrom(myMaze, myMaze.startRow, myMaze.startCol)
myMaze.wn.exitonclick()

Exemplo n.º 48
0
    def GenerateMaze(self, width, height, weights=None):
        if weights is None:
            use_weights = False
            weights = [1, 1, 1, 1]
        else:
            use_weights = True
        theMaze = Maze()
        theMaze.SetDimensions(width, height)

        # REWRITTEN AS OF 6/23
        # I wrote this from a cell list which prevented me from using bias terms and weights. I've rectified that
        used_cells = np.zeros([height, width])
        walls = []

        # choose a start cell
        current_cell = [
            np.random.randint(0, theMaze.height),
            np.random.randint(0, theMaze.width)
        ]
        used_cells[current_cell[0], current_cell[1]] = 1

        # create adjacency list
        borders = theMaze.GetBorders(current_cell)
        for index in xrange(len(borders)):
            if borders[index] == 0:
                new_cell = deepcopy(current_cell)
                if index == 0:
                    new_cell[0] -= 1
                elif index == 1:
                    new_cell[1] += 1
                elif index == 2:
                    new_cell[0] += 1
                elif index == 3:
                    new_cell[1] -= 1
                walls.append([weights[index], current_cell, new_cell, index])

        # print "FIRST SET", adjacent_cells, maze_cells
        while len(walls) > 0:
            # choose a wall to open
            if use_weights == False:
                wall_idx = randint(0, len(walls) - 1)
                wall = walls[wall_idx]
                walls.pop(wall_idx)

            else:
                index, wall = ValueFromHeap(walls)

            if self.mVerbosity > 0:
                print "active wall (weight, from, to, direction): ", wall

            # ensure the cell this wall refers to is not in the maze
            prior_cell = wall[1]
            current_cell = wall[2]

            if used_cells[current_cell[0], current_cell[1]] == 1:
                if self.mVerbosity > 0:
                    print "    to cell in maze"
                continue

            used_cells[current_cell[0], current_cell[1]] = 1

            # open the wall
            wallToOpen = wall[3]
            rWallToOpen = (wallToOpen + 2) % 4
            theMaze.cells[prior_cell[0], prior_cell[1], wallToOpen] = 1
            theMaze.cells[current_cell[0], current_cell[1], rWallToOpen] = 1

            # create adjacency list
            borders = theMaze.GetBorders(current_cell)
            for index in xrange(len(borders)):
                if borders[index] == 0:
                    new_cell = deepcopy(current_cell)
                    if index == 0:
                        new_cell[0] -= 1
                    elif index == 1:
                        new_cell[1] += 1
                    elif index == 2:
                        new_cell[0] += 1
                    elif index == 3:
                        new_cell[1] -= 1
                    walls.append(
                        [weights[index], current_cell, new_cell, index])

        theMaze.end_cell = [0, 0]
        theMaze.start_cell = [theMaze.height - 1, theMaze.width - 1]
        return theMaze
Exemplo n.º 49
0
        Main doesn't contain any method. They are all to be found either in Maze or Items modules

    Attributes in main:
        Main doesn't contain any attributes. They are all to be found either in Maze, or Items, or Config modules

    This program is based on Python 3.7.4 and Pygame 1.9.3
"""
import pygame
import Items
import Config
from Maze import Maze

pygame.init()

# Creates an instance to open a file containing the maze and build it up subsequently
maze = Maze()
maze.draw_maze(Config.maze_level)
maze.display_objects()

# Updates the maze_window
pygame.display.flip()

# Keeps the window open until intentionally closed
running = True
while running:
    # This loop goes through the 3 main phases of this game: movement, collect of the objects, exit the maze
    maze.macgyver.move(maze.walls, maze.window)
    maze.macgyver.collecting_item(
        maze.window, maze.objects_to_collect, maze.my_font)
    maze.macgyver.meeting_warden(
        maze.warden, maze.window, maze.my_font_end_game)
Exemplo n.º 50
0
from MazeworldProblem import MazeworldProblem
from MazeworldWithTimeFactorProblem import MazeworldProblemWithTimeFactor
from Maze import Maze

from astar_search import astar_search


# null heuristic, useful for testing astar search without heuristic (uniform cost search).
def null_heuristic(state):
    return 0


# Test problems

test_maze3 = Maze("maze3.maz")
test_mp = MazeworldProblem(test_maze3, (1, 4, 1, 3, 1, 2))

# print(test_mp.get_successors(test_mp.start_state))

# this should explore a lot of nodes; it's just uniform-cost search
result = astar_search(test_mp, null_heuristic)
print(result)

# this should do a bit better:
result = astar_search(test_mp, test_mp.manhattan_heuristic)
print(result)
# test_mp.animate_path(result.path)

# Your additional tests here:
maze_four = Maze("maze4corridor.maz")
test_corridor = MazeworldProblem(maze_four, (2, 4, 2, 5, 2, 6))
Exemplo n.º 51
0
from Runner import Runner

pygame.init()

# ------ Display ------
dis_width = 600
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
dis.fill((255, 255, 255))
pygame.display.set_caption("Maze Path Finder")

clock = pygame.time.Clock()
fps = 360

# ------ Maze ------
maze = Maze(dis, dis_width)
start = maze.get_start()
maze.solve(start[0], start[1])
print(start)

# ------ Runners ------
runner = Runner((start[1] * maze.block_size) + (maze.block_size / 2), (start[0] * maze.block_size) + (maze.block_size / 2), 10)

count = 0
def gameLoop(count):
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        dis.fill((255, 255, 255))
Exemplo n.º 52
0
 def __init__(self):
     self.maze = Maze()
     self.pacman = PacMan(1,1,self.maze)
Exemplo n.º 53
0
 def generateMazeList():
     mazeList = list()
     for i in range(0, Demo.limit):
         mazeList.append(Maze())
     return mazeList
Exemplo n.º 54
0
mazeseed = [None]
mazeseed = goodmazeseed
seednum = 0
pygame.init()

#mymazerunner = Mazerunner()

black = (0, 0, 0)
grey = (50, 50, 50)
green = (0, 200, 0)
red = (200, 0, 0)
yellow = (255, 255, 0)

screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Random Maze')
mymaze = Maze(screen, 41, 21, seed=mazeseed[seednum])
runner = Mazerunner(mymaze)

print(mymaze)

playing = True
while playing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playing = False
    screen.fill(black)
    mymaze.draw()
    if runner is not None:
        r = runner.draw()
    if r == -1:
        seednum += 1
Exemplo n.º 55
0
__author__ = 'rswoody'

from Maze import Maze

#generate 5 mazes
total_mazes = 5

maze_index = 0

while maze_index < total_mazes:
    maze = Maze(10, 10)
    maze.generate()

    maze.print_maze("maze" + str(maze_index + 1))

    maze_index += 1

print("Complete")
Exemplo n.º 56
0
            self.updateH(CLOSED, goal)
            if OPEN.isEmpty():  # open list is empty
                print("Cannot reach target")
                m.pprint(ZZ)
                return
            path = self.reversePath(start, goal)
            zero = path[0]
            self.moveTo(zero)
            ZZ[zero.x][zero.y] = 1
            self.lookAround(zero, m)
            last = None
            for s in path[1:]:  # moves agent, goes in proper order and checks for isBlocked
                if not s.isBlocked:
                    self.moveTo(s)
                    ZZ[s.x][s.y] = 1
                    m.pprint(ZZ, close=True)
                    self.lookAround(s, m)
                    last = s
                else:
                    break
            if last is not None and goal.x != last.x and goal.y != last.y:
                start = last
        print("target reached")
        m.pprint(ZZ)


if __name__ == "__main__":
    m = Maze()
    agent = aAgent(m.sx, m.sy, m.gx, m.gy)
    agent.adaptiveA(m)
Exemplo n.º 57
0
 def __init__(self, width, height):
     self.__maze = Maze(width, height)
Exemplo n.º 58
0
             'weight': 'normal',
             'size': 22}

    plt.ylabel('Average Throughput', font2)
    plt.xlabel('Training Episodes', font2)
    plt.savefig('average_migration.eps', bbox_inches='tight')
    plt.show()

    """    
    plt.figure(1)
    plt.plot(np.arange(len(remark)), remark)
    plt.xlabel("Total Episodes")
    plt.ylabel("Average Return")
    plt.figure(2)
    plt.plot(np.arange(len(average_migration)), average_migration)
    plt.xlabel("Total Episodes")
    plt.ylabel("Average Throughput")
    plt.show()
    """

    print('game over')
    env.destroy()


if __name__ == "__main__":
    env = Maze()
    RL = Dream(actions=list(range(env.n_actions)))

    env.after(100, update)
    env.mainloop()
Exemplo n.º 59
0
    plt.show()
    """

    env.reset_uav()
    env.render()
    end = time.time()
    print("game over!")
    print('运行时间:', end - start)
    engine = pyttsx3.init()
    engine.say('程序运行完成')
    engine.runAndWait()
    env.destory()


if __name__ == "__main__":
    env = Maze()
    RL1 = DeepQNetwork1(env.n_actions, env.n_features,
                        learning_rate=0.01,
                        reward_decay=0.9,
                        e_greedy=0.9,
                        replace_target_iter=200,  # 尝试减少替换次数
                        memory_size=2000,  # 尝试扩大记忆库
                        output_graph=False
                        )

    RL2 = DeepQNetwork2(env.n_actions, env.n_features,
                        learning_rate=0.01,
                        reward_decay=0.9,
                        e_greedy=0.9,
                        replace_target_iter=180,  # 尝试减少替换次数
                        memory_size=1800,  # 尝试扩大记忆库
Exemplo n.º 60
0
class Game(StateMachine):

    def __init__(self,gridSize=32,frameRate=60):
        super(Game,self).__init__('TheGame.You Lost It.')

        pygame.init()

        self.framerate = frameRate
        self.maze = Maze(gridSize)
        self.maze.read("theMaze")
        self.screen = pygame.display.set_mode(self.maze.rect.size,0,32)
        self.clock = pygame.time.Clock()
        self.paused = False

        self.add(AttractMode(self))
        self.add(SetupLevelMode(self))
        self.add(PlayingMode(self))
        self.add(IntermissionMode(self))
        self.add(GameOverMode(self))
        self.setStateByName(PlayingMode.name)


    @property
    def time(self):
        ms = self.clock.tick(self.framerate)
        return ms / 1000.0

    @property
    def font(self):
        try:
            return self._font
        except AttributeError:
            pass
        self._font = pygame.font.Font(FONTNAME,self.maze.gridWidth)
        return self._font

    @property
    def highScoreLabel(self):
        try:
            return self._highScoreLabel
        except AttributeError:
            pass
        self._highScoreLabel = self.font.render("HIGH SCORE", True, (255,255,255),(0,0,0))
        return self._highScoreLabel

    @property
    def oneUpLabel(self):
        try:
            return self._oneUpLabel
        except AttributeError:
            pass
        self._oneUpLabel = self.font.render("1UP", True, (255,255,255),(0,0,0))
        return self._oneUpLabel

    @property
    def twoUpLabel(self):
        try:
            return self._twoUpLabel
        except AttributeError:
            pass
        self._twoUpLabel = self.font.render("2UP", True, (255,255,255),(0,0,0))
        return self._twoUpLabel

    @property
    def readyLabel(self):
        try:
            return self._readyLabel
        except AttributeError:
            pass
        self._readyLabel = self.font.render("Ready!", True, (255,255,0),(0,0,0))
        return self._readyLabel


    def start(self):
        pass

    def up(self):
        self.maze.puckman.newHeading = UP

    def down(self):
        self.maze.puckman.newHeading = DOWN

    def left(self):
        self.maze.puckman.newHeading = LEFT

    def right(self):
        self.maze.puckman.newHeading = RIGHT

    def pause(self):
        self.paused = True

    def unpause(self):
        self.paused = False

    def run(self):
        while True:
            self.think()

    def quit(self):
        print "quitting"
        exit()

    def drawHighScore(self):
        lrect = self.highScoreLabel.get_rect()
        srect = self.screen.get_rect()
        lrect.midtop = srect.midtop
        self.screen.blit(self.highScoreLabel,lrect.topleft)

    def draw1UpScore(self,score):
        r = self.screen.get_rect()
        scoreText = self.font.render("%d" % score, True, (255,255,0),(0,0,0))
        lrect = scoreText.get_rect()

        x,y = r.midtop

        x /= 4
        y += self.maze.gridWidth

        lrect.midtop = (x,y)
        self.screen.blit(scoreText,lrect.topleft)
        

    def draw1Up(self):
        lrect = self.oneUpLabel.get_rect()
        srect = self.screen.get_rect()
        x,y = srect.midtop
        x /= 2
        lrect.midtop = (x,y)
        self.screen.blit(self.oneUpLabel,lrect.topleft)
        

    def draw2Up(self):
        pass

    def drawFruit(self):
        pass

    def update(self):
        if self.paused:
            self.time
        else:
            self.maze.update(self.time)

    def draw(self):

        self.maze.draw(self.screen)

        self.drawHighScore()
        self.draw1Up()
        self.draw1UpScore(self.maze.puckman.score)
        self.draw2Up()
        self.drawFruit()