Skip to main content
 首页 » 编程设计

python之如何随机生成 ASCII 艺术地 block

2025年01月19日10dflying

我正在尝试使用 Python 随机生成以 ASCII 艺术表示的陆地 block 。我有以下代码,它们要么主要产生水,要么主要产生土地。我怎样才能实现我的最终目标?

from random import * 
 
water = "." 
land = "#" 
 
waterChance = 40 
landChance = 100 - waterChance 
 
width = 40 
height = 20 
 
prevTile = water 
 
level = [] 
 
for i in range(height): 
    level += [[]] 
    for j in range(width): 
 
        # change to ternary  
        if prevTile == water: 
            waterChance += 10 
        elif prevTile == land: 
            waterChance -= 10 
 
        if(waterChance == 50): 
            waterChance += 5 
 
        tile = randrange(1,101) 
 
        if tile > waterChance: 
            print(land, end='') 
            prevTile = land 
        elif tile <= waterChance: 
            print(water, end='') 
            prevTile = water 
    print() 

我正在寻找类似的东西(X 替换为#s,空格替换为连字符。抱歉,我不得不使用 ASCIIFlow 来快速完成此操作)

    XXX 
    XXXXX 
   XXXXXXX 
   XXXXXXXX         X 
    XXXXXXX      XXXX 
        XX       XXXXXX 
                 XXXXXXX 
                  XXXXXXX 
               XXXXXXXXXX 
  XXXXXX       XXXXXXX 
 XXXXXXX 
XXXXXXXX 
XXXXXXXXXXX 
 XXXXXXXXXXXXXX 
  XX XXXXXXXXXXXX 
  XXXXXXXXXXXXXXX 
   XXXXXXXXXXXXXXX 
   XXXXXXXXXXXXXXXXXX 
   XXXXXXXXXXXXXXXX 
    XXXXXXXXXXXX 

请您参考如下方法:

这是一个 loss function用于连接。

将总损失作为土地与水的比率 + 一些常数乘以连通性。现在您有了 map “优度”的度量标准。 map 生成器中的所有这些硬编码值都是参数。执行 grid search超过这些参数以获得高“优”值。

这将问题简化为调整一个值,即损失函数中的常量。这是可取的,因为现在用户可以权衡连接性和土地与水的比例。

编辑 这是第一个链接中的连通性函数。

计算 bool 二维矩阵中岛屿的程序

class Graph: 
  def __init__(self, row, col, g): 
    self.ROW = row 
    self.COL = col 
    self.graph = g 
 
  # A function to check if a given cell  
  # (row, col) can be included in DFS 
  def isSafe(self, i, j, visited): 
    # row number is in range, column number 
    # is in range and value is 1  
    # and not yet visited 
    return (i >= 0 and i < self.ROW and 
            j >= 0 and j < self.COL and 
            not visited[i][j] and self.graph[i][j]) 
 
 
  # A utility function to do DFS for a 2D  
  # boolean matrix. It only considers 
  # the 8 neighbours as adjacent vertices 
  def DFS(self, i, j, visited): 
 
    # These arrays are used to get row and  
    # column numbers of 8 neighbours  
    # of a given cell 
    rowNbr = [-1, -1, -1,  0, 0,  1, 1, 1]; 
        colNbr = [-1,  0,  1, -1, 1, -1, 0, 1]; 
 
    # Mark this cell as visited 
    visited[i][j] = True 
 
    # Recur for all connected neighbours 
    for k in range(8): 
        if self.isSafe(i + rowNbr[k], j + colNbr[k], visited): 
            self.DFS(i + rowNbr[k], j + colNbr[k], visited) 
 
 
  # The main function that returns 
  # count of islands in a given boolean 
  # 2D matrix 
  def countIslands(self): 
    # Make a bool array to mark visited cells. 
    # Initially all cells are unvisited 
    visited = [[False for j in range(self.COL)]for i in range(self.ROW)] 
 
    # Initialize count as 0 and travese  
    # through the all cells of 
    # given matrix 
    count = 0 
    for i in range(self.ROW): 
        for j in range(self.COL): 
            # If a cell with value 1 is not visited yet,  
            # then new island found 
            if visited[i][j] == False and self.graph[i][j] ==1: 
                # Visit all cells in this island  
                # and increment island count 
                self.DFS(i, j, visited) 
                count += 1 
 
    return count 
 
 graph = [[1, 1, 0, 0, 0], 
    [0, 1, 0, 0, 1], 
    [1, 0, 0, 1, 1], 
    [0, 0, 0, 0, 0], 
    [1, 0, 1, 0, 1]] 
 
 
row = len(graph) 
col = len(graph[0]) 
 
g= Graph(row, col, graph) 
 
print "Number of islands is :" 
print g.countIslands()