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: How can I assign random cards to correct images?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How can I assign random cards to correct images?

    I have a deck of cards that I want to make in order and shuffle and when they are shuffled, I want to deal them out on the table but I don't know how to assign the correct image to the card. The card names consist of ks.gif for king of spades, and jd.gif for jack of diamonds, 5c.gif for five of clubs, etc... how can I assign the correct image to the correct card without having to individually assign all 52 cards by hand if possible? Thanks for your time.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I assign random cards to correct images?

    how this relates to cat and category.
    If you have a naming convention so that a program can generate the names of the image files, then you could use some nested loops: suit in one, face value in the other. Have an array of names for the 13 face value names and an array for the names of the four suits. In the loops concatenate a value from each array to get filename.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How can I assign random cards to correct images?

    Quote Originally Posted by Skynet928 View Post
    I have a deck of cards...
    I do it with numbers.

    Here's what I mean:

    Let the 52 card values be integers 0, 1, 2, ... , 51

    Suppose a given card has an integer value of, say, x.

    A number value for the rank is obtained by the following:

    int rank = x % 13

    You could make the following correspondences between integer value of rank and a card. (I usually consider Ace to be the highest rank, so I will give it the largest integer value.)

    0 => Deuce
    1 => Trey
    2 => Four
    .
    .
    .
    8 => Ten
    9 => Jack
    10 => Queen
    11 => King
    12 => Ace


    Next, a number value for the suit is obtained by the following:

    int suit = x / 13

    There are four possible values for suit, and you might make the following correspondences:
    0 => Clubs
    1 => Diamonds
    2 => Hearts
    3 => Spades

    By having each card represented by a single integer, lots of things become "easy."

    For example a Card is an integer having value 0..51
    A Deck for Poker or Bridge can be simply an array of 52 Cards, each of which has a different integer value.

    You will need methods that can convert a given card to text or graphical representations for your application. For simple (not particularly attractive) text applications, you only need an array of chars for suits and another array for ranks.

    Maybe something like:
        char suits[] = {'c', 'd', 'h', 's'}; // For Clubs, Diamonds, Hearts, Spades
        char ranks[] = {'2', '3', '4', '5', '6', '7', '8', '9', 't', 'j',  'q', 'k', 'a'}; // Ace-high

    Then you make a String representation of a card by appending the char from ranks[] to the char from suits[] for the given card. So, for example a card with an integer value of 24 gives suit=1, rank = 9 so, by indexing into the corresponding arrays, it becomes "jd" for the Jack of Diamonds. (You could use arrays of Strings instead of Chars to make the representation a little more attractive if you have a mind to.)

    Or some such thing.

    Now: Start with a deck that consists of an array of 52 integer values that has contents 0, 1, 2, ... ,50, 51 (easily assigned in a single loop, since you are only dealing with integer variables)..

    You can shuffle the deck by performing a "random shuffle" on the array of 52 integer values of the cards. It's really easy. Look it up. Fisher-Yates shuffle Scroll down to the pseudo-code in the section titled "The modern algorithm."

    I mean, it's OK to try to figure out how to "randomize" the order of elements in an array, but most people's initial efforts don't do it "right" from the point of view of "how random is random" as well as requiring an unknown number of calls to a random function.

    Bottom line: Don't do something naive; look it up. It takes something like five lines of Java, and consists of a single pass that always calls a random function exactly 51 times to shuffle a 52 card deck, and has mathematically proven properties of unpredictableness (depending only on the distribution of variates from the random function that gets called).

    Or...

    Instead of making your own random shuffle method, consider using the shuffle() method that is conveniently part of the Collections class. You can't plug an array into the Collections.shuffle() method, but you can shuffle a List of Integers corresponding to a an array of Integers (using Arrays.asList()) with the result is it will shuffle the underlying array. It just flabbers my gast every time I go over it again.


    Cheers!

    Z

Similar Threads

  1. Assign layout_width programmatically
    By ptia in forum Android Development
    Replies: 2
    Last Post: December 16th, 2012, 04:09 AM
  2. Assign a value in a void method
    By Freezer Boy in forum Object Oriented Programming
    Replies: 4
    Last Post: December 12th, 2012, 11:13 AM
  3. Trying to make 3 images become random in an Applet. Need help
    By slahsdash in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 9th, 2012, 09:39 PM
  4. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM
  5. Cannot assign requested address
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2012, 01:32 PM