This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I think the easiest way would be to create a shuffle method. There are many ways to accomplish this easily, just think about what is going on when a real deck is being shuffled. Yes, random numbers are involved, but not random numbers over the entire deck.
Once that is done, a deal method can easily deal each card off the top of the deck. [ May 04, 2006: Message edited by: Rusty Shackleford ]
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Joined: Feb 28, 2006
OK I have the following code
int DHand1 = new int; int DHand2 = new int; int PHand1 = new int; int PHand2 = new int;
public void deal(); for (int k = 0; k< 52; k++) Random RNum = new Random();
Random RNum = new Random(); int start; start = 1 + RNum.nextInt(51);
This is with the java.util.Random class imported, this should generate the random integers, however i am unsure of 2 things, the first one is how to make the integers unique from each other, the second is how to spread the deal across the 4 arrays. I think the random integers can be assigned to an array with DHand1[k] = start; But i dont know how to put the first 13 numbers in DHand1, then the next 13 in DHand2 etc.
Think again about Rusty's shuffle idea. You start with 52 cards in a new deck in order, then move them about in random ways. If you start with your arrays filled in order you don't have to worry about duplicates and you can focus on moving them about. How would you make sure each card gets moved at least once to some other position in the array?
BTW: The 4 arrays for 4 suits may be exactly right for your problem but they would be a pain in most normal card playing problems. To put all the cards in one array you'd need to know the number and the suit of the card in each slot but your int array only holds one number in each slot. That sounds like a job for a little tiny object with two fields to me.
Try these ideas in some code and let us know how it goes!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi