How do I code a LinkedList in VB.Net?
Hello everybody,
I am new to all things .Net, but have several years experience of OOP in Java from my college days.
I have recently written a sliding block/tile type puzzle game in Java and I would now like to rewrite it in VB.Net. The programming construct I used to manage the logic behind the game was a LinkedList - each element of the list represented one block/tile.
Basically speaking In Java I coded the following:
Code:
LinkedList tileManager = new LinkedList();
Then after populating my list like so:
Code:
//The int[] tileLayout was used to hold the colour values
//of the tiles - 0 for black, 1 for white.
public void populateLinkedList(int[] tileLayout)
{
for(int i = 0; i < tileLayout.length; i++)
{
tileManager.add((Integer)tileLayout[i]);
}
}
I could move the tiles in the list in the following manner.
Code:
//Note that the boolean variable direction used below was initialised
//earlier in the code depending on the users actions.
if(direction==true)
{
//Move the tiles clockwise in the list.
tileManager.addFirst(tileManager.removeLast());
}
else if(direction==false)
{
//Move the tiles anti-clockwise in the list.
tileManager.addLast(tileManager.removeFirst());
}
How then would I go about coding the same functionality in VB.Net?
Thank you for your help.
Regards,
The Thing.
Re: How do I code a LinkedList in VB.Net?
.NET 2.0 has a LinkedList class. That code would be almost exactly the same in C# as it is in Java. There are links to C# -> VB.NET converters in my signature. Have a ball. :)
Re: How do I code a LinkedList in VB.Net?
I mean, you could right your own really easily. On the fly untested something like:
VB Code:
Public Class clsTile
Public value As Int16
Public NextT As clsTile
Public PrevT As clsTile
End Class
Public Class MyLinkedList
Private First As clsTile
Private Last As clsTile
Public Sub AddFirst(ByVal oTile As clsTile)
If First Is Nothing Then
oTile = First
oTile = Last
oTile.NextT = Nothing
oTile.PrevT = Nothing
Else
First.PrevT = oTile
oTile.NextT = First
oTile.PrevT = Nothing
First = oTile
End If
End Sub
Public Sub AddLast(ByVal oTile As clsTile)
If Last Is Nothing Then
oTile = First
oTile = Last
oTile.NextT = Nothing
oTile.PrevT = Nothing
Else
Last.NextT = oTile
oTile.PrevT = Last
oTile.NextT = Nothing
Last = oTile
End If
End Sub
Public Function RemoveFirst(ByVal oTile As clsTile) As clsTile
Dim oReturn As clsTile
oReturn = First
If oReturn.NextT Is Nothing = False Then
First = oReturn.NextT
oReturn.PrevT = Nothing
Else
First = Nothing
Last = Nothing
End If
oReturn.NextT = Nothing
oReturn.PrevT = Nothing
Return oReturn
End Function
Public Function RemoveLast(ByVal oTile As clsTile) As clsTile
Dim oReturn As clsTile
oReturn = Last
If oReturn.PrevT Is Nothing = False Then
Last = oReturn.PrevT
oReturn.NextT = Nothing
Else
First = Nothing
Last = Nothing
End If
oReturn.NextT = Nothing
oReturn.PrevT = Nothing
Return oReturn
End Function
End Class
Re: How do I code a LinkedList in VB.Net?
Hi folks, thanks for your help.
Really the only reason I used a linked list was for convenience - the ease in which I could shift the tiles left or right in the list using a single line of code:
Code:
tileManager.addFirst(tileManager.removeLast());
I had two classes - one to handle the display, and another to handle the logic behind the game. The class handling the display used an int[] to store the colour values (0 or 1) of the tiles so perhaps a simple sub-routine/function which takes, manipulates, and returns an int[] is the way to go.
Thanks again.
The Thing.
Re: How do I code a LinkedList in VB.Net?
I would stick with the class concept, though you might alter the functionality somewhat. Returning an array would be possible, but it seems like an ugly solution. Better to have the array wrapped in a class, with the function as a memeber that manipulates it.