"MC" <fd***@sdfas.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Good Morning
Anyone know any good lessons online about Arrays in vb.net.
I am a hobbiest and stuggling to understand Arrays. Let me explain briefly
what I want to do.
I am writing a Poker Solutions Program purely for my oen fun and want to
shuffle a deck of cards. now the easiest way, i think, is to make an
Array that looks like this:
Card(0) = (FaceCard, SuitCard, RandomNumber)
Card(1) = (FaceCard, SuitCard, RandomNumber)
......
Card(51) = (FaceCard, SuitCard, RandomNumber)
Card.Sort(2)
FaceCard and SuitCard would just run through a for next loop.
I've got the mechanics in my head but just getting my head around OOP
which I presume the Card would have to be an object I just dont know how
to build that array.
Here's some code to get you started:
Class Cards
Public Enum Rank
Deuce
Trey
Four
Five
Six
Seven
Eight
Nine
Ten
Jack
Queen
King
Ace
End Enum
Public Enum Suit
Club
Diamond
Hart
Spade
End Enum
Public Structure Card
Public ReadOnly Rank As Rank
Public ReadOnly Suit As Suit
Public Sub New(ByVal Rank As Rank, ByVal Suit As Suit)
Me.Rank = Rank
Me.Suit = Suit
End Sub
Public Class CardOrder
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As
Object) As Integer Implements System.Collections.IComparer.Compare
Dim cx As Card = DirectCast(x, Card)
Dim cy As Card = DirectCast(y, Card)
Return cx.Rank.CompareTo(cy.Rank)
End Function
End Class
End Structure
Public Class Deck
'VB arrays are zero based and declared using their Upper Bound,
'not their number of elements
Private cards(51) As Card
Public Sub New()
Dim ix As Integer = 0
For r As Rank = Rank.Deuce To Rank.Ace
For s As Suit = Suit.Club To Suit.Spade
cards(ix) = New Card(r, s)
ix += 1
Next
Next
End Sub
Public Sub Shuffle()
Dim r As New Random
Dim xx As Card
For i As Integer = i To 1000
Dim c1 As Integer = r.Next(0, 51)
Dim c2 As Integer = r.Next(0, 51)
If c1 <> c2 Then
xx = cards(c1)
cards(c1) = cards(c2)
cards(c2) = xx
End If
Next
End Sub
End Class
End Class
David