이전 강의까지해서 미로 만드는 법을 마쳤습니다.

 

이젠 우리가 키보드를 이용해 직접 미로를 풀어봅시다.

 

그 전에 이전 강의까지의 코드입니다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()

	''' 생략 '''
    
	'반복문 추가
	Do Until completeMakeMaze(maze, map_size)
		Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
			
			''' 생략 '''
            
		Loop
        
		'미로와 맞닿은 셀을 찾는 반복문
		found = False
		For i=2 To map_size+1
			For j=2 To map_size+1
				If maze(i,j) = False Then
					If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeBottom).LineStyle = xlNone
						row = i+1 'row값 수정
						col = j 'col갑 수정
						found = True
					ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeTop).LineStyle = xlNone
						row = i-1 'row값 수정
						col = j 'col갑 수정
						found = True
					ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeRight).LineStyle = xlNone
						row = i 'row값 수정
						col = j+1 'col갑 수정
						found = True
					ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeLeft).LineStyle = xlNone
						row = i 'row값 수정
						col = j-1 'col갑 수정
						found = True
					End If
				End If
				If found Then
					Exit For
				End If
			Next j
			If found Then
				Exit For
			End If
		Next i
        
	Loop  '추가된 반복문의 끝
End Sub

Function completeMakeMaze(maze() As Variant, map_size)
	Dim isComplete As boolean
	isComplete = True
	For i=1 To map_size+2
		For j=1 To map_size+2
			If maze(i,j) = False Then
				isComplete = False
			End If
		Next j
	Next i
	completeMakeMaze = isComplete
End Function

 

우리는 Hunt And Kill 알고리즘을 구현하기 전에 조이스틱이란 키보드 이벤트를 미리 만들어 놓았습니다.

 

아래 링크를 참고해 주세요.

 

2020/02/27 - [엑셀 vba 게임] - 엑셀 vba 미로만들기(2) - 키보드 이벤트 구성

 

엑셀 vba 미로만들기(2) - 키보드 이벤트 구성

우리가 만드는 미로에서는 직접 방향키를 이용해 캐릭터를 움직여가며 미로를 풀어나갈 겁니다. 그러기 위해서는 각 방향키를 눌렀을 때 이벤트가 발생하도록 만들어야 합니다. 키보드 이벤트를 발생시키기 위해서..

comgonghakdo.tistory.com

우선 코딩의 장소를 바꿔야 합니다.

 

지금까지의 코딩 장소는 "Module1" 이었지만 키보드 이벤트를 구성하려면 이벤트가 일어나는 "Sheet2"로 옮겨야 합니다.

 

 

"Sheet2"에 아래의 코드를 작성해 줍시다.

 

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
	Dim row, col As Integer
	row = 2
	col = 2
	With Target.Cells
		If .Row = row-1 Then ' 위쪽 방향 키보드를 눌럿을 때
			Call MoveUp
		ElseIf .Row = row+1 Then ' 아래쪽 방향 키보드를 눌럿을 때
			Call MoveDown
		ElseIf .Column = col-1 Then ' 왼쪽 방향 키보드를 눌럿을 때
			Call MoveLeft
		ElseIf .Column = col+1 Then ' 오른쪽 방향 키보드를 눌럿을 때
			Call MoveRight
		End If
		Cells(row, col).Select
	End With
End Sub

 

각각의 키보드 이벤트에 MoveUp, MoveDown, MoveLeft, MoveRight 프로시저를 연결해 줍니다.

 

위의 프로시저들은 아직 구현하지 않은 프로시저들이므로 구현해 봅시다.

 

다시 "Sheet2"에서 "Module1"로 돌아갑시다

 

각각의 이동 프로시저를 구현하기 전에 한 가지 고려해야 할 것이 있습니다.

 

1. 플레이어의 위치는 어떤 변수에서 보관해야 하고,

2. 어떻게 이동 프로시저에 전해줄까

 

이를 해결하기 위해서 전역변수를 사용해야 합니다. 플레이어의 위치를 전역변수로 사용하게 된다면 모든 이동 프로시저에서 공유가 가능하므로 위의 2가지 문제점이 해결됩니다.

 

그럼 코드를 작성해 봅시다.

 

먼저 전역변수 선언부터 해 줍시다. 전역변수 선언은 코드의 맨 위에 해 주어야 합니다.

 

전역변수를 선언해 주는 김에 MakeMaze() 프로시저 안에 있는 지역변수인 map_size도 전역변수로 다시 선언해 줍시다.

 

map_size도 MakeMaze() 프로시저뿐만 아니라 다른 프로시저에서도 이용해야 하기 때문입니다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Option Explicit ' 전역변수 선언
Dim player_row, player_col As Integer
Dim map_size As Integer ' map_size를 전역변수로 선언

Sub MazeMaze()
    
    ' Dim map_size As Integer 삭제!
    
''' 이하 생략 '''

 

player_row 와 player_col 이라는 플레이어 위치를 저장할 전역변수를 선언해 줍니다.

 

그럼 처음으로 전역변수에 값을 넣는 곳은 어디일까요.

 

당연히 미로가 모두 만들어지고 난 후 입니다.

 

그러나 MakeMaze() 프로시저 끝에 미로가 모두 만들어지고 난 후의 코드까지 구현하는 것은 코드가 복잡해질 수 있으니 AfterMakeMaze() 프로시저를 하나 더 만들어 봅시다.

 

Sub AfterMakeMaze()
	
