# Having a hard time figuring out how to make my code work

• September 6th, 2011, 05:15 AM
iainnitro
Having a hard time figuring out how to make my code work
Greetings:

I am writing a program that basically so far is just populating some arrays. I found myself needing a "non-standard" nested loop setup but can not seem to get the order working the way I would like.

The program is a take on BlackJack and I chose to follow a method for setting up the deck that is to say the least very unorthodox.

There are two char arrays that contain the 13 ranks and 4 suits respectively.

Code :

```char [] rank = {'A', '2'...'K'}; //where ten is 'T' char [] suit = {'C','D','S','H'};```

so my first thought was to just randomly pick a rank and suit and thereby "shuffle" my deck as each card is drawn.

BTW: I welcome suggestions on this.

Here is the problem. I need 13 C's, 13 D's, 13 S's, and 13 H's. I want to put these into a string array as 2 characters for example Queen of Hearts would be "QH".

That is no problem, the conversion is just to use the Character.toString() method :-)

Near as I can tell, I need to iterate though the "rank" array 13 times to fill the "deck" array (that is what I named the string array). and I need to read element 0 of the suit array for those 13 times. On the next run through the loop, we need to read element 1 on the suit array 13 times until the end of the suit array. This will give us 52 "cards", but not shuffled.

The key is that we need only 13 of each suit and of course only 1 of each card in each suit. I was using the Random function to randomly read the 13 cards at first, but decided that I might actually end up with more suit cards or 2 kings of diamonds or whatever, this is DEFINITELY not the idea.

I would love a solution that meets this criteria and randomizes the cards in a single deck array if possible, but due to the late hour and not near a viable solution yet, I hope someone can help.

I was looking at a possible 3-tier nested loop to do it, but it does not seem to logically work out when I tried it and I am sure I am not seeing something.

my thought for the loops were:

13x outside
set first nested loop to element 0
count 4 times

doesn't seem to work in practice. So I hope this makes sense and that someone can help.

Thanks,

Mike
• September 6th, 2011, 07:12 AM
Norm
Re: Having a hard time figuring out how to make my code work
Not sure I understand your problem.
A way to shuffle the cards is to create an array holding the 52 cards and use the Collections shuffle method.
• September 6th, 2011, 07:48 AM
pushpendra.saraswat@gmail
Re: Having a hard time figuring out how to make my code work
What I feel
Card is a Class
and which is having attributes .. point and color
so create a deck array put all d Card inside this array and shuffle this array...
use any algorithm ......