Visual_Basic 板


LINE

应用所学写了一只程式 让它可以自己产生迷宫 用乱数产生墙 用滑鼠点击修改墙 清空墙 重新设定墙 等等功能 想问问各位前辈还有没有可以优化的地方 BUG在请人玩了以後应该是没有甚麽BUG了 另外inputbox不输入东西按X会整个程式跳掉有办法解决吗@@ 这是包装好的程式:http://ppt.cc/!NPc 以下原始码: form1.frm Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public xd As Integer Public yd As Integer Const bound As Integer = 1500 '边界值 Public num As Long '格子数 Public seed As Long '种子数 Dim map() As Obj Private Sub Command1_Click() ans Command1.Enabled = False End Sub Private Sub Command2_Click() seed = InputBox("请输入障碍物数量") Do While 1 > seed Or seed > num * num - 1 MsgBox "输入有误 数字限制在1~" & num * num - 1 seed = InputBox("请输入障碍物数量") Loop ReDim map(num, num) showMap getRndMap seed Command1.Enabled = True End Sub Private Sub Command3_Click() num = InputBox("请输入大小(1~200)") Do While 1 > num Or num > 200 MsgBox "输入有误 数字限制在1~" & 200 num = InputBox("请输入大小(1~200)") Loop seed = num * num * 0.3 xd = (Form1.ScaleWidth - bound * 2) / num yd = (Form1.ScaleHeight - bound * 2) / num ReDim map(num, num) showMap getRndMap seed Command1.Enabled = True End Sub Private Sub Command4_Click() ReDim map(num, num) getRndMap seed Command1.Enabled = True End Sub Private Sub Command5_Click() Form2.Show End Sub Private Sub Command6_Click() ReDim map(num, num) Cls showMap Command1.Enabled = True End Sub Private Sub custom_Click() If custom.Tag = 0 Then custom.Caption = "停用自订障碍" custom.Tag = 1 Else custom.Caption = "启用自订障碍" custom.Tag = 0 End If End Sub Private Sub Form_Activate() initNum showMap getRndMap seed End Sub Sub initNum() seed = 20 num = 10 xd = (Form1.ScaleWidth - bound * 2) / num yd = (Form1.ScaleHeight - bound * 2) / num ReDim map(num, num) End Sub Sub showMap() For cnt = 0 To num Line (bound, bound + yd * cnt)-(bound + xd * num, bound + yd * cnt) Line (bound + xd * cnt, bound)-(bound + xd * cnt, bound + yd * num) Next End Sub Sub showObj(ByVal yoffset As Integer, ByVal xoffset As Integer, ByVal rgb_ As Long) '显示物件 offset起始值0 Line (bound + xd * xoffset, bound + yd * yoffset)- _ (bound + xd * (xoffset + 1), bound + yd * (yoffset + 1)), rgb_, BF Line (bound + xd * xoffset, bound + yd * yoffset)- _ (bound + xd * (xoffset + 1), bound + yd * (yoffset + 1)), RGB(0, 0, 0), B End Sub Function isWall(ByVal localSet As Long, ByVal nextSet As String) As Boolean y = localSet \ num x = localSet Mod num Select Case nextSet Case "r" 'right If x = num - 1 Then isWall = True: Exit Function If map(y, x + 1).wall = 1 Or map(y, x + 1).route = 1 Then isWall = True: Exit Function Case "l" 'left If x = 0 Then isWall = True: Exit Function If map(y, x - 1).wall = 1 Or map(y, x - 1).route = 1 Then isWall = True: Exit Function Case "t" 'top If y = 0 Then isWall = True: Exit Function If map(y - 1, x).wall = 1 Or map(y - 1, x).route = 1 Then isWall = True: Exit Function Case "b" 'bottom If y = num - 1 Then isWall = True: Exit Function If map(y + 1, x).wall = 1 Or map(y + 1, x).route = 1 Then isWall = True: Exit Function End Select End Function Sub getRndMap(ByVal seedNum As Long) Randomize Cls totalN = num * num ReDim numArr(totalN - 1) As Long ReDim buff(seedNum) As Long For i = LBound(numArr) To UBound(numArr) numArr(i) = i Next For j = 0 To seedNum - 1 totalN = totalN - 1 tmpRnd = Int(Rnd * totalN + 1) buff(j) = numArr(tmpRnd) numArr(tmpRnd) = numArr(totalN) Next For k = LBound(buff) To UBound(buff) - 1 map(buff(k) \ num, buff(k) Mod num).wall = 1 showObj buff(k) \ num, buff(k) Mod num, RGB(0, 0, 255) Next showMap End Sub Sub ans() Dim top As Long Dim ptr As Long tmp = num * num ReDim stack(tmp) As Long top = 0 ptr = 0 map(0, 0).route = 1 showObj 0, 0, RGB(0, 255, 100) Do While ptr <> num * num - 1 If Not isWall(ptr, "r") Then showObj (ptr \ num), (ptr Mod num), RGB(0, 255, 100) map(ptr \ num, ptr Mod num).route = 1 stack(top) = ptr top = top + 1 ptr = ptr + 1 ElseIf Not isWall(ptr, "b") Then showObj (ptr \ num), (ptr Mod num), RGB(0, 255, 100) map(ptr \ num, ptr Mod num).route = 1 stack(top) = ptr top = top + 1 ptr = ptr + num ElseIf Not isWall(ptr, "l") Then showObj (ptr \ num), (ptr Mod num), RGB(0, 255, 100) map(ptr \ num, ptr Mod num).route = 1 stack(top) = ptr top = top + 1 ptr = ptr - 1 ElseIf Not isWall(ptr, "t") Then showObj (ptr \ num), (ptr Mod num), RGB(0, 255, 100) map(ptr \ num, ptr Mod num).route = 1 stack(top) = ptr top = top + 1 ptr = ptr - num ElseIf ptr = 0 Then '四面都是墙时检查是否无解 MsgBox "无解": GoTo EXIT_LABEL Else '走到死路 使用backtracking showObj (ptr \ num), (ptr Mod num), Form1.BackColor map(ptr \ num, ptr Mod num).route = 1 top = top - 1 ptr = stack(top) map(ptr \ num, ptr Mod num).route = 0 End If Loop showObj num - 1, num - 1, RGB(0, 255, 100) MsgBox "Find it" EXIT_LABEL: ReDim stack(tmp) ReDim map(num, num) End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If (x > bound And x < Form1.ScaleWidth - bound) And (y > bound And y < Form1.ScaleHeight - bound) And custom.Tag = 1 Then If map((y - bound) \ yd, (x - bound) \ xd).wall = 0 Then map((y - bound) \ yd, (x - bound) \ xd).wall = 1 showObj (y - bound) \ yd, (x - bound) \ xd, RGB(0, 0, 255) Else map((y - bound) \ yd, (x - bound) \ xd).wall = 0 showObj (y - bound) \ yd, (x - bound) \ xd, Form1.BackColor End If End If End Sub Module1.bas Type Obj route As Integer wall As Integer End Type --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.240.28.247
1F:→ x000032001:http://nopaste.csie.org/73129 04/30 23:23
2F:推 jwjeng:有加上找出路的功能吗? 抱歉我没开来看啦 :) 05/01 10:05
3F:→ x000032001:有@@ 05/01 11:45
4F:推 ClubT:关於inputbox可以先用 IsNumeric这函数判断inputbox结果是否 05/04 09:55
5F:→ ClubT:是数字,如果是再执行之後的程式 05/04 09:55
6F:→ ClubT:另外迷宫应该要加个"起点"和"终点"的标示 05/04 09:56
7F:→ x000032001:现在才看到 感谢建议@@ 05/10 20:30







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP