Solved

Posted on 2009-04-25

I have 3 arraylists. I am working on a program that can plot out a tilepath for a game.

I have an arraylist named "nodes". In it are a bunch of lines in this format: 2561:1245 (not exact numbers). They represent different tiles in the game.

Dim distanceFromStart As New ArrayList()

Dim distanceFromEnd As New ArrayList()

For i As Integer = 0 To nodes.Count() - 1

Dim x As Integer = ParseLeft(nodes(i), ":")

Dim y As Integer = ParseRight(nodes(i), ":")

Dim dist As Integer = distance(endTile, x, y)

distanceFromEnd.Add(dist)

Next

For i As Integer = 0 To nodes.Count() - 1

Dim x As Integer = ParseLeft(nodes(i), ":")

Dim y As Integer = ParseRight(nodes(i), ":")

Dim dist As Integer = distance(startTile, x, y)

distanceFromStart.Add(dist)

Next

My distance function finds the distance between the 2 tiles.

I want to find which tile is closest to startTile, but between startTile and endTile.

My tile class just contains two integers, x and y

Thanks!

I have an arraylist named "nodes". In it are a bunch of lines in this format: 2561:1245 (not exact numbers). They represent different tiles in the game.

Dim distanceFromStart As New ArrayList()

Dim distanceFromEnd As New ArrayList()

For i As Integer = 0 To nodes.Count() - 1

Dim x As Integer = ParseLeft(nodes(i), ":")

Dim y As Integer = ParseRight(nodes(i), ":")

Dim dist As Integer = distance(endTile, x, y)

distanceFromEnd.Add(dist)

Next

For i As Integer = 0 To nodes.Count() - 1

Dim x As Integer = ParseLeft(nodes(i), ":")

Dim y As Integer = ParseRight(nodes(i), ":")

Dim dist As Integer = distance(startTile, x, y)

distanceFromStart.Add(dist

Next

My distance function finds the distance between the 2 tiles.

I want to find which tile is closest to startTile, but between startTile and endTile.

My tile class just contains two integers, x and y

Thanks!

10 Comments

Then you can use LINQ to easily sort the nodes in different ways or find min/max values...all without changing the order of the original list. (ArrayLists seem an odd choice of storage given that you are using .Net 3.X)

Also, why are you storing numeric data in a delimited string like that? Your code would run faster if you didn't have to parse it first...

These comments may be way off base, though, since we really can't see the "big picture" of your app. =\

Like, I would load the node list, correct? Then parse each tile into X class. How would I save the list of X that I made?

Sorry if that's confusing.

Or at least understand better what you are doing.

You are populating "nodes" with a bunch of coordinates? x:y

It seems like you should be putting actual REFERENCES to the nodes in to a List and then querying the node for its X,Y values to make the calculations. Or even passing the the "target" start/end tile into the Tile class so that it can return its distance to that node.

Just seems like the whole thing would be easier to manage and EXTEND if you had more of an Object Oriented approach.

But again, I would need to see ALOT more code to definitely say either way and make good suggestions. How big is the current project?

The nodes list is just a text file with x:y over and over again.

Public Function ParseLeft(ByVal strSource As String, ByVal strEnd As String) As String

Return Left(strSource, strSource.IndexOf(strEnd))

End Function

Public Function ParseRight(ByVal strSource As String, ByVal strStart As String) As String

Return Right(strSource, strSource.Length - strSource.IndexOf(strStart

End Function

Function distance(ByVal startTile As Tile, ByVal endX As Integer, ByVal endY As Integer) As Integer

Dim dx As Integer = startTile.x - endX

Dim dy As Integer = startTile.y - endY

Return Math.Sqrt(dx * dx + dy * dy)

End Function

Function distance(ByVal startTile As Tile, ByVal endTile As Tile) As Integer

Dim dx As Integer = startTile.x - endTile.x

Dim dy As Integer = startTile.y - endTile.y

Return Math.Sqrt(dx * dx + dy * dy)

End Function

What else do you want to see? :D

Public Class Tile

Public distFrStart As Integer

Public distFrEnd As Integer

Public x As Integer, y As Integer

Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal distFrStart As Integer, ByVal distFrEnd As Integer)

Me.x = x

Me.y = y

Me.distFrEnd = distFrEnd

Me.distFrStart = distFrStart

End Sub

End Class

my code:

Dim Tilelist as new ArrayList()

Tilelist.Add(New Tile(x, y, distance(startTile, x, y), distance(endTile, x, y)))

Now, how I can use these to determine which tile is closest to startTile, but between startTile and endTile? I can use Tilelist(i).x, Tilelist(i).y, etc. This is so much easier!

I'll take a closer look and possibly post some code this evening though. =)

```
Public Sub SortObjectCol(ByVal List As ArrayList, ByVal min As Integer, _
ByVal max As Integer, ByVal propName As String)
Dim last_swap As Integer
Dim i As Integer
Dim j As Integer
Dim tmp As Object
' Repeat until we are done.
Do While min < max
' Bubble up.
last_swap = min - 1
' For i = min + 1 To max
i = min + 1
Do While i <= max
' Find a bubble.
If CallByName(List(i - 1), propName, CallType.Get) > CallByName(List(i), propName, CallType.Get) Then
' See where to drop the bubble.
tmp = List(i - 1)
j = i
Do
List(j - 1) = List(j)
j = j + 1
If j > max Then Exit Do
Loop While CallByName(List(j), propName, CallType.Get) < CallByName(tmp, propName, CallType.Get)
List(j - 1) = tmp
last_swap = j - 1
i = j + 1
Else
i = i + 1
End If
Loop
' Update max.
max = last_swap - 1
' Bubble down.
last_swap = max + 1
' For i = max - 1 To min Step -1
i = max - 1
Do While i >= min
' Find a bubble.
If CallByName(List(i + 1), propName, CallType.Get) < CallByName(List(i), propName, CallType.Get) Then
' See where to drop the bubble.
tmp = List(i + 1)
j = i
Do
List(j + 1) = List(j)
j = j - 1
If j < min Then Exit Do
Loop While CallByName(List(j), propName, CallType.Get) > CallByName(tmp, propName, CallType.Get)
List(j + 1) = tmp
last_swap = j + 1
i = j - 1
Else
i = i - 1
End If
Loop
' Update min.
min = last_swap + 1
Loop
End Sub
```

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**24** Experts available now in Live!