Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import os,re,telnetlib
  2. from collections import namedtuple
  3.  
  4. Dir = namedtuple("Dir", ["char", "dy", "dx"])
  5. tn = telnetlib.Telnet()
  6.  
  7. class Maze:
  8.    
  9.     START = "S"
  10.     END   = "E"
  11.     WALL  = "#"
  12.     PATH  = " "
  13.     OPEN  = {PATH, END}  # map locations you can move to (not WALL or already explored)
  14.  
  15.     RIGHT = Dir("D",  0,  1)
  16.     DOWN  = Dir("X",  1,  0)
  17.     LEFT  = Dir("A",  0, -1)
  18.     UP    = Dir("W", -1,  0)
  19.     DIRS  = [RIGHT, DOWN, LEFT, UP]
  20.  
  21.     movelist = "";
  22.  
  23.     @classmethod
  24.     def load_maze(cls, fname):
  25.         with open(fname) as inf:
  26.             lines = (line.rstrip("\r\n") for line in inf)
  27.             maze  = [list(line) for line in lines]
  28.         return cls(maze)
  29.  
  30.     def __init__(self, maze):
  31.         self.maze = maze
  32.  
  33.     def __str__(self):
  34.         return "\n".join(''.join(line) for line in self.maze)
  35.  
  36.     def find_start(self):
  37.         for y,line in enumerate(self.maze):
  38.             try:
  39.                 x = line.index("S")
  40.                 return y, x
  41.             except ValueError:
  42.                 pass
  43.  
  44.         # not found!
  45.         raise ValueError("Start location not found")
  46.  
  47.     def solve(self, y, x):
  48.         if self.maze[y][x] == Maze.END:
  49.             # base case - endpoint has been found
  50.             return True
  51.         else:
  52.             # search recursively in each direction from here
  53.             for dir in Maze.DIRS:
  54.                 ny, nx = y + dir.dy, x + dir.dx
  55.                 if self.maze[ny][nx] in Maze.OPEN:  # can I go this way?
  56.                     if self.maze[y][x] != Maze.START: # don't overwrite Maze.START
  57.  
  58.                         self.maze[y][x] = dir.char  # mark direction chosen
  59.  
  60.                     if self.solve(ny, nx):          # recurse...
  61.                         self.movelist += dir.char
  62.                         return True                 # solution found!
  63.  
  64.             # no solution found from this location
  65.             if self.maze[y][x] != Maze.START:       # don't overwrite Maze.START
  66.                 self.maze[y][x] = Maze.PATH         # clear failed search from map
  67.             return False
  68.  
  69. def main():
  70.  
  71.     host = "bufferoverflow.disappointedmama.com"
  72.     port = 9000
  73.  
  74.     tn.open(host, port)
  75.  
  76.     os.remove("test.txt")
  77.     os.remove("checkback.txt");
  78.  
  79.     while True:
  80.         line = tn.read_until("\n", 5)
  81.         if ( "[]" ) not in line:
  82.             break
  83.        
  84.         origLine = line;
  85.         line = line.replace( "  ", " " ).replace( "[]", "#" ).replace( "<>", "S" ).replace( "######################################### #", "#########################################E#" )
  86.        
  87.         with open("test.txt", "a") as myfile:
  88.             myfile.write(line)
  89.  
  90.         with open("checkback.txt", "a") as myfile:
  91.             myfile.write(origLine)
  92.    
  93.     maze = Maze.load_maze("test.txt")
  94.  
  95.     print("Maze loaded:")
  96.     print(maze)
  97.  
  98.     try:
  99.         sy, sx = maze.find_start()
  100.         print("solving...")
  101.         if maze.solve(sy, sx):
  102.            
  103.             print(maze)
  104.             print(maze.movelist.replace("X", "S")[::-1])
  105.            
  106.            
  107.         else:
  108.             print("no solution found")
  109.     except ValueError:
  110.         print("No start point found.")
  111.  
  112. if __name__=="__main__":
  113.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement