2005-10-05, 03:10 PM | #1 |
註冊會員
|
請問哪裡有老鼠走迷宮的程式相關資料?
因為學校要交作業~所以我在找相關資料~希望有的大大能分享一下~感恩喔~
|
送花文章: 23,
|
2005-10-05, 11:44 PM | #2 (permalink) |
註冊會員
|
/* =============== Program Description =============== */
/* 程式名稱: maze.c */ /* 程式目的: 運用遞迴來解迷宮問題 */ /* Written By Kuo-Yu Huang. (WANT Studio.) */ /* =================================================== */ int Maze[8][7] = { /* 宣告5*4的迷宮,外圍不可走 */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}; /* --------------------------------------------------- */ /* 遞迴解迷宮問題 */ /* --------------------------------------------------- */ int Way(int LocX,int LocY) { if ( Maze[6][5] == 2 ) /* 遞迴結束條件 */ return 1; else /* 遞迴執行部分 */ if ( Maze[LocY][LocX] == 0 ) { Maze[LocY][LocX] = 2; if ( Way(LocX,LocY-1) ) return 1; else if ( Way(LocX+1,LocY-1) ) return 1; else if ( Way(LocX+1,LocY) ) return 1; else if ( Way(LocX+1,LocY+1) ) return 1; else if ( Way(LocX,LocY+1) ) return 1; else if ( Way(LocX-1,LocY+1) ) return 1; else if ( Way(LocX-1,LocY) ) return 1; else if ( Way(LocX-1,LocY-1) ) return 1; else { Maze[LocY][LocX] = 3; return 0; } } else return 0; } /* --------------------------------------------------- */ /* 主程式 */ /* --------------------------------------------------- */ void main () { int i,j; /* 迴圈計數變數 */ printf("==Problem of Maze ==\n"); printf("The Maze source is (1,1).\n"); printf("The Maze Destination is (6,5).\n"); Way(1,1); printf("The graph of Maze.\n"); printf(" 0 1 2 3 4 5 6 \n"); printf(" +---+---+---+---+---+---+---+\n"); for (i=0;i<8;i++) { printf(" %d |",i); for (j=0;j<7;j++) printf("-%d-|",Maze[i][j]); printf("\n +---+---+---+---+---+---+---+\n"); } } |
送花文章: 623,
|
2005-11-02, 01:25 AM | #5 (permalink) | |
註冊會員
|
引用:
有哪位大大可以把這個翻成VB呢??....小弟剛好要做程式報告需要的..... 感恩喔 |
|
送花文章: 112,
|
2005-11-02, 10:50 PM | #7 (permalink) |
註冊會員
|
那版主...我想到了一個想法!!!
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Static Xinit, Yinit As Integer If Xinit = 0 And Yinit = 0 Then Xinit = X Yinit = Y Exit Sub End If Line (Xinit, Yinit)-(X, Y) Xinit = X Yinit = Y If Button = 2 Then Cls Xinit = 0 Yinit = 0 End If End Sub 這個可以改成"點下去以後有一個picturebox在移動" |
送花文章: 112,
|
2005-11-03, 11:45 AM | #8 (permalink) |
管理版主
|
幫你改了一下
請自行加一個 Shape物件 程式是先跑水平再跑垂直 語法:
Dim XEnd, YEnd As Integer Dim ShapeSpeedX, ShapeSpeedY As Integer 'X Y 移動值可配合方向指標做 ShapeSpeedX=-ShapeSpeedX 變化 Dim OverX, OverY As Boolean 'Dim Direction(1 To 4) As Boolean '方向指標: **請配合障礙判斷式使用** Private Sub Form_Load() Form1.AutoRedraw = True Timer1.Interval = 100 XEnd = 0 YEnd = 0 End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If XEnd = 0 And YEnd = 0 Then XEnd = X YEnd = Y OverX = False OverY = False ShapeSpeedX = InputBox("請輸入移動速度", "老鼠移動速度", Shape1.Width / 2) ShapeSpeedY = ShapeSpeedX If XEnd < Shape1.Left Then ShapeSpeedX = -ShapeSpeedX If YEnd < Shape1.Top Then ShapeSpeedY = -ShapeSpeedY Form1.DrawWidth = 3 Me.PSet (XEnd, YEnd) Me.Print " <=終點" Form1.DrawWidth = 1 Timer1.Enabled = True Exit Sub End If If Button = 2 Then Timer1.Enabled = False Me.Cls XEnd = 0 YEnd = 0 End If End Sub '移動 Shape1 Private Sub Timer1_Timer() Dim XOld, YOld As Integer '未移動前座標 Dim NewPosX, NewPosY As Integer '預測用 XOld = Shape1.Left + Shape1.Width / 2 YOld = Shape1.Top + Shape1.Height / 2 '**請加上遇到障礙時的判斷式** If Not OverX Then NewPosX = Shape1.Left + ShapeSpeedX '新的位址 If ShapeSpeedX > 0 Then If NewPosX >= XEnd Then OverX = True '水平移動終止條件 If Not OverX Then Shape1.Left = NewPosX '移動 If ShapeSpeedX < 0 Then If NewPosX <= XEnd Then OverX = True '水平移動終止條件 '畫出水平移動軌跡 Line (XOld, Shape1.Top + Shape1.Height / 2)-(Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2) Exit Sub '**請配合障礙判斷式修改** End If '**請加上遇到障礙時的判斷式** If Not OverY Then NewPosY = Shape1.Top + ShapeSpeedY '新的位址 If ShapeSpeedY > 0 Then If NewPosY >= YEnd Then OverY = True '垂直移動終止條件 If Not OverY Then Shape1.Top = NewPosY '移動 If ShapeSpeedY < 0 Then If NewPosY <= YEnd Then OverY = True '垂直移動終止條件 '畫出垂直移動軌跡 Line (Shape1.Left + Shape1.Width / 2, YOld)-(Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2) Exit Sub '**請配合障礙判斷式修改** End If '到達後停止 Timer1.Enabled = False End Sub |
送花文章: 2013,
|
2005-11-03, 12:59 PM | #9 (permalink) | |
註冊會員
|
引用:
感謝大大指點~~~但我剛執行了一下出現這樣的訊息方塊 執行階段錯誤'424':此處需要物件~~~我看不懂這句話要表達啥意思!!! 請大大解說一下喔!!!.....笑我沒關係....VB太複雜的比較需要思考 |
|
送花文章: 112,
|
2005-11-03, 01:28 PM | #10 (permalink) |
管理版主
|
到這裡下程式碼
http://d.turboupload.com/d/132474/Mouse.rar.html 解開後執行看看 To 執行階段錯誤'424':此處需要物件 整個程式只有兩個物件 Timer 與 Shape 可能是少哪一個 或直接下上面的程式碼 打開 test1.vbp 專案檔執行 要修改成障礙物迷宮 就要做些許修改及添加程式判斷式 重點在 Dim Direction(1 To 4) As Boolean '方向指標: 1~4依序為 右、上、左、下 的設計 比如 Direction(1)=True 表示右邊無障礙物 此時如欲往右邊走 if ShapeSpeedX <0 then ShapeSpeedX= -ShapeSpeedX '或ShapeSpeedX= Abs(ShapeSpeedX) |
送花文章: 2013,
|
2005-11-04, 09:20 PM | #11 (permalink) |
註冊會員
|
OverX = False
OverY = False If XEnd < Shape1.Left Then ShapeSpeedX = -ShapeSpeedX If YEnd < Shape1.Top Then ShapeSpeedY = -ShapeSpeedY If Button = 2 Then Timer1.Enabled = False Me.Cls XEnd = 0 YEnd = 0 End If 我先問這些好了!!!上述這些事代表啥意思??請大大指點嚕!!! |
送花文章: 112,
|
2005-11-04, 10:08 PM | #12 (permalink) |
管理版主
|
OverX = False
OverY = False 這兩個表示是否超過目的(X,Y)座標 如要改寫成障礙物迷宮 且 目的地(出口) 不是在邊緣 那就不是這麼用了 If XEnd < Shape1.Left Then ShapeSpeedX = -ShapeSpeedX 當XEnd目的地X座標在物件(Shape1)左邊(If XEnd < Shape1.Left) 時 Shape1是向左移動 (物件.Left 是遞減) 所以 ShapeSpeedX 必須為負 (ShapeSpeedX = -ShapeSpeedX) 當然 如要改寫成障礙物迷宮 那必須改寫 (與 遇到障礙時的判斷式 結合) 語法:
If Button = 2 Then Timer1.Enabled = False Me.Cls XEnd = 0 YEnd = 0 End If 語法:
If Button = 2 Then Cls Xinit = 0 Yinit = 0 End If 等有空把比較完整的判斷式完成 鼠一直往前 遇障礙檢查左右 左右也不通 回走 |
送花文章: 2013,
|