End Sub

이 프로시저에서 해야할 일은 여러가지가 있습니다.

 

1. 플레이어 위치 초기화 시키기

2. 시작 위치와 도착 위치의 셀 배경색 바꾸기

3. 조이스틱 셀 선택하기 (우리의 조이스틱인 2행 2열로 셀 선택을 옮겨야 키보드 이벤트를 이용할 수 있습니다.)

 

그럼 코드로 구현해 봅시다.

 

Sub AfterMakeMaze()

    '플레이어 위치 초기화
    player_row = 5
    player_col = 5
    
    '시작 셀과 도착 셀 배경색 바꾸기
    Cells(5,5).Interior.ColorIndex = 33
    Cells(map_size+4, map_size+4).Interior.ColorIndex = 33
End Sub

 

프로시저를 구현하고 MakeMaze() 프로시저 맨 마지막에 Call AfterMakeMaze 를 추가하는 걸 잊으면 안됩니다!!

 

이젠 각각의 이동 프로시저를 구현해야 합니다.

 

만약 플레이어가 오른쪽 방향키를 누른다면 우선 현재 위치해 있는 셀에서 오른쪽 테두리가 있는지 없는지 확인해야 합니다.

 

오른쪽 테두리가 비어있다면 이동할 수 있으므로 이동 후 셀의 배경색을 33으로 바꾸어 줍니다.

 

또한 플레이어가 이동한 흔적을 남기기 위해 이동 전 셀의 배경색도 바꿔주기로 합시다. 이때는 43으로 바꿉시다.

 

마지막으로 전역변수 player_row 값을 바꾸면 됩니다.

 

코드로 구현해봅시다.

Sub MoveUp()
    If Cells(player_row, player_col).Borders(xlEdgeUp).LineStyle = xlNone Then
        Cells(player_row-1, player_col).Interior.ColorIndex = 33
        Cells(player_row, player_col).Interior.ColorIndex = 43
        player_row = player_row - 1
    End If
End Sub

 

나머지도 구현해 봅시다.

 

Sub MoveDown()
    If Cells(player_row, player_col).Borders(xlEdgeDown).LineStyle <> xlNone Then
        Cells(player_row+1, player_col).Interior.ColorIndex = 33
        Cells(player_row, player_col).Interior.ColorIndex = 43
        player_row = player_row + 1
    End If
End Sub

Sub MoveLeft()
    If Cells(player_row, player_col-1).Borders(xlEdgeLeft).LineStyle <> xlNone Then
        Cells(player_row, player_col-1).Interior.ColorIndex = 33
        Cells(player_row, player_col).Interior.ColorIndex = 43
        player_col = player_col - 1
    End If
End Sub

Sub MoveRight()
    If Cells(player_row, player_col).Borders(xlEdgeRight).LineStyle <> xlNone Then
        Cells(player_row, player_col+1).Interior.ColorIndex = 33
        Cells(player_row, player_col).Interior.ColorIndex = 43
        player_col = player_col + 1
    End If
End Sub

끝났습니다.

 

이젠 미로를 생성하고 직접 미로를 플레이하면 됩니다.

 

이것으로 엑셀 vba 미로만들기를 모두 마치겠습니다~.

 

고생하셨습니다.


WRITTEN BY
컴공학도

,

이전 강의까지는

 

1. 랜덤한 셀에서 갈 수 있을 때까지 미로를 만들며 나아간다.

 

2. 미로가 아니면서 1에서 만든 미로와 맞닿은 셀을 찾는다.

 

까지 만들었습니다.

 

아래의 코드는 이전 강의까지의 코드입니다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()
	Randomize
	worksheets("Sheet2").Select
	Dim map_size As Integer
	Dim maze() As Boolean '동적배열 선언
	Dim row, col As Integer
	Dim found As Boolean
	
	map_size = worksheets("Sheet1").Cells(8,2).value
	ReDim maze(map_size+2, map_size+2) '배열 크기 선언
	
	'배열값 초기화
	For i=1 To map_size+2
		For j=1 To map_size+2
			If i = 1 Or j = 1 Or i = map_size+2 Or j = map_size+2 Then
				maze(i,j) = True
			Else
				maze(i,j) = False
			End If
		Next j
	Next i
	
	'셀을 정사각형으로 만들기
	Range("A1:XFD1048576").ColumnWidth = 1.00 
	Range("A1:XFD1048576").RowHeight = 10.00
    
	'조이스틱을 위한 셀 만들기
	Range(Cells(1,1), Cells(3,3)).ColumnWidth = 0.1 
	Range(Cells(1,1), Cells(3,3)).RowHeight = 1.0
    
	'필요없는 셀은 숨기기
	Rows(map_size+6 & ":" & Rows.Count).EntireRow.Hidden = True
	Range(Cells(4,map_size+6), Cells(map_size+5,Columns.Count)).EntireColumn.Hidden = True
	
	'미로 테두리 그리기
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Interior.ColorIndex = 3
	Range(Cells(3+2, 3+2), Cells(3+map_size+1, 3+map_size+1)).Interior.ColorIndex = 0
	
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Borders.Weight = xlThick
	
	row = Int(Rnd * map_size) + 2
	col = Int(Rnd * map_size) + 2
	maze(row,col) = True
	Cells(3+row, 3+col).Interior.ColorIndex = 6
	
    
	'갈 수 있을 때까지 미로를 만들며 랜덤으로 이동하기
	Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
		move = Int(Rnd * 4) + 1
		Select Case move
			Case 1 '위쪽으로 이동할 때
				If maze(row-1, col) = False Then
					Cells(3+row - 1, 3+col).Borders(xlEdgeBottom).LineStyle = xlNone
					row = row - 1
				End If
			Case 2 '오른족으로 이동할 때
				If maze(row, col+1) = False Then
					Cells(3+row, 3+col + 1).Borders(xlEdgeLeft).LineStyle = xlNone
					col = col + 1
				End If
			Case 3 '왼쪽으로 이동할 때
				If maze(row, col-1) = False Then
					cells(3+row, 3+col - 1).Borders(xlEdgeRight).LineStyle = xlNone
					col = col - 1
				End If
			Case 4 '아래쪽으로 이동할 때
				If maze(row+1, col) = False Then
					Cells(3+row + 1, 3+col).Borders(xlEdgeTop).LineStyle = xlNone
					row = row + 1
				End If
			End Select
		maze(row,col) = True
		Cells(3+row, 3+col).interior.ColorIndex = 6
		Sleep(50) '시간지연 코드
	Loop
	
	'미로와 맞닿은 셀 찾기
	found = False
	For i=2 To map_size+1
		For j=2 To map_size+1
			If maze(i,j) = False Then
				If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				End If
			End If
			If found Then
				Exit For
			End If
		Next j
		If found Then
			Exit For
		End If
	Next i
