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?
import random as rd
nearmaybe=[
[-2,0],
[2,0],
[0,-2],
[0,2]
]
def createMaze(row,col):
maze=[[0 for i in range(col)] for i in range(row)]
check=[]
firstrow=rd.randrange(1,row-2,2)
firstcol=rd.randrange(1,col-2,2)
maze[firstrow][firstcol]=1
check.append([firstrow,firstcol])
while len(check):
c=rd.choice(check)
nears=[]
conditions=[]
for maybe in nearmaybe:
conditions.append([c[0]+maybe[0],c[1]+maybe[1]])
for condition in conditions:
if condition[0]>=1 and condition[0]<=row-2 \
and condition[1]>=1 and condition[1]<=col-2:
nears.append([condition[0],condition[1]])
for n in nears.copy():
if maze[n[0]][n[1]]:
nears.remove(n)
for block in nears:
if block[0]==c[0]:
if block[1]<c[1]:
maze[block[0]][c[1]-1]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
else:
maze[block[0]][block[1]-1]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
else:
if block[0]<c[0]:
maze[c[0]-1][block[1]]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
else:
maze[block[0]-1][block[1]]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
if not len(nears):
check.remove(c)
maze[1][1]="s"
while True:
c=rd.randint(1,col-2)
if maze[row-2][c]:
maze[row-2][c]="e"
break
return maze
調(diào)用該函數(shù)的方法:
maze=createMaze(27,27)
for l in maze:
for s in l:
print(s,end=" ")
print()
CreateMaze傳入的兩個(gè)參數(shù)必須都是單數(shù),否則程序可能會(huì)無法運(yùn)行!
下面的for循環(huán)遍歷就是將生成的迷宮矩陣打印出來
S表示出發(fā)點(diǎn)
1表示路
0表示墻
E表示終點(diǎn)
生成迷宮矩陣后,就可以將其引用到游戲中去啦!
原文鏈接:https://blog.csdn.net/leleprogrammer/article/details/124205148
相關(guān)推薦
- 2022-11-26 python?布爾注入原理及滲透過程示例_python
- 2023-01-03 Kotlin?Thread線程與UI更新詳解_Android
- 2022-09-29 解決react組件渲染兩次的問題_React
- 2022-06-15 go語(yǔ)言context包功能及操作使用詳解_Golang
- 2023-08-15 vite打包報(bào)錯(cuò) Rollup failed to resolve
- 2022-05-20 springboot心跳機(jī)制,定時(shí)任務(wù)
- 2022-12-01 關(guān)于Linux之grep查找文本時(shí)匹配反斜杠\轉(zhuǎn)義問題_linux shell
- 2022-03-13 C語(yǔ)言實(shí)現(xiàn)求最大公約數(shù)的三種方法_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支