4programmers.net

Witaj!

tBane dodał nowy post w wątku: Dungeons Generator - Generator Lochów


Cześć. Piszę generator lochów. Program działa w ten sposób, że generuje rekurencyjnie pokoje (kwadratowe obszary) w taki sposób, że każdy z pokoi ma przynajmniej jednego sąsiada. Ogólnie wszystko ok do momentu, aż trzeba połączyć pokoje. Wymyśliłem dwie metody jak rozwiązać ten problem.

Kopiuj
........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ...#######.............................................. ...#+++++#.............................................. ...#+++++#.............................................. ...#######.............................................. ......................############...................... ......................#++++++++++#...................... ......................#++++++++++#...................... ......#########.......#++++++++++#...................... ......#+++++++#.......#++++++++++#...................... ......#+++++++#.......#++++++++++#...................... ......#+++++++#.......############...................... ......#########......................................... ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ 
  1. wyszukiwanie ścieżki z jednego pokoju do drugiego z wykorzystaniem algorytmu astar - ale wtedy korytarz przylega do ściany a to brzydko wygląda
Kopiuj
........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ...#######.............................................. ...#+++++#.............................................. ...#+++++#.............................................. ...#######.............................................. ......#####...........############...................... ..........#...........#++++++++++#...................... ..........#...........#++++++++++#...................... ......#################++++++++++#...................... ......#+++++++##......#++++++++++#...................... ......#+++++++##......#++++++++++#...................... ......#+++++++#.......############...................... ......#########......................................... ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ 
  1. 16 pętli for ... bo dla każdego z kierunków. Algorytm ma działać w ten sposób, że wyznacza punkt środkowy między pokojami a następnie z pomocą czterech pętli for pokoje są łączone w taki sposób, że ścieżka przechodzi przez ten punkt środkowy
Kopiuj
........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ...#######.............................................. ...#+++++#.............................................. ...#+++++#.............................................. ...#######.............................................. ......#...............############...................... ......#####...........#++++++++++#...................... ..........#...........#++++++++++#...................... ......#########...#####++++++++++#...................... ......#+++++++#...#...#++++++++++#...................... ......#+++++++#####...#++++++++++#...................... ......#+++++++#.......############...................... ......#########......................................... ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ ........................................................ 

Jak Wy byście rozwiązali ten problem ?

Kopiuj
import numpy as np import heapq import time  map_wdt = 56 map_hgh = 32 mapa = np.empty((map_hgh, map_wdt), dtype=str)  empty_char = '.' wall_char = '#' floor_char = '+'  ##############################  def generate_room(x,y,w,h,iter): if iter > 0 and w>=4 and h>=4 and mapa[y][x] != floor_char: sx = int(x-w/2) sy = int(y-h/2)  if sx < 0 or sy < 0 or sx+int(w)>=map_wdt or sy+int(h)>=map_hgh: return  for yy in range(sy, sy+int(h)): for xx in range(sx, sx+int(w)): if xx>=0 and yy>=0 and xx<map_wdt and yy<map_hgh: mapa[yy][xx] = floor_char  neighbour = np.random.choice([1,2,3,4]) xx : int yy : int  if neighbour == 1: #top neighbour xx = np.random.randint(-4,4) yy = np.random.randint(h*5/4, h*6/4) if neighbour == 2: #right neighbour xx = np.random.randint(w*5/4, w*6/4) yy = np.random.randint(-4,4) if neighbour == 3: #bottom neighbour xx = np.random.randint(-4,4) yy = -np.random.randint(h*5/4, h*6/4) if neighbour == 4: #left neighbour xx = -np.random.randint(w*5/4, w*6/4) yy = np.random.randint(-4,4)   rx = x + xx ry = y + yy  if rx>=0 and ry>=0 and rx<map_wdt and ry<map_hgh:  rw = np.random.uniform(w*3/4, w*5/6) rh = np.random.uniform(h*3/4, h*5/6) generate_room(rx, ry, rw, rh, iter-1) ##############################  def char_in_range(char, r, x, y): for yy in range(y-r, y+r+1): for xx in range(x-r, x+r+1): if xx>=0 and yy>=0 and xx<map_wdt and yy<map_hgh and mapa[yy][xx] == char: return True  return False  def add_walls(): for y in range(map_hgh): for x in range(map_wdt): if mapa[y][x]==floor_char and char_in_range(empty_char, 1, x, y): mapa[y][x] = wall_char  ##############################  def clear_map(): for y in range(map_hgh): for x in range(map_wdt): mapa[y][x] = empty_char  def draw_map(): for y in range(map_hgh): for x in range(map_wdt): print(mapa[y][x], end='') print()  ##############################  while True: clear_map() x = int(map_wdt/2) y = int(map_hgh/2) w = int(map_wdt*2/9) h = int(map_hgh*2/9) generate_room(x,y,w,h,3) add_walls() draw_map() print("\n\n") time.sleep(1); 


Zobacz post

Z poważaniem,
4programmers.net

Jeżeli masz problemy z kliknięciem na przycisk "Zobacz post", skopiuj i wklej poniższy link w przeglądarce:

http://4programmers.net/Notification?path=%252FForum%252FPython%252F376485-dungeons_generator_generator_lochow%253Fp%253D2003336%2523id2003336