史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   請問哪裡有老鼠走迷宮的程式相關資料? (http://forum.slime.com.tw/thread158268.html)

kinco 2005-10-05 03:10 PM

請問哪裡有老鼠走迷宮的程式相關資料?
 
因為學校要交作業~所以我在找相關資料~希望有的大大能分享一下~感恩喔~ :ddrf567h:

snoopy 2005-10-05 11:44 PM

/* =============== 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");
}
}

kinco 2005-10-10 10:19 AM

請問要怎麼隨機畫出 1024*768的地圖?
謝謝大大的指導~

mini 2005-10-11 10:43 AM

引用:

作者: kinco
請問要怎麼隨機畫出 1024*768的地圖?
謝謝大大的指導~

這種東西要自己寫個相對應的程式
一般來講還是要自己畫(設計)路線
不過 1024*768的地圖應該很費神吧

rockman132 2005-11-02 01:25 AM

引用:

作者: snoopy
/* =============== 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");
}
}



有哪位大大可以把這個翻成VB呢??....小弟剛好要做程式報告需要的.....
感恩喔

mini 2005-11-02 08:19 PM

VB是視窗設計軟體
上面那一個程式碼是 類似命令提示字元之 DOS畫面
要改的話
幾乎全部都要重新設計
rockman132版友不知要如何的畫面
這裡只能給你個設計構思

可以直接用 Form 來做顯示畫面的底圖
接著用 Me.Line 方法畫實心磚塊 (寫一個迴圈讀陣列資料畫地圖)
再用另一個 PictureBox代表老鼠在Form上移動(也可用資源使用較低的 Image)
剩下的運作原理就參照上面的 C改成 VB程式碼

rockman132 2005-11-02 10:50 PM

那版主...我想到了一個想法!!!
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在移動"

mini 2005-11-03 11:45 AM

幫你改了一下
請自行加一個 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


rockman132 2005-11-03 12:59 PM

引用:

作者: mini
幫你改了一下
請自行奶竷郎A跑垂直


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


感謝大大指點~~~但我剛執行了一下出現這樣的訊息方塊
執行階段錯誤'424':此處需要物件~~~我看不懂這句話要表達啥意思!!!
請大大解說一下喔!!!.....笑我沒關係....VB太複雜的比較需要思考

mini 2005-11-03 01:28 PM

到這裡下程式碼
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)

rockman132 2005-11-04 09:20 PM

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

我先問這些好了!!!上述這些事代表啥意思??請大大指點嚕!!!

mini 2005-11-04 10:08 PM

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

當按下右鍵時 清除畫面 與 停止Timer1運作

等有空把比較完整的判斷式完成
鼠一直往前
遇障礙檢查左右
左右也不通
回走

rockman132 2005-11-13 04:20 PM

Shape1.Width / 2

Line (Shape1.Left + Shape1.Width / 2, YOld)-(Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2)

這代表啥意思呢??

mini 2005-11-13 08:41 PM

將畫線的座標移到 Shape1 的中心點
http://nov13.imghost.us/tusbmc.jpg

這幾週一有空就在寫完整的程式
其方法是用棋幟標示法
大概下一週可以出來

思考方式如下
http://nov13.imghost.us/tjokrd.jpg

rockman132 2005-11-14 03:19 PM

ㄟ.....大大不好意思....要挑戰你的極限~~~經過老師的指點以後....期末報告要求說做出類似貪食蛇的程式....p.s....要有關卡

或著可以把這程式改造成怎樣的程式呢???


所有時間均為台北時間。現在的時間是 11:46 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1