473,890 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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()

{

InitializeCompo nent();



this.deck1.Shuf fleInPlace(deck 1.getallCards() );

this.deck2.Shuf fleInPlace(deck 2.getallCards() );

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

{

prtlDeck1.Add(d eck1.getCard(i) );

prtlDeck2.Add(d eck2.getCard(i) );

}


}
Jul 5 '07 #1
4 1503
vanc
211 Recognized Expert New Member
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
tvance929
13 New Member
I am using just a generic LIST and I dont think they have the Clone() method.
Jul 6 '07 #3
Plater
7,872 Recognized Expert Expert
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
638 Recognized Expert Contributor
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

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

Similar topics

23
12973
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For C = 0 To 1000 C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
7
3104
by: Ken Smith | last post by:
I have a little video poker game I created in Javascript. It uses Tables and inner html stuff - for example: (psudo-code) imagePicked=random card image (2h.gif); getElementById(cellNum).innerHTML="<img src='imagePicked'>"; I use small 1k.gif images for the cards. Sometimes on the net the 1k.gifs take a while to load & kills bandwith traffic.
18
2248
by: DaveLessnau | last post by:
I'm trying to learn C on my own and, apparently, my brain went on vacation somewhere. I just can't figure out how to parse the following function call: "void fillDeck( Card * const wDeck, const char * wFace, const char * wSuit)" Card is aliased before that call with: "typedef struct card Card"
8
5562
by: ericvw | last post by:
How would I shuffle a static array of 52 cards that you input an integer, n, into a function and it takes the first n cards as the left segment and the remaining as the right. Then it shuffles this deck starting from the first of the right segment, then first of the left, second of the right, second of the left. Once one side is exhausted it fills in the rest of the shuffle with the other remaining segment. Any suggestions?
0
3020
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can someone helps me. Thanks!! ----------------------------------------------------------------------- The code for the Stock class: ----------------------------------------------------------------------- public class Stock { private Deck deck = new...
6
1899
by: JNeko | last post by:
Hello all, awesome site! I guess I am technically not a beginner in JAVA, but from my code you would not realize it! I don’t expect anyone to help me with this, but I figure I might as well as try and ask. Any help is really appreciated; this should be a piece of cake…driving me crazy. I am writing a simple program (from a book for fun) that creates a deck of cards, shuffles them, gives the user a card, and asks if the next card will be...
5
1956
by: codie | last post by:
I got my code to work be the print out go's down on a angle is there anyway to stop this from happing. P.S sorry for taking up your time class Card: suitList = rankList = def __init__ (self, suit=0, rank=1): if suit >= 0 and suit <= 3:
3
2319
by: Fiona1200 | last post by:
Please help me..i have to write a code in java for a card game as follows. there are 4 players each with 4 decks of cards. each deck has four zeros - four seven as follows (0000,1111,2222,3333,4444,5555,6666,7777). each pack is then shuffled player 1 deals player 2 four of his cards, player 2 deals player 3, 4 of his cards and so on. when each player has four cards, player 1 will pull a card from his deck (the object is to get four of the same...
2
3354
by: rubyhuang | last post by:
the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a standard card. Thus: 0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 50 51 We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards. For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get (assuming this is the first swap): 0...
0
9976
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9814
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11214
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10903
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5834
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4257
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.