End Sub

 

이젠 미로와 맞닿은 셀부터 다시 갈 수 있을 때까지 랜덤으로 이동해야 합니다.

 

그러기 위해서는 위의 코드 중 (갈 수 있을 때까지 랜덤으로 이동)을 다시 재사용 해야합니다.

 

결국 논리구조는 

 

갈 수 있을 때까지 랜덤으로 미로를 만들며 이동 -> 미로가 아니면서 미로와 맞닿은 셀 찾고 미로와 연결하기 -> 찾은 셀 부터 다시 랜덤으로 미로를 만들며 이동 -> 미로가 아니면서 미로와 맞닿은 셀 찾고 미로와 연결하기 -> .....

 

으로 반복이 되며 maze() 배열의 모든 값이 True가 될 때까지 실행해야 합니다.

 

먼저 찾은 셀과 미로를 연결해 봅시다.

 

이를 위해서는 찾은 셀의 위쪽에 미로가 있는지, 아래쪽에 미로가 있는지 등의 경우를 구분해서 테두리를 없애주어야 합니다.

 

예를들어 만약 maze(i,j)의 위쪽 maze(i-1,j)가 미로라면(True라면) Cells(3+i,3+j)의 위쪽 테두리를 없애야 합니다.

 

이는 Cells(3+i,3+j).Borders(xlEdgeTop).LineStyle = xlNone 를 통해 위쪽 테두리를 없앨 수 있습니다.

 

다른 경우에서도 테두리를 없애는 코드를 추가해 줍시다.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()

    ''' 생략 '''
	
	found = False
	For i=2 To map_size+1
		For j=2 To map_size+1
			If maze(i,j) = False Then
				If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					Cells(3+i, 3+j).Borders(xlEdgeBottom).LineStyle = xlNone
					found = True
				ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					Cells(3+i, 3+j).Borders(xlEdgeTop).LineStyle = xlNone
					found = True
				ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					Cells(3+i, 3+j).Borders(xlEdgeRight).LineStyle = xlNone
					found = True
				ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					Cells(3+i, 3+j).Borders(xlEdgeLeft).LineStyle = xlNone
					found = True
				End If
			End If
			If found Then
				Exit For
			End If
		Next j
		If found Then
			Exit For
		End If
	Next i
End Sub

 

미로 생성 과정을 보기 위해 시간 지연을 추가하였습니다.

 

위의 사진처럼 I5의 셀이 미로와 연결되는 모습을 볼 수 있습니다.

 

다음에 해야할 것은 찾은 셀부터 다시 미로를 생성해나가는 것입니다. 

 

하지만 생각해보면

 

랜덤한 셀부터 더이상 갈 곳이 없을 때까지 미로를 만드는 것과,

찾은 셀부터 더이상 갈 곳이 없을 때까지 미로를 만드는 것은

 

시작하는 셀이 랜덤한 셀이냐, 특정 셀이냐는 것만 다르지 그 이후의 코드는 같습니다.

 

따라서 반복문을 이용하여 우리가 이미 작성한 코드를 재사용 해 봅시다.

 

우선 반복문이 끝나는 조건은 maze()의 배열이 모두 True 값일 때 입니다. 이를 확인하기 위한 함수 하나를 작성해 줍니다.

 

Function completeMakeMaze(maze() As Variant, map_size)
	Dim isComplete As boolean
	isComplete = True
	For i=1 To map_size+2
		For j=1 To map_size+2
			If maze(i,j) = False Then
				isComplete = False
			End If
		Next j
	Next i
	completeMakeMaze = isComplete
End Function

 

completeMakeMaze라는 함수를 만들었습니다.

 

미로 배열과 미로 크기를 인자로 받아 확인하여 배열값이 하나라도 False라면 False값을 반환합니다.

 

그럼 반복문을 추가하고 조건에 위의 함수를 이용해 봅시다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()

	''' 생략 '''
    
	'반복문 추가
	Do Until completeMakeMaze(maze, map_size)
    	
		'나아가면서 미로를 만드는 반복문
		Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
			
			''' 생략 '''
            
		Loop
        
		'미로와 맞닿은 셀을 찾는 반복문
		found = False
		For i=2 To map_size+1
        	
			''' 생략 '''
            
		Next i
        
	Loop '추가된 반복문의 끝
