2

I have an question about how to shuffle an array. The background is:
Write a memory matching game, which using 16 cards that laid out in a 4*4 square and are labeled with pairs of number from 1 to 8.

Currently, what I need to do is to initialize these cards and shuffle these cards.

And now what I can think of is that creating a Card class including variable Rank from 1 to 8, secondly, naming a new class (matchingGame)(whatever you like) and writing a new static method: shuffle().

But I am stuck at this step.

My first question is how to initialize these 16 cards (8 pairs)?
(I think my code is not the efficient way).

My second question is how to shuffle cards after initialization?

My code is:

public class Card 
{
private int rank;

public Card(int iniRank)
{
    switch (iniRank)
    {
    case 1:
        rank =1;
        break;
    case 2:
        rank =2;
        break;
    case 3:
        rank =3;
        break;
    case 4:
        rank =4;
        break;
    case 5:
        rank =5;
        break;
    case 6:
        rank =6;
        break;
    case 7:
        rank =7;
        break;
    case 8:
        rank =8;
        break;
    }
}

public int getRank()
{
    return rank;
}


}


public static void initialize(Card[] cards)
{
    for (int i=0;i<2;i++)
    {
        cards[i] = new Card(1);
    }
    for (int i=2;i<4;i++)
    {
        cards[i] = new Card(2);
    }
....
}    

Thanks everyone for my previous question!
Just one more question in the same background, As your good advice, you know how to shuffle a 1D array?

But how to shuffle a 2D array? Obviously, I cannot use

List<Card> newcards = Arrays.asList(cards) 

for converting to List now.

Thanks for ur help

Justin
  • 2,765
  • 6
  • 24
  • 25
  • There's no need to use a _switch statement_. You can just set rank to the value of _iniRank_. To figure out how to shuffle, you'll want to look at how to create random numbers: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html – jahroy Dec 07 '12 at 02:32

5 Answers5

2

Your method for initializing the array is essentially correct: loop through your array and add a new instance at each point.

Card[] cards = ...;
for (int i = 0; i < cards.length; i++)
    cards[i] = ...  // new Card instance

Now for shuffling - don't reinvent the wheel: Use Collections.shuffle:

Card[] cards = ...;
List<Card> l = Arrays.asList(cards);
Collections.shuffle(l);
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • @A. R. S. do you mean that creating an arraylist and using collections.shuffle? and how to shuffle an array? could you help me with that? – Justin Dec 07 '12 at 02:48
  • @Ivy - He already answered that question above: convert the array to a collection, shuffle it, then convert it back to an array. – jahroy Dec 07 '12 at 02:53
  • 1
    @Ivy `Collections.shuffle` takes a `List` (not an array). So in the code I posted we first convert the array to a `List`, call `Collections.shuffle` on it, and then convert this shuffled list back into an array and restore it in the original variable, as jahroy indicated. – arshajii Dec 07 '12 at 02:57
  • you don't need the last `toArray()` call because the `asList()` wrapper will write-through to the array. – jtahlborn Dec 08 '12 at 01:25
1

There's no need to use a switch statement in your constructor, as I think you've realized. Just assign rank the value of the int parameter directly.

I'm also not sure what's going on in your initialization method. You create a loop that iterates twice, to create a card of a certain rank?

As far as shuffling goes, you may want to look at the Knuth shuffle.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
1

If you think of the card positions linearly (ie as a single list of grid positions left to right, top to bottom), you can simply use the shuffle() method of the Collections utility class:

List<Card> cards = new ArrayList<Card>();
// populate the list with what ever cards you want, how you want
Collections.shuffle(cards);

then display the cards as a grid

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

I would highly recommend implementing this algorithm.

It is efficient, well known, and correct. You'd be surprised how important the latter is - see this for an interesting example of what can happen when a casino implements an incorrect shuffle algorithm.

JBentley
  • 6,099
  • 5
  • 37
  • 72
0

I don't get your switch statement. You could just create a Card class like this:

public class Card 
{
   private int rank;

   public Card(int iniRank){
      rank = iniRank;
   }

   public int getRank(){
      return rank;
   }
}

Than you create the matchingGame class. Add 16 Cards then shuffle them.

public class matchingGame
{
   private List<Card> cards = null;

   public matchingGame(){
      cards = new ArrayList<Card>(16);
      for (int i=1; i<=8; i++)
      {
         cards.add(new Card(i));
         cards.add(new Card(i));
      }
   }

   public void shuffle()
   {
      //Shuffle algorithm here.
   }
}

For the algorithm I would suggest to look at this post: Java's Collections.shuffle is doing what?

Community
  • 1
  • 1
totymedli
  • 29,531
  • 22
  • 131
  • 165