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 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Location
    Olathe, Kansas
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ......

Similar Threads

  1. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  2. what's going on? I tried to make a scrollable JFrame and it didn't work!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 20th, 2011, 09:47 PM
  3. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 01:51 AM
  4. How to make for loop work?
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2011, 01:14 PM
  5. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 06:09 PM

Tags for this Thread