Module Module1
'values for special locations
Dim blank As Integer = 0
Dim wall As Integer = 255
Dim goal As Integer = 1
Dim robot As Integer = 254
'starting robot/goal locations
Dim robot_x As Integer = 5
Dim robot_y As Integer = 3
Dim goal_x As Integer = 0
Dim goal_y As Integer = 3
'map starting pos
Dim x As Integer = 0
Dim y As Integer = 0
'temp variables
Dim temp_A As Integer = 0
Dim temp_B As Integer = 0
Dim counter As Integer = 0
Dim steps As Integer = 0
'when searching for node with lowest value
Dim minimum_node As Integer = 250
Dim min_node_location As Integer = 250
Dim new_state As Integer = 4
Dim old_state As Integer = 0
Dim trans As Integer = 50
Dim reset_min As Integer = 250
'x is vertical, y is horizontal
Dim map(,) As Integer = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {255, 255, 255, 255, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}
Sub Main()
Console.WriteLine("Starting WaveFront")
Console.WriteLine()
'WAVEFRONT CODE'
While map(robot_x, robot_y) <> goal
'find new location to go to
new_state = propagate_wavefront(robot_x, robot_y)
'update new location of robot
If new_state = 1 Then
robot_x -= 1
End If
If new_state = 2 Then
robot_y += 1
End If
If new_state = 3 Then
robot_x += 1
End If
If new_state = 4 Then
robot_y -= 1
End If
'make new state the old state
old_state = new_state
trans -= 1
End While
Console.WriteLine("Steps: " & steps)
Console.Write("Press any key to continue...")
Console.ReadKey()
End Sub
Public Function propagate_wavefront(ByVal robot_x, ByVal robot_y)
'clear old wavefront
unpropagate(robot_x, robot_y)
'add goal
map(goal_x, goal_y) = goal
Console.WriteLine("Adding Goal:")
print_map()
counter = 0
While counter < 50
x = 0
y = 0
'while the map hasn't been fully scanned
While x < 6 And y < 6
'if this location is a wall or a goal, ignore it
If map(x, y) <> wall And map(x, y) <> goal Then
'a full trail to the robot has been found, finished!
If min_surrounding_node_value(x, y) < reset_min And map(x, y) = robot Then
Console.WriteLine("Finished Wavefront:")
print_map()
'finished! tell robot to start moving down path
Return min_node_location
'record a value into this node
ElseIf minimum_node <> reset_min Then 'if this isn't here, nothing will go in the location
map(x, y) = minimum_node + 1
End If
End If
'go to next node and/or row
y += 1
If y = 6 And x <> 6 Then
x += 1
y = 0
End If
End While
Console.WriteLine("Sweep #: " & counter + 1)
print_map()
counter += 1
End While
End Function
Public Sub unpropagate(ByVal robot_x, ByVal robot_y)
Console.WriteLine("Old Map:")
print_map()
'go through each location
For x = 0 To 5
For y = 0 To 5
'if it's not wall or goal
If map(x, y) <> wall And map(x, y) <> goal Then
'set it to 0
map(x, y) = blank
End If
Next
Next
' put the robot location back in
map(robot_x, robot_y) = robot
Console.WriteLine("Unpropagation Complete:")
print_map()
End Sub
Public Function min_surrounding_node_value(ByVal x, ByVal y)
'reset minimum
minimum_node = reset_min
'down
If x < 5 Then
If map(x + 1, y) < minimum_node And map(x + 1, y) <> blank Then
minimum_node = map(x + 1, y)
min_node_location = 3
End If
End If
'up
If x > 0 Then
If map(x - 1, y) < minimum_node And map(x - 1, y) <> blank Then
minimum_node = map(x - 1, y)
min_node_location = 1
End If
End If
'right
If y < 5 Then
If map(x, y + 1) < minimum_node And map(x, y + 1) <> blank Then
minimum_node = map(x, y + 1)
min_node_location = 2
End If
End If
'left
If y > 0 Then
If map(x, y - 1) < minimum_node And map(x, y - 1) <> blank Then
minimum_node = map(x, y - 1)
min_node_location = 4
End If
End If
Return minimum_node
End Function
Public Sub print_map()
'go through each location
For temp_B = 0 To 5
For temp_A = 0 To 5
If map(temp_B, temp_A) = wall Then
'if it's a wall, print W
Console.Write("W ")
ElseIf map(temp_B, temp_A) = robot Then
'if it's the robot, print R
Console.Write("R ")
ElseIf map(temp_B, temp_A) = goal Then
'if it's the goal, print G
Console.Write("G ")
Else
'otherwise, print the value
Console.Write(map(temp_B, temp_A) & " ")
End If
Next
'new line
Console.WriteLine()
Next
Console.WriteLine()
steps += 1
End Sub
Public Sub Pause(ByVal Milliseconds As Integer)
Dim dTimer As Date
dTimer = Now.AddMilliseconds(Milliseconds)
Do While dTimer > Now
Loop
End Sub
End Module