Python 如何随机生成ASCII艺术地块?
我尝试使用Python随机生成由ASCII艺术表示的地块。我有下面的代码,它要么主要生成水,要么主要生成土地。我怎样才能实现我的最终目标Python 如何随机生成ASCII艺术地块?,python,random,procedural-generation,Python,Random,Procedural Generation,我尝试使用Python随机生成由ASCII艺术表示的地块。我有下面的代码,它要么主要生成水,要么主要生成土地。我怎样才能实现我的最终目标 from random import * water = "." land = "#" waterChance = 40 landChance = 100 - waterChance width = 40 height = 20 prevTile = water level = [] for i in range(height): leve
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()
这里有一个关于连通性的例子
将总损失视为土地与水的比率+某些常数乘以连通性。现在你有了一个衡量地图“好坏”的标准。地图生成器中所有硬编码的值都是参数。对这些参数执行测试,以获得较高的“优度”值
这将问题简化为调整一个值,即损失函数中的常量。这是可取的,因为现在用户可以权衡连通性和土地与水的比率
编辑
这是第一个链接中的连通性函数
计算布尔二维矩阵中孤岛的程序
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()
类图:
定义初始化(self、row、col、g):
self.ROW=行
self.COL=COL
self.graph=g
#一个函数,用于检查给定单元格
#(行、列)可以包含在DFS中
def isSafe(自我、i、j、访问):
#行号在范围内,列号在范围内
#在范围内,值为1
#还没来过
返回(i>=0,i=0和jlandChance
是否在该代码中执行任何操作?而且,在waterChance
通过100或低于0后,似乎只能获得水或土地。它可能会帮助您将waterChance
值打印为标准输出,或者使用其他调试方法查看它在程序运行时的增长情况。请提供一个或多个所需输出类型的示例。ASCII艺术地块模糊不清-我玩过许多基于ASCII的Roguelike&其中许多在绘制地形方面存在显著差异。@Pikalek更新了我的后随机分形(例如随机科赫曲线)通常用于生成景观。这种分形可以用ascii近似表示。这个答案似乎高度依赖于链接。根据经验,如果回复没有提供足够的信息来解决没有链接的问题,那么您需要提供额外的信息/详细信息。生成ASCII land art的任务非常广泛,并且不接受10行的答案。我已经从一个链接复制并粘贴了代码以解决您的问题,但是实现一个健壮的解决方案需要3倍于此的代码,stackoverflow不是一个代码编写服务。
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()