0

I am working on a poker program. I am having some issues figuring out how to shuffle. I have to use arrays. I can't use enums or Lists. i know there are much better ways to make this program but the point is to understand arrays.

I am having some issues figuring out how to shuffle my deck of cards. I'm trying to keep this as simple as possible so I have one class to get the cards of the deck and shuffle them.

import java.util.Arrays;
import java.util.Random;

public class Card 
{


    String[] suits = { "Spades", "Clubs", "Hearts", "Diamonds" };
    String[] values = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

    public Card()
    {

    }

        public void getCards()
        {
           for(int indexSuit = 0; indexSuit < suits.length; indexSuit++)
           {

              String suit = suits[indexSuit];
              System.out.print(suit + " ");

               for(int indexType = 0; indexType < values.length; indexType++)
               {
                  String type = values[indexType];
                  System.out.print(type + " ");
               }
           }

        }    

        public void shuffle(String[] suit, String[] value)
        {
            Random rndSuits = new Random();
            Random rndType = new Random();

            for(int indexSuit = suits.length - 1; indexSuit > 0; indexSuit--)
            {
                int index = rndSuits.nextInt(indexSuit + 1);

                String temp = suits[indexSuit];
                suits[indexSuit] = suit[index];
                suit[index] = temp;

                System.out.print(temp);

                for(int indexType = values.length -1; indexType > 0; indexType--)
                {
                    int index2 = rndType.nextInt(indexType + 1);

                    String temp2 = values[indexType];
                    values[indexType] = value[index2];
                    value[index2] = temp2;

                    System.out.print(temp2);
                }
            }
        }

        public void deal()
        {

        }
}

Here is my main:

public class FiveCardPoker 
{

    public static void main(String[] args){
    Card timsCards = new Card();

    timsCards.getCards();
    timsCards.shuffle(args, args);

    }

}

I was trying to write the shuffle method based on another question I found on here, but it was only for a single array. I'm not sure if logic is correct for the shuffle method. I'm also not exactly sure how to call it in my main method.

How Do I get my arrays from the Card class to the Main class when I call timsCards.shuffle?

EDIT: So I have tried editing it like this, but it doesn't seem to be doing anything.

public void shuffle()
        {
            Collections.shuffle(Arrays.asList(suits));
            Collections.shuffle(Arrays.asList(values));
            System.out.println(Arrays.toString(values) + " of " + Arrays.toString(suits));
        }
user3769297
  • 179
  • 1
  • 2
  • 12
  • Just a tip: Suit and rank are probably better modelled as Enums than as String arrays. – Keppil Jul 17 '14 at 18:35
  • possible duplicate of [Random shuffling of an array](http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – merlin2011 Jul 17 '14 at 18:35
  • I can't use enums, I need to learn how to utilize arrays. As far as the possible duplicate, that is where I got the initial code, but It is only for a single array mine seems more complex and that one doesn't fully answer my questions. how to call the function if I have arguments in the method. – user3769297 Jul 17 '14 at 18:47

4 Answers4

1

Ideal way that I can think of is, You can create some Collection out of these arrays and use

Collections.shuffle() method.

Code -

 String[] suits = {"Spades", "Clubs", "Hearts", "Diamonds"};
 List<String> list = Arrays.asList(suits);
 Collections.shuffle(list);
 System.out.println(list); //Prints shuffled list

Javadoc

Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
1

As for your last question, regarding how to get the array from inside the shuffle function into the main function, you should have your shuffle function return an array, and call it like this:

shuffledCards[] = timsCards.Shuffle(...);

I realize that other people have provided other solutions, but you mentioned the purpose is to understand arrays so I figured I'd mention that this will accomplish the same thing.

Also, although I admittedly haven't tested it, it seems as if your shuffle array will shuffle each suit within itself, but not the deck as a whole. You may want to think about a more creative solution to that (If you aren't going to use Collections.shuffle())

doomsday
  • 83
  • 1
  • 8
0

If you want to write a shuffle method yourself you could do it like this.

static Random rnd = new Random();

static void shuffle(Object[] array) {
  for(int index1 = array.length - 1; index1 > 0; index1--) {
    int index2 = rnd.nextInt(index1 + 1);
    Object temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
  }
}

public static void main(String[] args) {
  String[] suits = { "Spades", "Clubs", "Hearts", "Diamonds" };
  String[] values = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

  shuffle(suits);
  shuffle(values);
}

That way you are able to shuffle arrays of any arbitrary objects, not just strings. You may want to fill an array with objects representing cards and then shuffle that.

Tesseract
  • 8,049
  • 2
  • 20
  • 37
0

There are several thing to improve in your example.

  1. Firstly, suits ("Spades", "Clubs", "Hearts", "Diamonds") and values should be manipulated with an Enum type and not a String. It will be easier to read and debug. You can implement an array of enum if you want.

  2. Only ONE Random object is good enough. And you should use a seed if you hope an unpredictable shuffle...

  3. How you shuffle each array is correct, but NOT the global one. Here you suffle suits, and inside a suit you shuffle the values, but all diamonds stay together... If you want to shuffle a deck of cards (as I imagine) then you should implement only ONE collection/array: an array of Cards and each Card is a uniq couple suit,value.

For your last question, I recommand to implement 'shuffle' as a factory (so it should be static) that returns a shuffle deck of cards. If you prefer an object method (NO STATIC) then you should implement a Deck class, then shuffle will mean shuffle THIS deck.

mcoolive
  • 3,805
  • 1
  • 27
  • 32