Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem with String Array for Deck of Cards

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with String Array for Deck of Cards

    I am new to Java and trying to write a BlackJack game. The first time I create the deck of cards everything works fine. When a start a second hand and recreate the deck that's when I am having problems. I print the cards when I create the deck in the class array cards (a [52][2] array) and get the correct results but when I reprint the contents of the deck again right after the array is populated I get a number of King of Hearts represented by "h13". The number of h13's in the deck depends on how many cards were dealt the hand before. How can the two prints of the same array be different when one is printed right after the other? I am printing the array index with each card.

    public static void createDeck(){
     
    		int s, n, i, j, cardIndex=0;
    		String temp[][]= new String[1][2]; 	// Array used as temp storage 
    		Random rCard= new Random();			// Use to shuffle the deck
     
    		for (s=0; s<4; s++){			//Create the suits
    			for(n=1; n<=13; n++){		//Create each card value
    			    switch(s)
    			    {
    			    case 0:
    			    	cards[cardIndex][0]=("s");
    			    	cards[cardIndex][1]=(Integer.toString(n));
    			      	break;
    			    case 1:
    			    	cards[cardIndex][0]=("c");
    			    	cards[cardIndex][1]=(Integer.toString(n));
    			      	break;
    			    case 2:
    			    	cards[cardIndex][0]=("d");
    			    	cards[cardIndex][1]=(Integer.toString(n));	
    			      	break;
    			    case 3:                 // Hearts Section
    		       	              cards[cardIndex][0]=("h");
    			    	cards[cardIndex][1]=(Integer.toString(n));
    			    	System.out.println(cardIndex + " " + cards[cardIndex][0] +cards[cardIndex][1]);
    			    	break;
    			    }
    			   cardIndex++;
    			 }
    		}
     
    		for(i=0; i<52; i++)
    			System.out.println(i+ " " +cards[i][0]+cards[i][1]);
     
    		// Shuffle Cards.
     
    		for(i=0; i<52; i++){
    			j=rCard.nextInt(52);
    			temp[0]=cards[i];
    			cards[i]=cards[j];
    			cards[j]=temp[0];
    		}
     
    	}
     
    	public static String[][] dealCard(){
     
    		int i;
    		String tcard[][] = new String [1][2];
    		tcard[0] = cards[0];		 		// get the top card
    		for(i=0; i<numCards-1; i++){	  	// Shift cards to the left by 1
    			cards[i]=cards[i+1];
    		}
    		numCards=numCards-1;				// Reduce the number of cards by 1
    		return tcard;
    	}
    Output of the hearts section of the case statement
    39 h1
    40 h2
    41 h3
    42 h4
    43 h5
    44 h6
    45 h7
    46 h8
    47 h9
    48 h10
    49 h11
    50 h12
    51 h13
    Output after Switch Statement - Prints full array
    0 s1
    1 s2
    2 s3
    3 s4
    4 s5
    5 s6
    6 s7
    7 s8
    8 s9
    9 s10
    10 s11
    11 s12
    12 s13
    13 c1
    14 c2
    15 c3
    16 c4
    17 c5
    18 c6
    19 c7
    20 c8
    21 c9
    22 c10
    23 c11
    24 c12
    25 c13
    26 d1
    27 d2
    28 d3
    29 d4
    30 d5
    31 d6
    32 d7
    33 d8
    34 d9
    35 d10
    36 d11
    37 d12
    38 d13
    39 h1
    40 h2
    41 h3
    42 h4
    43 h5
    44 h6
    45 h7
    46 h13 Problem Area from here down
    47 h13
    48 h13
    49 h13
    50 h13
    51 h13


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with String Array for Deck of Cards

    A useful method for debugging code with 2 dim arrays:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    It prints out the full contents of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with String Array for Deck of Cards

    You've shown an interesting approach to managing 52 different cards that will have numerous chances for errors and constant confusion. I can't imagine how this approach will adjust when the cards are shuffled or dealt. I recommend you either develop a Card class (preferred) or if you must stick with parallel arrays, then store the cards as complete string objects rather than matching the suits to ranks on the fly.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with String Array for Deck of Cards

    The code prints this for me:
    Running: java -cp . TestCode16

    39 h1
    40 h2
    41 h3
    42 h4
    43 h5
    44 h6
    45 h7
    46 h8
    47 h9
    48 h10
    49 h11
    50 h12
    51 h13
    before=[[s, 1], [s, 2], [s, 3], [s, 4], [s, 5], [s, 6], [s, 7], [s, 8], [s, 9], [s, 10], [s, 11], [s, 12], [s, 13], [c, 1], [c, 2], [c, 3], [c, 4], [c, 5], [c, 6], [c, 7], [c, 8], [c, 9], [c, 10], [c, 11], [c, 12], [c, 13], [d, 1], [d, 2], [d, 3], [d, 4], [d, 5], [d, 6], [d, 7], [d, 8], [d, 9], [d, 10], [d, 11], [d, 12], [d, 13], [h, 1], [h, 2], [h, 3], [h, 4], [h, 5], [h, 6], [h, 7], [h, 8], [h, 9], [h, 10], [h, 11], [h, 12], [h, 13]]
    0 s1
    1 s2
    2 s3
    3 s4
    4 s5
    5 s6
    6 s7
    7 s8
    8 s9
    9 s10
    10 s11
    11 s12
    12 s13
    13 c1
    14 c2
    15 c3
    16 c4
    17 c5
    18 c6
    19 c7
    20 c8
    21 c9
    22 c10
    23 c11
    24 c12
    25 c13
    26 d1
    27 d2
    28 d3
    29 d4
    30 d5
    31 d6
    32 d7
    33 d8
    34 d9
    35 d10
    36 d11
    37 d12
    38 d13
    39 h1
    40 h2
    41 h3
    42 h4
    43 h5
    44 h6
    45 h7
    46 h8
    47 h9
    48 h10
    49 h11
    50 h12
    51 h13
    after=[[c, 11], [s, 10], [c, 13], [s, 6], [d, 8], [c, 1], [d, 10], [d, 3], [c, 8], [h, 9], [c, 10], [s, 8], [h, 8], [h, 3], [d, 4], [h, 7], [s, 4], [c, 3], [h, 2], [c, 9], [s, 3], [s, 1], [s, 13], [c, 12], [h, 11], [h, 10], [d, 5], [s, 11], [d, 11], [h, 4], [c, 4], [s, 12], [c, 2], [h, 1], [d, 7], [c, 5], [s, 9], [s, 2], [d, 1], [d, 13], [h, 13], [d, 6], [s, 7], [d, 2], [c, 7], [d, 9], [d, 12], [s, 5], [h, 12], [c, 6], [h, 6], [h, 5]]

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with String Array for Deck of Cards

    Thanks for the tip Norm.

    --- Update ---

    Norm, I get the same output as you show the first time I run the game. When a start the second hand and call createDeck() for the second time and from then on in a get the output that I have posted. I can't understand how the array contents can be correct when output in case 3 of the switch and different when printed in the for loop that executes right after that.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with String Array for Deck of Cards

    Quoting myself:
    You've shown an interesting approach to managing 52 different cards that will have numerous chances for errors and constant confusion. I can't imagine how this approach will adjust when the cards are shuffled or dealt.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with String Array for Deck of Cards

    When a start the second hand and call createDeck() for the second time and from then on in a get the output that I have posted.
    Please make a small, complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Deck of cards
    By gavinqotsa92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2013, 10:57 AM
  2. [SOLVED] Shuffling a deck of cards
    By aterrorbite in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2013, 07:44 PM
  3. Please help with unwrap, deck, shuffle cards
    By pots in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2013, 12:46 PM
  4. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  5. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM