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 6 3629
Well, it's pretty easy.
Given a class, Card:
Public Class Card
....
End Class
Dim theDeck(51) As Card
"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.
Cheers in advance
"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
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
Cool, lots of ways for me to experiment.
Cheers Guys
"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.
Cheers in advance
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
"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.
Cheers in advance This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Matteo Settenvini |
last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to...
|
by: JezB |
last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of
files that match one of 3 search patterns, so I need something like
DirectoryInfo ld = new DirectoryInfo(searchDir);...
|
by: Michel Rouzic |
last post by:
It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly
I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; }...
|
by: Rob Griffiths |
last post by:
Can anyone explain to me the difference between an element type and a
component type?
In the java literature, arrays are said to have component types, whereas
collections from the Collections...
|
by: Rene Nyffenegger |
last post by:
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in...
|
by: Robert Bravery |
last post by:
Hi all,
Can some one show me how to achieve a cross product of arrays. So that if I
had two arrays (could be any number) with three elements in each (once again
could be any number) I would get:...
|
by: Doug_J_W |
last post by:
I have a Visual Basic (2005) project that contains around twenty
embedded text files as resources. The text files contain two columns
of real numbers that are separated by tab deliminator, and are...
|
by: mike3 |
last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.programmer.win32
since there's Windows material in here as well as questions related to
standard
C++. Not sure how that'd go over...
|
by: weaknessforcats |
last post by:
Arrays Revealed
Introduction
Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with
arrays and array syntax but is not clear on a )exactly how...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |