Skip to content

Instantly share code, notes, and snippets.

@yangruihan
Created July 11, 2018 06:35
Show Gist options
  • Select an option

  • Save yangruihan/1804caadba61169650d7f55dd4af7309 to your computer and use it in GitHub Desktop.

Select an option

Save yangruihan/1804caadba61169650d7f55dd4af7309 to your computer and use it in GitHub Desktop.
细胞自动机生成随机地图
#!/usr/bin/env python3
from random import *
DIR = [(-1, 0), (0, -1), (1, 0), (0, 1)]
def R(arr, x, y, n = 1):
global DIR
ret = 0
for t in range(1, n + 1):
m = t * 2
pos = (x + t, y + t)
for current_dir in DIR:
count = 0
while count < m:
pos = (pos[0] + current_dir[0], pos[1] + current_dir[1])
ret += 1 if is_wall(arr, pos[0], pos[1]) else 0
count += 1
return ret
def is_wall(arr, x, y):
if x >= 0 and y >= 0 and len(arr) > y and len(arr[y]) > x:
return arr[y][x] == 1
return True
def random_generate_map(arr, n):
for i in range(n):
arr.append([])
for j in range(n):
arr[i].append(randint(0, 1))
def print_map(arr):
for i in range(len(arr)):
for j in range(len(arr[i])):
print(arr[i][j], end=' ')
print()
def main():
arr = []
n = 40 # 地图大小
random_generate_map(arr, n)
print('Origin Map:')
print_map(arr)
print('\n\n\n')
for t in range(5): # 迭代10次
target = arr.copy()
arr = []
for i in range(n):
arr.append([])
for j in range(n):
if R(target, j, i, 1) >= 5 or R(target, j, i, 2) <= 4:
arr[i].append(1)
else:
arr[i].append(0)
print_map(arr)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment