Arrays

  • Thread starter Thread starter MC
  • Start date Start date
M

MC

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.

Cheers in advance
 
Well, it's pretty easy.

Given a class, Card:

Public Class Card

....

End Class

Dim theDeck(51) As Card
 
MC said:
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
 
another way to do the shuffle is to add a sequence nbr to the card.

Generate 52 random numbers in say the range 1-20000, setting the
sequence for each card to one of these

Sort by sequence.

Should give a pretty good random distribution for the cost of
generating 52 random numbers and sorting the deck.

hth,
Alan.
 
another way to do the shuffle is to add a sequence nbr to the card.

Or just generate the pack already shuffled in an arraylist (modifying
David's code):-

Dim pack As ArrayList = New ArrayList(52)
Dim p As Integer
For s as Suit = Suit.Club To Suit.Spade
For r as Rank = Rank.Deuce To Rank.Ace
pack.Insert(Random(0, p), New Card(r, s))
p += 1
Next
Next


Where Random is a function (thank you VB help):-

Function Random(ByVal lowerbound As Integer, ByVal upperbound As Integer)
Return CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
End Function

Andrew
 
Hi Folks. MC Again, thanks for your advice earlier,

i cracked it and i thought i'd share my final Deck Dealing with you. I know
have to figure out a solution to what hand am I holding. But thats for
another time.

Public Class DeckOfCards

Public Enum Suit
Clubs
Spades
Diamonds
Hearts
End Enum

Public Enum Face
Ace
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Jack
Queen
King
End Enum

Public Class Card

Implements IComparable

Public ReadOnly Suit As Suit

Public ReadOnly Face As Face

Public ReadOnly random As Integer

Public Sub New(ByVal suit As Suit, ByVal face As Face, ByVal Random As
Integer)

Me.Suit = suit

Me.Face = face

Me.random = Random

End Sub

Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.IComparable.CompareTo

Dim c As Card = obj

Return (Me.random - c.random)

End Function

End Class


Public Shared Function Deck()

Dim pack As ArrayList = New ArrayList(51)

Dim r As New Random

For s As Suit = Suit.Clubs To Suit.Hearts

For f As Face = Face.Ace To Face.King

Dim m_card As New Card(s, f, r.Next(1, 99999))

pack.Add(m_card)

Next

Next

pack.Sort()

Return pack

End Function

End Class

Cheers guys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top