リカーシブコールの応用

<戻る

ここに載せてあるソースコードは、参考のために載せてあります

サンプルコードは、一番下にLZHとしてあります




Option Explicit

Private Type XyType
    X As Integer
    Y As Integer
End Type

Dim MyStart As XyType, MyExit As XyType, Buf(20, 20) As Integer, BoxSize As Integer, ExitGet As Boolean

Private Sub Command1_Click()
    Command1.Enabled = False
    ExitSearch MyStart.X, MyStart.Y
End Sub

Private Sub Form_Load() '迷路の作成
    Dim txt As String, jx, jy, jBuf
    
    BoxSize = 12 '迷路の壁と道の大きさ
    
    'ファイルから迷路データの読み込み
    Open App.Path & "\Map.dat" For Input As #1
        For jy = 1 To 20
            Input #1, txt
            For jx = 1 To 20
                Buf(jx, jy) = Mid(txt, jx, 1)
            Next
        Next
    Close
    
    '迷路の作成
    For jy = 1 To 20
        For jx = 1 To 20
            jBuf = Buf(jx, jy)
            
            If jBuf = 1 Then
                Picture1.Line ((jx - 1) * BoxSize, (jy - 1) * BoxSize)-(jx * BoxSize, jy * BoxSize), RGB(150, 150, 150), BF
            ElseIf jBuf = 2 Then
                Picture1.Line ((jx - 1) * BoxSize, (jy - 1) * BoxSize)-(jx * BoxSize, jy * BoxSize), vbBlue, BF
                MyStart.X = jx
                MyStart.Y = jy
            ElseIf jBuf = 3 Then
                Picture1.Line ((jx - 1) * BoxSize, (jy - 1) * BoxSize)-(jx * BoxSize, jy * BoxSize), vbRed, BF
                MyExit.X = jx
                MyExit.Y = jy
            Else
                Picture1.Line ((jx - 1) * BoxSize, (jy - 1) * BoxSize)-(jx * BoxSize, jy * BoxSize), vbGreen, BF
            End If
    Next jx, jy
    
End Sub

Sub ExitSearch(ByVal X As Integer, ByVal Y As Integer) 'ゴールの検索
    Dim j As Long
    
    If Buf(X, Y) = 3 Then
        MsgBox "ゴール!"
        ExitGet = True
    End If
    Buf(X, Y) = 1
    Picture1.Line ((X - 1) * BoxSize - 2, (Y - 1) * BoxSize - 2)-(X * BoxSize - 2, Y * BoxSize - 2), RGB(0, 100, 255), BF
    Picture1.Refresh
    For j = 1 To 10000: Next
    If ExitGet = False Then
        If Buf(X + 1, Y) <> 1 Then ExitSearch X + 1, Y
        If Buf(X, Y + 1) <> 1 Then ExitSearch X, Y + 1
        If Buf(X - 1, Y) <> 1 Then ExitSearch X - 1, Y
        If Buf(X, Y - 1) <> 1 Then ExitSearch X, Y - 1
    End If
End Sub



<戻る

Sample13.lzh


http://www.vector.co.jp/authors/VA015521/