End Sub

Function completeMakeMaze(maze() As Variant, map_size)
	Dim isComplete As boolean
	isComplete = True
	For i=1 To map_size+2
		For j=1 To map_size+2
			If maze(i,j) = False Then
				isComplete = False
			End If
		Next j
	Next i
	completeMakeMaze = isComplete
End Function

Do Until completeMakeMaze(maze, map_size) 반복문이 두개의 반복문을 감쌌습니다.

 

하나는 더이상 갈 수 없을 때까지 미로를 만드는 반복문

다른 하나는 미로와 맞닿은 셀을 찾는 반복문

 

결국 위에서 말한 언급한 대로 

 

미로를 만들고 -> 미로와 맞닿은 셀을 찾고 -> 찾은 셀부터 미로를 만들고 -> 다시 미로와 맞닿은 셀을 찾고 -> 다시 찾은 셀부터 미로를 만들고 -> .....

 

가 반복이 되면서 결국 모든 셀을 미로로 만들 수 있게 됩니다.

 

여기서 하나 빠트린 것이 있습니다. 분명 위의 코드대로 실행을 시키면 무한 루프에서 빠져나오지 못하게 됩니다.

 

이유는 우리는 그저 반복문만 추가했을 뿐, 찾은 셀부터 다시 미로를 만들도록 하지는 않았습니다.

 

이를 해결하기 위해서 코드 초반에 사용한 두개의 변수, row 와 col 을 이용할 것입니다.

 

우리는 row와 col에 랜덤한 수를 집어넣어 미로를 시작했습니다. 하지만 한번 미로를 만들고, 맞닿은 셀을 찾게 되면 row와 col의 값은 랜덤한 수가 아닌 맞닿은 셀의 주소를 가져야 합니다.

 

코드로 구현해 봅시다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()

	''' 생략 '''
    
	'반복문 추가
	Do Until completeMakeMaze(maze, map_size)
		Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
			
			''' 생략 '''
            
		Loop
        
		'미로와 맞닿은 셀을 찾는 반복문
		found = False
		For i=2 To map_size+1
			For j=2 To map_size+1
				If maze(i,j) = False Then
					If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeBottom).LineStyle = xlNone
						row = i+1 'row값 수정
						col = j 'col갑 수정
						found = True
					ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeTop).LineStyle = xlNone
						row = i-1 'row값 수정
						col = j 'col갑 수정
						found = True
					ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeRight).LineStyle = xlNone
						row = i 'row값 수정
						col = j+1 'col갑 수정
						found = True
					ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
						Cells(3+i, 3+j).Interior.ColorIndex = 6
						Cells(3+i, 3+j).Borders(xlEdgeLeft).LineStyle = xlNone
						row = i 'row값 수정
						col = j-1 'col갑 수정
						found = True
					End If
				End If
				If found Then
					Exit For
				End If
			Next j
			If found Then
				Exit For
			End If
		Next i
        
	Loop  '추가된 반복문의 끝
End Sub

Function completeMakeMaze(maze() As Variant, map_size)
	Dim isComplete As boolean
	isComplete = True
	For i=1 To map_size+2
		For j=1 To map_size+2
			If maze(i,j) = False Then
				isComplete = False
			End If
		Next j
	Next i
	completeMakeMaze = isComplete
End Function

 

맞닿은 셀을 찾을 때마다 row와 col의 값을 수정해 줍니다.

 

이렇게 되면 맨 처음 MakeMaze 를 실행할 때는 row와 col값이 랜덤의 값을 가지지만 그 이후부터는 특정 셀의 값을 가진 채 반복문을 실행하게 됩니다.

 

이걸로 Hunt And Kill 알고리즘 구현이 모두 끝이 났습니다. 이젠 GameStart버튼을 누르면 완전한 미로가 생성되는 걸 볼 수 있습니다.

10 x 10 미로 생성 과정

 

10 x 10의 미로보다 더 큰 사이즈의 미로도 만들 수 있습니다.

 

50 x 50 매로 생성 과정

이것으로 강의를 마치고 다음에는 유저가 직접 미로를 플레이 하도록 만드는 법을 배워보겠습니다.

 

고생하셨습니다.~


WRITTEN BY
컴공학도

,

Hunt And Kill 알고리즘 구현의 두번째 시간입니다.

 

이전 강의까지 우리는 미로 안의 랜덤한 셀을 하나 선택에 더이상 갈 곳이 없을 때까지 미로를 만들며 나아가는 로직을 구현했습니다.

 

아래의 코드가 이전까지 작성한 코드입니다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()
	Randomize
	worksheets("Sheet2").Select
	Dim map_size As Integer
	Dim maze() As Boolean '동적배열 선언
	Dim row, col As Integer
	
	map_size = worksheets("Sheet1").Cells(8,2).value
	ReDim maze(map_size+2, map_size+2) '배열 크기 선언
	
	'배열값 초기화
	For i=1 To map_size+2
		For j=1 To map_size+2
			If i = 1 Or j = 1 Or i = map_size+2 Or j = map_size+2 Then
				maze(i,j) = True
			Else
				maze(i,j) = False
			End If
		Next j
	Next i
	
	'셀을 정사각형으로 만들기
	Range("A1:XFD1048576").ColumnWidth = 1.00 
	Range("A1:XFD1048576").RowHeight = 10.00
    
	'조이스틱을 위한 셀 만들기
	Range(Cells(1,1), Cells(3,3)).ColumnWidth = 0.1 
	Range(Cells(1,1), Cells(3,3)).RowHeight = 1.0
    
	'필요없는 셀은 숨기기
	Rows(map_size+6 & ":" & Rows.Count).EntireRow.Hidden = True
	Range(Cells(4,map_size+6), Cells(map_size+5,Columns.Count)).EntireColumn.Hidden = True
	
	'미로 테두리 그리기
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Interior.ColorIndex = 3
	Range(Cells(3+2, 3+2), Cells(3+map_size+1, 3+map_size+1)).Interior.ColorIndex = 0
	
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Borders.Weight = xlThick
	
	row = Int(Rnd * map_size) + 2
	col = Int(Rnd * map_size) + 2
	maze(row,col) = True
	Cells(3+row, 3+col).Interior.ColorIndex = 6
	MsgBox map_size+1 & " " & row+3 & " " & col+3
	
	Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
		move = Int(Rnd * 4) + 1
		Select Case move
			Case 1 '위쪽으로 이동할 때
				If maze(row-1, col) = False Then
					Cells(3+row - 1, 3+col).Borders(xlEdgeBottom).LineStyle = xlNone
					row = row - 1
				End If
			Case 2 '오른족으로 이동할 때
				If maze(row, col+1) = False Then
					Cells(3+row, 3+col + 1).Borders(xlEdgeLeft).LineStyle = xlNone
					col = col + 1
				End If
			Case 3 '왼쪽으로 이동할 때
				If maze(row, col-1) = False Then
					cells(3+row, 3+col - 1).Borders(xlEdgeRight).LineStyle = xlNone
					col = col - 1
				End If
			Case 4 '아래쪽으로 이동할 때
				If maze(row+1, col) = False Then
					Cells(3+row + 1, 3+col).Borders(xlEdgeTop).LineStyle = xlNone
					row = row + 1
				End If
			End Select
		maze(row,col) = True
		Cells(3+row, 3+col).interior.ColorIndex = 6
		Sleep(50) '시간지연 코드
	Loop
End Sub

 

이번엔 Hunt And Kill이라는 이름처럼 미로가 아닌 곳 중에서 미로와 맞닿아 있는 곳을 찾아서(Hunt) 미로로 만들어 주면(Kill) 됩니다.

 

그럼 미로가 아닌 곳 중에서 왼쪽에서 오른쪽으로, 위쪽에서 아래쪽으로 미로와 맞닿아 있는 셀을 찾아봅시다.

 

우리가 미로와 맞닿아 있는지 알아보려면 확인해야 하는 것이 두가지 있습니다.

 

1. 상하좌우에 maze 배열의 값이 True인가

2. 상하좌우에 maze 배열의 값이 True인 곳이 미로의 테두리인가

 

두번째 조건을 확인하는 이유는 앞서 우리가 maze 배열에서 테두리에 해당하는 부분을 True로 설정하였기 때문입니다.

 

두번째 조건을 확인하지 않는다면 분명 maze(2,2)를 확인할 때 maze(1,2)가 테두리임에도 불구하고 미로와 맞닿아 있다고 판단할 것이므로 예방해주어야 합니다.

 

이를 코드로 구현해 봅시다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()

	''' 이전 강의의 코드 '''
	
	For i=2 To map_size+1
		For j=2 To map_size+1
			If maze(i,j) = False Then
				If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
                    
				ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
                    
				ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
                    
				ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
				End If
			End If
		Next j
	Next i
End Sub

 

i는 행을, j는 열을 뜻하는 변수로 이중 For문을 이용해서 왼쪽에서 오른쪽으로, 위쪽에서 아래쪽으로 훑어줍니다.

 

여기서 i와 j가 2부터 시작하는 이유는 굳이 미로의 테두리까지 확인할 필요가 없기 때문이고 이는 map_size+2가 아닌 map_size+1까지만 확인하는 이유이기도 합니다.

 

maze(i,j)의 값이 False라면 미로가 아닌 셀이고 여기서 If ElseIf 조건문을 통해 미로와 맞닿아 있는지 확인합니다. 조건은 위에서 설명한 것과 같습니다.

 

아래의 사진은 코드 실행 결과입니다.(맵의 크기는 10입니다.)

 

 

실행 결과에서 들어나는 문제점은 미로와 맞닿아있는 모든 셀이 노란색이 되어버리는 것입니다.

 

우리가 원하는 건 왼쪽에서 오른쪽으로, 위쪽에서 아래로의 순서로 확인했을 때 가장 먼저 찾는 셀만을 미로로 만드는 것입니다.

 

따라서 우리가 원하는 셀을 찾으면 이중 For문을 빠져나가는 코드를 추가해 줍시다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()
	Randomize
	worksheets("Sheet2").Select
	Dim map_size As Integer
	Dim maze() As Boolean '동적배열 선언
	Dim row, col As Integer
	Dim found As Boolean
	
	''' 중략 '''
    
	For i=2 To map_size+1
		For j=2 To map_size+1
			If maze(i,j) = False Then
				If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
                    
				ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
                    
				ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
                    
				ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				End If
			End If
			If found Then
				Exit For
			End If
		Next j
		If found Then
			Exit For
		End If
	Next i
End Sub

실행 결과입니다.

Boolean형의 found 변수를 활용해 셀을 찾는다면 Exit를 이용해서 For문을 빠져나오도록 했습니다.

 

이것으로 Hunt And Kill 알고리즘의 두번째인 미로와 맞닿아 있는 부분을 찾는 것을 구현해 보았습니다.

 

아래의 코드는 지금까지의 코드입니다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()
	Randomize
	worksheets("Sheet2").Select
	Dim map_size As Integer
	Dim maze() As Boolean '동적배열 선언
	Dim row, col As Integer
	Dim found As Boolean
	
	map_size = worksheets("Sheet1").Cells(8,2).value
	ReDim maze(map_size+2, map_size+2) '배열 크기 선언
	
	'배열값 초기화
	For i=1 To map_size+2
		For j=1 To map_size+2
			If i = 1 Or j = 1 Or i = map_size+2 Or j = map_size+2 Then
				maze(i,j) = True
			Else
				maze(i,j) = False
			End If
		Next j
	Next i
	
	'셀을 정사각형으로 만들기
	Range("A1:XFD1048576").ColumnWidth = 1.00 
	Range("A1:XFD1048576").RowHeight = 10.00
    
	'조이스틱을 위한 셀 만들기
	Range(Cells(1,1), Cells(3,3)).ColumnWidth = 0.1 
	Range(Cells(1,1), Cells(3,3)).RowHeight = 1.0
    
	'필요없는 셀은 숨기기
	Rows(map_size+6 & ":" & Rows.Count).EntireRow.Hidden = True
	Range(Cells(4,map_size+6), Cells(map_size+5,Columns.Count)).EntireColumn.Hidden = True
	
	'미로 테두리 그리기
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Interior.ColorIndex = 3
	Range(Cells(3+2, 3+2), Cells(3+map_size+1, 3+map_size+1)).Interior.ColorIndex = 0
	
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Borders.Weight = xlThick
	
	row = Int(Rnd * map_size) + 2
	col = Int(Rnd * map_size) + 2
	maze(row,col) = True
	Cells(3+row, 3+col).Interior.ColorIndex = 6
	
	Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
		move = Int(Rnd * 4) + 1
		Select Case move
			Case 1 '위쪽으로 이동할 때
				If maze(row-1, col) = False Then
					Cells(3+row - 1, 3+col).Borders(xlEdgeBottom).LineStyle = xlNone
					row = row - 1
				End If
			Case 2 '오른족으로 이동할 때
				If maze(row, col+1) = False Then
					Cells(3+row, 3+col + 1).Borders(xlEdgeLeft).LineStyle = xlNone
					col = col + 1
				End If
			Case 3 '왼쪽으로 이동할 때
				If maze(row, col-1) = False Then
					cells(3+row, 3+col - 1).Borders(xlEdgeRight).LineStyle = xlNone
					col = col - 1
				End If
			Case 4 '아래쪽으로 이동할 때
				If maze(row+1, col) = False Then
					Cells(3+row + 1, 3+col).Borders(xlEdgeTop).LineStyle = xlNone
					row = row + 1
				End If
			End Select
		maze(row,col) = True
		Cells(3+row, 3+col).interior.ColorIndex = 6
		Sleep(50) '시간지연 코드
	Loop
	
	found = False
	For i=2 To map_size+1
		For j=2 To map_size+1
			If maze(i,j) = False Then
				If maze(i+1,j) = True And Cells(3+i+1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				ElseIf maze(i-1,j) = True And Cells(3+i-1, 3+j).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				ElseIf maze(i,j+1) = True And Cells(3+i, 3+j+1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				ElseIf maze(i,j-1) = True And Cells(3+i, 3+j-1).Interior.ColorIndex <> 3 Then
					Cells(3+i, 3+j).Interior.ColorIndex = 6
					found = True
				End If
			End If
			If found Then
				Exit For
			End If
		Next j
		If found Then
			Exit For
		End If
	Next i
End Sub

 

고생하셨습니다~


WRITTEN BY
컴공학도

,

본 강의부터 본격적으로 미로만들기 알고리즘인 Hun And Kill을 구현해 봅시다.

 

이전 강의까지의 코드입니다.

Sub MakeMaze()
	worksheets("Sheet2").Select
	Dim map_size As Integer
	Dim maze() As Boolean '동적배열 선언
	
	map_size = worksheets("Sheet1").Cells(8,2).value
	ReDim maze(map_size+2, map_size+2) '배열 크기 선언
	
	'배열값 초기화
	For i=1 To map_size+2
		For j=1 To map_size+2
			If i = 1 Or j = 1 Or i = map_size+2 Or j = map_size+2 Then
				maze(i,j) = True
			Else
				maze(i,j) = False
			End If
		Next j
	Next i
	
	'셀을 정사각형으로 만들기
	Range("A1:XFD1048576").ColumnWidth = 1.00 
	Range("A1:XFD1048576").RowHeight = 10.00
    
	'조이스틱을 위한 셀 만들기
	Range(Cells(1,1), Cells(3,3)).ColumnWidth = 0.1 
	Range(Cells(1,1), Cells(3,3)).RowHeight = 1.0
    
	'필요없는 셀은 숨기기
	Rows(map_size+6 & ":" & Rows.Count).EntireRow.Hidden = True
	Range(Cells(4,map_size+6), Cells(map_size+5,Columns.Count)).EntireColumn.Hidden = True
	
	'미로 테두리 그리기
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Interior.ColorIndex = 3
	Range(Cells(3+2, 3+2), Cells(3+map_size+1, 3+map_size+1)).Interior.ColorIndex = 0
	
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Borders.Weight = xlThick
End Sub

 

Hunt And Kill이 알고리즘은 아래의 링크에 자세히 설명되어 있습니다.

2020/02/26 - [엑셀 vba 게임] - 엑셀 vba 미로만들기(1) - Hunt And Kill 알고리즘 설명

 

엑셀 vba 미로만들기(1) - Hunt And Kill 알고리즘 설명

본 강의에서는 엑셀 vba를 이용해 미로를 만들어 보겠습니다. 우선 미로를 만드는 알고리즘에는 여러가지가 있지만 저희가 쓸 알고리즘은 Hunt And Kill 알고리즘 입니다. 1. 미로가 아닌 칸을 랜덤으로 선택합니..

comgonghakdo.tistory.com

먼저 알고리즘의 첫번째인 임의의 칸을 설정해 봅시다

 

먼저 난수를 생성해 봅시다. 난수 생성 방법도 아래의 링크에서 자세한 설명을 찾아보실 수 있습니다.

2020/02/21 - [엑셀 vba 기초] - 엑셀 vba 랜덤한 수 생성하는 법

 

엑셀 vba 랜덤한 수 생성하는 법

본 강의에서는 랜덤한 수를 생성하는 법을 알아보겠습니다. 랜덤한 수를 생성할 때는 기본적으로 Rnd가 이용됩니다. Sub CreateRandomNumber() Dim randomNumber As Integer randomNumber = Rnd End Sub 하지만 위..

comgonghakdo.tistory.com

Sub MakeMaze()
	Randomize '난수 Seed값 초기화
	Dim row, col  As Integer
    
	''' 이전 강의의 코드(생략) '''
	
	row = Int(Rnd * map_size) + 2
	col = Int(Rnd * map_size) + 2
	maze(row,col) = True
	Cells(3+row, 3+col).Interior.ColorIndex = 6
End Sub

row와 col의 변수를 새로 생성하고 각 변수에 테두리를 제외한 셀에 해당하는 행과 열을 넣어야 합니다.

 

난수 생성의 범위 크기는 map_size이고 시작값은 테두리를 제외해야 하므로 1이 아닌 2를 사용해야 합니다.

 

만약 map_size가 5라면 2~6까지의 난수를, map_size가 10이라면 2~12까지의 난수를 생성해야 하기 때문입니다.

 

그 후에 랜덤으로 선택된 셀에서 부터 미로를 만들어 갈 것이므로 maze(row,col)의 값을 True로 설정해 줍니다.

 

또한 어떤 셀이 선택되었는지 알아보기 위해서 선택된 셀의 배경색을 노란색으로 바꿔줍니다

 

여기서 유의해야 할 점은 셀을 선택할 때에는 테두리 안의 미로를 선택해야 하므로 조이스틱크기인 3을 더한  Cells(3+row, 3+col)과 같이 표현해 줍니다.

row=9, col=5의 셀이 선택된 모습

이젠 노란색 셀을 움직여 봅시다.

 

Hunt And Kill의 첫번째 로직은 미로가 아닌 칸이 미로를 만들면서 나아갈 수 있을 때까지 나아가는 것입니다.

 

이를 코드로 구현해 봅시다.

 

Sub MakeMaze()
	Randomize '난수 Seed값 초기화
	Dim row, col  As Integer
    
	Dim move As Integer
    
	''' 이전 강의의 코드(생략) '''
	
	row = Int(Rnd * map_size) + 2
	col = Int(Rnd * map_size) + 2
	maze(row,col) = True
	Cells(3+row, 3+col).Interior.ColorIndex = 6
    
	Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
		''' 코드 작성 중...'''
	Loop
End Sub

우선 움직일 방향을 정하는 move 변수를 생성해 줍니다.

 

그리고 Do Until Loop 반복문을 사용합니다.

 

maze(row-1, col)은 현재 선택된 미로의 위쪽 셀

maze(row+1,col)은 현재 선택된 미로의 아래쪽 셀

maze(row,col-1)은 현재 선택된 미로의 왼쪽 셀

maze(row,col+1)은 현재 선택된 미로의 오른쪽 셀

 

입니다.

 

그리고 maze()의 배열값이 True라면 그 칸은 이미 미로가 생성되었다는 뜻입니다.

 

따라서 Do Until Loop 반복문에 사용된 조건문은 현재 선택된 미로에서 더이상 나아갈 곳이 없다는 걸 의미합니다.

 

그럼 계속해서 코드를 작성해 봅시다.

 

코드가 길어지면 보기 어려우니 아래의 예제에서는 Do Until Loop 반복문의 안쪽 코드만 작성하겠습니다.

 

Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
	move = Int(Rnd * 4) + 1
	Select Case move
		Case 1 '위쪽으로 이동할 때
			If maze(row-1, col) = False Then
				Cells(3+row - 1, 3+col).Borders(xlEdgeBottom).LineStyle = xlNone
				row = row - 1
			End If
		Case 2 '오른족으로 이동할 때
			If maze(row, col+1) = False Then
				Cells(3+row, 3+col + 1).Borders(xlEdgeLeft).LineStyle = xlNone
				col = col + 1
			End If
		Case 3 '왼쪽으로 이동할 때
			If maze(row, col-1) = False Then
				cells(3+row, 3+col - 1).Borders(xlEdgeRight).LineStyle = xlNone
				col = col - 1
			End If
		Case 4 '아래쪽으로 이동할 때
			If maze(row+1, col) = False Then
				Cells(3+row + 1, 3+col).Borders(xlEdgeTop).LineStyle = xlNone
				row = row + 1
			End If
	End Select
	maze(row,col) = True
	Cells(3+row, 3+col).interior.ColorIndex = 6
Loop

move 변수에 1부터 4까지 중 난수를 입력해 줍니다. 그 후에 Select Case 문으로 move의 각 값에 맞는 코드를 실행해 줍니다.

 

move = 1 일 때를 보면,

 

move = 1 은 위쪽으로 이동을 뜻하므로 If maze(row-1, col) = False 를 통해 위쪽 셀이 미로인지 확인합니다.

maze()의 값이 False라면 미로가 아니라는 뜻이므로 우선 Cells().Borders(xlEdgeBottom).LineStyle = xlNone을 이용해 위쪽 셀의 아래쪽 테두리를 지워줍니다.

 

그 후에 row 값에서 1을 빼고 Select Case 문을 빠져나옵니다.

 

Select Case 문 밖에 있는 남은 코드를 실행함으로써 이동한 셀도 미로로 선택이 되고 셀 배경색도 노란색으로 바뀌게 됩니다.

 

매크로 실행 사진

위의 사진과 같이 미로의 생성 과정을 딜레이를 통해 보고싶다면 시간 지연을 이용하시면 됩니다.

 

시간 지연에 대한 자세한 설명은 아래 링크에 있습니다.

2020/02/22 - [엑셀 vba 기초] - 엑셀 vba 시간 지연(딜레이) 넣는 법

 

엑셀 vba 시간 지연(딜레이) 넣는 법

Sleep(x) 시간 지연은 반복문 실행시 너무 빠르게 지나가 진행 과정을 볼 수 없거나, 엑셀을 통한 시각적 효과(게임 등)를 만들 때 사용합니다. 여러가지 방법이 있지만 필자는 가장 편한 Sleep(x) 함수를 사용합..

comgonghakdo.tistory.com

지금까지의 코드를 올리면서 이번 강의를 마치겠습니다.

 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '시간지연 코드

Sub MakeMaze()
	Randomize
	worksheets("Sheet2").Select
	Dim map_size As Integer
	Dim maze() As Boolean '동적배열 선언
	Dim row, col As Integer
	
	map_size = worksheets("Sheet1").Cells(8,2).value
	ReDim maze(map_size+2, map_size+2) '배열 크기 선언
	
	'배열값 초기화
	For i=1 To map_size+2
		For j=1 To map_size+2
			If i = 1 Or j = 1 Or i = map_size+2 Or j = map_size+2 Then
				maze(i,j) = True
			Else
				maze(i,j) = False
			End If
		Next j
	Next i
	
	'셀을 정사각형으로 만들기
	Range("A1:XFD1048576").ColumnWidth = 1.00 
	Range("A1:XFD1048576").RowHeight = 10.00
    
	'조이스틱을 위한 셀 만들기
	Range(Cells(1,1), Cells(3,3)).ColumnWidth = 0.1 
	Range(Cells(1,1), Cells(3,3)).RowHeight = 1.0
    
	'필요없는 셀은 숨기기
	Rows(map_size+6 & ":" & Rows.Count).EntireRow.Hidden = True
	Range(Cells(4,map_size+6), Cells(map_size+5,Columns.Count)).EntireColumn.Hidden = True
	
	'미로 테두리 그리기
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Interior.ColorIndex = 3
	Range(Cells(3+2, 3+2), Cells(3+map_size+1, 3+map_size+1)).Interior.ColorIndex = 0
	
	Range(Cells(3+1, 3+1), Cells(3+map_size+2, 3+map_size+2)).Borders.Weight = xlThick
	
	row = Int(Rnd * map_size) + 2
	col = Int(Rnd * map_size) + 2
	maze(row,col) = True
	Cells(3+row, 3+col).Interior.ColorIndex = 6
	
	Do Until maze(row-1, col) And maze(row+1, col) And maze(row, col-1) And maze(row, col+1)
		move = Int(Rnd * 4) + 1
		Select Case move
			Case 1 '위쪽으로 이동할 때
				If maze(row-1, col) = False Then
					Cells(3+row - 1, 3+col).Borders(xlEdgeBottom).LineStyle = xlNone
					row = row - 1
				End If
			Case 2 '오른족으로 이동할 때
				If maze(row, col+1) = False Then
					Cells(3+row, 3+col + 1).Borders(xlEdgeLeft).LineStyle = xlNone
					col = col + 1
				End If
			Case 3 '왼쪽으로 이동할 때
				If maze(row, col-1) = False Then
					cells(3+row, 3+col - 1).Borders(xlEdgeRight).LineStyle = xlNone
					col = col - 1
				End If
			Case 4 '아래쪽으로 이동할 때
				If maze(row+1, col) = False Then
					Cells(3+row + 1, 3+col).Borders(xlEdgeTop).LineStyle = xlNone
					row = row + 1
				End If
			End Select
		maze(row,col) = True
		Cells(3+row, 3+col).interior.ColorIndex = 6
		Sleep(50) '시간지연 코드
	Loop
End Sub

 


WRITTEN BY
컴공학도

,