Removing an option from random number

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
Hey, I'm programming card games right now, so i need to know how to make sure you don't get the same card again. I assigned the cards by using random numbers for the suit and the card value, i realize i could have used a number out of 52 as well. I need to find a way, that once one of those numbers is chosen, it won't choose it again. Forgive me if I'm vauge, but I don't have my code in front of me right now.
 
Another idea: You implement a card class that can be any single one of the 52 cards (it needs two properties: suit and value). Create 52 instances of this class to fill a full deck (value 1-13 for each suit), these instances can be added to a collection (which is the deck). Now you can select a random index of that collection and draw the card out (remove it) from the collection and keep doing this until all cards are drawn.
 
I know enough to follow what your saying, but I don't think i could do it yet. My computer science class hasn't even started learning about variables or if statements yet (high school class). The stuff i know is what I've tought myself, and i don't know about collections or even classes yet.
 
Since you have barely started learning yet I will throw in a very basic and easy example to understand, even if you haven't learned any of the code keywords used you should see what is happening and why it is so. You can take it or leave it, but it is much easier than to keep track of cards already 'taken' and keep on pulling random numbers until you hit one that isn't. (That's all the way backwards and wrong approach if you ask me.)

All this code can be put in a new form with only one Button and a Label on it, I've just divided it up a little to add some comments.

First defined is the enumerations of suits and ranks (values):
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE][SIZE=2] cardsuit[/SIZE]
[SIZE=2]spades[/SIZE]
[SIZE=2]hearts[/SIZE]
[SIZE=2]clubs[/SIZE]
[SIZE=2]diamonds[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE][SIZE=2] cardrank[/SIZE]
[SIZE=2]ace[/SIZE]
[SIZE=2]two[/SIZE]
[SIZE=2]three[/SIZE]
[SIZE=2]four[/SIZE]
[SIZE=2]five[/SIZE]
[SIZE=2]six[/SIZE]
[SIZE=2]seven[/SIZE]
[SIZE=2]eigth[/SIZE]
[SIZE=2]nine[/SIZE]
[SIZE=2]ten[/SIZE]
[SIZE=2]jack[/SIZE]
[SIZE=2]queen[/SIZE]
[SIZE=2]king[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE]
Then the Card class, I have added to basic methods, the Sub New constructor used to simplify creating a new card - and the utility function ToString used to get a reasonable text representation of the card:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] card[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] suit [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] cardsuit[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] rank [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] cardrank[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] suit [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] cardsuit, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] rank [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] cardrank)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].suit = suit[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].rank = rank[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] ToString() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] rank.ToString & [/SIZE][SIZE=2][COLOR=#800000]" of "[/COLOR][/SIZE][SIZE=2] & suit.ToString[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
Here is the collection, a list of Cards:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] aDeck [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] List([/SIZE][SIZE=2][COLOR=#0000ff]Of[/COLOR][/SIZE][SIZE=2] card)[/SIZE]
In form load the Deck is setup, adding one of each possible card.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmDiv_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2]SetupDeck()[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SetupDeck()[/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] suit [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 3[/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] rank [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 12[/SIZE]
[SIZE=2]aDeck.Add([COLOR=#0000ff]New[/COLOR][SIZE=2] card(suit, rank)[/SIZE])[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Each time button is clicked a random card is retrieved from the collection, telling how many left in deck and what card was drawn.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2]DrawCard()[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[COLOR=#0000ff]Dim[/COLOR][SIZE=2] r [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Random[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DrawCard()[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] aDeck.Count > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aCard [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] card[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] randomindex [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = r.Next(0, aDeck.Count)[/SIZE]
[SIZE=2]aCard = aDeck(randomindex)[/SIZE]
[SIZE=2]aDeck.RemoveAt(randomindex)[/SIZE]
[SIZE=2][SIZE=2]Label1.Text = [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#800000]"{0} cards left in deck.{1}Card removed from collection was: {2}"[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2]aDeck.Count.ToString, vbNewLine, aCard.ToString)[/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2][SIZE=2]Label1.Text = [/SIZE][/SIZE][SIZE=2][COLOR=#800000]"No more cards in deck."[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
If none of this ring even the slightest bell you could always paste in the code and pick random cards just for the laughs.
 
Back
Top