Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.13 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import random, os, time
  4.  
  5. WALL_CHAR = '#'
  6. FLOOR_CHAR = '.'
  7. PLAYER_CHAR = '@'
  8. EXIT_CHAR = 'X'
  9. COIN_CHAR = '*'
  10. ENEMY_CHAR = 'a'
  11. WATER_CHAR = '~'
  12.  
  13. def random_fill():
  14.     if random.randint(0, 100) < 28:
  15.         return WALL_CHAR
  16.     else:
  17.         return FLOOR_CHAR
  18.  
  19. def count_walls_around(arr, i, j, n):
  20.     counter = 0
  21.     diff = [-n+i for i in range(2*n+1)]
  22.     for k in diff:
  23.         for m in diff:
  24. #            if k==0 and m ==0:
  25. #               continue
  26.             try:
  27.                 if arr[i+k][j+m] == WALL_CHAR:
  28.                     counter += 1
  29.             except IndexError:
  30.                 continue
  31.     return counter
  32.  
  33. def dist(x1, y1, x2, y2):
  34.     return abs(x2-x1)+abs(y2-y1)
  35.  
  36. def return_max_region(regions):
  37.     if len(regions)==1:
  38.         return regions[0]
  39.     else:
  40.         lens = [len(i) for i in regions]
  41.         maxlen = max(lens)
  42.         return regions[lens.index(maxlen)]
  43.  
  44. class Dungeon:
  45.     def __init__(self, n, m):
  46.         self.height, self.width = n, m
  47.         self.marray = [[WALL_CHAR]*self.width for _ in range(self.height)]
  48.         self.update()
  49.  
  50.     def update(self):
  51.         self.sarray = [''.join(i) for i in self.marray]
  52.    
  53.     def make_borders(self):
  54.         for i in range(1, self.height-1):
  55.             for j in range(1, self.width-1):
  56.                 self.marray[i][j] = FLOOR_CHAR
  57.         self.update()
  58.    
  59.     def iterate_CA(self):
  60.         for i in range(1, self.height-1):        
  61.             for j in range(1, self.width-1):
  62.                 c1 = count_walls_around(self.marray, i, j, 1)
  63.                 c2 = count_walls_around(self.marray, i, j, 2)
  64.                 if c1 > 4 or c2 < 3:
  65.                     self.marray[i][j] = WALL_CHAR
  66.  
  67.    
  68.     def iterate_CA_s(self):
  69.         for i in range(1, self.height-1):
  70.             for j in range(1, self.width-1):
  71.                 c1 = count_walls_around(self.marray, i, j, 1)
  72.                 if c1 < 3:              
  73.                     self.marray[i][j] = FLOOR_CHAR
  74.  
  75.     def make_CA(self):
  76.         #random initialisation
  77.         for i in range(1, self.height-1):
  78.             for j in range(1, self.width-1):
  79.                 self.marray[i][j] = random_fill()
  80. #        self.make_borders()
  81.         self.update()
  82.         self.show_dungeon()
  83.         for i in range(5):
  84.             self.iterate_CA()
  85.             self.update()
  86.             self.show_dungeon()
  87.         for i in range(5):
  88.            self.iterate_CA_s()
  89.            self.update
  90.            self.show_dungeon()
  91.  
  92.     def floor_coords(self):
  93.         self.floor = []
  94.         for i in range(1, self.height-1):
  95.             for j in range(1, self.width-1):
  96.                 if self.marray[i][j] not in (WALL_CHAR, WATER_CHAR):
  97.                     self.floor.append((i, j))
  98.  
  99.     def set_player_exit(self):
  100.         x, y = self.max_region[0]
  101.         self.marray[x][y] = PLAYER_CHAR
  102.         x, y = self.max_region[-1]
  103.         self.marray[x][y] = EXIT_CHAR
  104.         self.update()
  105.         self.show_dungeon()
  106.  
  107.     def set_coin(self):
  108.         u_floor = self.max_region[1:-1]
  109.         for i,j in u_floor:
  110.             r = random.randint(0,100)
  111.             if r < 20:
  112.                 self.marray[i][j] = COIN_CHAR  
  113.         self.update()
  114.         self.show_dungeon()
  115.  
  116.     def set_enemy(self):
  117.         x1, y1 = self.max_region[-1]
  118.         if 300 < len(self.max_region):
  119.             for i, j in self.max_region:
  120.                 if dist(i, j, x1, y1) == 7:
  121.                     self.marray[i][j] = ENEMY_CHAR
  122.                     break
  123.         if len(self.max_region) > 400:
  124.              x1, y1 = self.max_region[0]
  125.              for i, j in self.max_region:
  126.                 if dist(i, j, x1, y1) == 11:
  127.                     self.marray[i][j] = ENEMY_CHAR
  128.                     break
  129.         self.update()
  130.         self.show_dungeon()
  131.  
  132.     def show_dungeon(self):
  133.         time.sleep(0.05)
  134.        #os.system('clear')
  135.         for i in self.sarray:
  136.             print(i)
  137.  
  138.     def is_water_nearby(self, i, j):
  139.         if self.marray[i][j-1] == WATER_CHAR or self.marray[i][j+1] == WATER_CHAR or self.marray[i-1][j] == WATER_CHAR or self.marray[i+1][j] == WATER_CHAR:
  140.             return True
  141.    
  142.     def iterate_water(self):
  143.         for i, j in self.floor:
  144.             if self.is_water_nearby(i, j):
  145.                 self.marray[i][j] = WATER_CHAR
  146.         self.floor_coords()
  147.         self.update()
  148.         self.show_dungeon()
  149.  
  150.     def count_water(self):
  151.         counter = 0
  152.         for i in range(self.height):
  153.             for j in range(self.width):
  154.                 if self.marray[i][j] == WATER_CHAR:
  155.                     counter += 1
  156.         return counter
  157.  
  158.    
  159.     def set_regions(self):
  160.         regions = []
  161.         used_cells = []
  162.         while self.floor:
  163.             i, j = self.floor[0]
  164.             self.marray[i][j] = WATER_CHAR
  165.             counter = 1
  166.             done = False
  167.             while not done:
  168.                 self.iterate_water()
  169.                 new_counter = self.count_water()
  170.                 if counter == new_counter:
  171.                     done = True
  172.                 else:
  173.                     counter = new_counter
  174.             region = []
  175.             for i in range(self.height):
  176.                 for j in range(self.width):
  177.                     if self.marray[i][j] == WATER_CHAR and (i,j) not in used_cells:
  178.                         region.append((i,j))
  179.             regions.append(region.copy())
  180.             used_cells = [item for sublist in regions for item in sublist]
  181.         self.max_region = return_max_region(regions)
  182.         for i in range(self.height):
  183.             for j in range(self.width):
  184.                 if self.marray[i][j] == WATER_CHAR:
  185.                     self.marray[i][j] = FLOOR_CHAR
  186.         self.update()
  187.         self.show_dungeon()
  188.         self.max_region = return_max_region(regions)
  189.         print(self.max_region)
  190.        
  191. n, m = 30, 90
  192. dungeon = Dungeon(n, m)
  193. filename = 'init.txt'
  194. dungeon.make_CA()
  195. dungeon.floor_coords()
  196. dungeon.set_regions()
  197. dungeon.set_player_exit()
  198. dungeon.set_coin()
  199. dungeon.set_enemy()
  200.  
  201. with open(filename, 'w') as f:
  202.     for i in dungeon.sarray:
  203.         f.write(i+'\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement