By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,407 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,407 IT Pros & Developers. It's quick & easy.

Created two of the same Card Deck class - when I shuffle each deck I get the SAME res

P: 13
Hey everyone,



I created a theDeck class that creates a 52 card int List. Inside of this class I have a ShuffleCards method.



I simply want 2 seperate decks that I can then shuffle and then count a few cards out of each deck.


THUS returning two portions of two decks holding different cards. SOUNDS simple...but when I write the code below as I would think SHOULD work, I get two EXACT matching partial decks!



Can I get some guidance?



public Form1()

{

InitializeComponent();



this.deck1.ShuffleInPlace(deck1.getallCards());

this.deck2.ShuffleInPlace(deck2.getallCards());

for (i = 0; i < 20; i++)

{

prtlDeck1.Add(deck1.getCard(i));

prtlDeck2.Add(deck2.getCard(i));

}


}
Jul 5 '07 #1
Share this Question
Share on Google+
4 Replies


vanc
Expert 100+
P: 211
The usual problem with this situation is that you're using ArrayList (if I guess right). ArrayList keeps all reference of things, not a separate copy of the objects. So you should be noticed about the way you add a reference of a separate copy of object.
You can debug and observe the changing when you add and remove or change items in those two ArrayLists.
To get a separate copy of and object not just a reference you can use Shallow copy method of that object. You can use Clone() method in similar way.

If this is not your problem, give me a yell.

cheer.
Jul 6 '07 #2

P: 13
I am using just a generic LIST and I dont think they have the Clone() method.
Jul 6 '07 #3

Plater
Expert 5K+
P: 7,872
I would say it is a seed issue for random number production.
I would guess that what the shuffle does is select a random card from the deck and push it onto an array, repeating the proccess until no cards are left. Then returning the new collection in the new order.
If there is no random number seed it will produce the same random number every time?
Jul 6 '07 #4

TRScheel
Expert 100+
P: 638
I would say it is a seed issue for random number production.
I would guess that what the shuffle does is select a random card from the deck and push it onto an array, repeating the proccess until no cards are left. Then returning the new collection in the new order.
If there is no random number seed it will produce the same random number every time?
If he uses random() it will seed with time unless a seed is pushed through as a variable. It might be helpful though if he exposes how he is generating the 'shuffling'.
Jul 6 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.