Aaron
Aaron

Reputation: 1016

Recreating Arrays

I'm trying to make a deck of cards, with arrays for each suit in a deck. When a card is drawn, it will be removed from the array of the suit it belongs too. When someone calls the shuffle method, it will "recreate" these arrays. I need help with how to do this.

public class Deck {

  private static String[] hearts = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
  private static String[] diamonds = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
  private static String[] clubs = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
  private static String[] spades = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
  private static int cardsLeft = 52;


  public static void shuffle() {

      /*
       * This method will shuffle the deck. It will simply recreate
       * the arrays after the cards have been deleted from being dealt.
       */

      hearts = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
      diamonds = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
      clubs = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
      spades = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"};
      cardsLeft = 52;
  }
}

I create the arrays at the top, then try to overwrite it with the origonal one below in the shuffle method.

Please do not help with anything else -- I want to do this on my own and I am only asking for help with this problem.

Thanks!

Upvotes: 1

Views: 170

Answers (3)

asenovm
asenovm

Reputation: 6517

As pointed out above do use an instance of the List interface instead of arrays and also use Collections.shuffle(List list); method instead of writing one of your own. rename the shuffle method as currently its name is quite counterintuitive compared to what it actually does.

Upvotes: 2

SimonSez
SimonSez

Reputation: 7769

As already mentioned simply use a List out of the Java collection.

Quick and dirty example:

private static ArrayList<String> hearts;
private static ArrayList<String> diamonds;
private static ArrayList<String> clubs;
private static ArrayList<String> spades;
private static int cardsLeft = 52;

static {
    hearts = new ArrayList<String>();
    diamonds = new ArrayList<String>();
    clubs = new ArrayList<String>();
    spades = new ArrayList<String>();

    for(int i = 0; i <= 10; i++) {
        hearts.add(Integer.toString(i));
    }
    hearts.add("jack");
    hearts.add("queen");
    hearts.add("king");
    hearts.add("ace");

    diamonds.addAll(hearts);
    clubs.addAll(hearts);
    spades.addAll(hearts);

}

// will shuffle your list
public static void main(String[] args) {

    for(int j = 0; j <=10 ; j++) {
        Collections.shuffle(hearts);  // this will do the magic

        System.out.println("List shuffled: ");      
        for(String i : hearts) 
            System.out.println(i + " ");  
    }

}

Hope this helped, have Fun!

Upvotes: 2

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

Use this syntax:

hearts = new String[]{"1","2","3",...};

Anyway I'd recommend using list instead of array if you delete elements.

Upvotes: 2

Related Questions