开发者

Shuffling a 2D array in Java

I'm trying to shuffle a 2D object array in Java. I thought Collections.shuffle would do the trick, but it looks like it only shuffles the objects in each row, but does not mix rows together, which I w开发者_Python百科anted it to do. Any built in methods or easy to implement methods that can shuffle a 2D array for me? The array is cards[13][4].


It looks like you want to shuffle a deck of cards.

In real world games, we first shuffle the deck, then distribute the cards to the player. You try to change the order of the sequence: you want to distribute a sorted deck to to the players and ask them to exchange cards until all cards are shuffled ;-)

As Stephen C suggested: collect the cards from the players, shuffle the deck and distribute again.


IMO, there is (very close to) zero chance of finding a pre-existing library class / method to do this kind of thing. It is waaay too specialized.

But that's OK. This is a Java 101 coding problem :-) The approach I'd take is to copy the 2D array elements into a 1D array, shuffle them, and copy them back into a 2D array.


Since you have a deck of cards, you should just keep the cards in a list (representing the deck) and shuffle that list.

If i still needed to solve the general problem of shuffling a 2d array, i'd probably make a list view of the 2d array and shuffle through that, like so:

import java.util.AbstractList;

public class TwoDimensionalArrayViewList extends AbstractList implements RandomAccess {
    private Object[][] array;

    public TwoDimensionalArrayViewList(Object[][] array) {
        this.array = array;
    }

    @Override
    public Object get(int index) {
        int row = rowForIndex(index);
        int column = columnForIndex(index);
        return array[row][column];
    }

    private int columnForIndex(int index) {
        return index % array[0].length;
    }

    private int rowForIndex(int index) {
        return index / array[0].length;
    }

    @Override
    public Object set(int index, Object element) {
        Object previous = get(index);
        int row = rowForIndex(index);
        int column = columnForIndex(index);
        array[row][column] = element;
        return previous;
    }

    @Override
    public int size() {
        return array.length*array[0].length;
    }
}

import org.junit.Test;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class TwoDimensionalArrayViewListTest {
    @Test
    public void test() {
        Integer[][] array = { {1, 2, 3}, {4, 5, 6} } ;
        List list = new TwoDimensionalArrayViewList(array);
        assertEquals(1, list.get(0));
        assertEquals(2, list.get(1));
        assertEquals(3, list.get(2));
        assertEquals(4, list.get(3));
        assertEquals(5, list.get(4));
        assertEquals(6, list.get(5));
        Collections.shuffle(list);
    }
}


Another option is to store the data in a single array and calculate the index in this array, based in the i, j values. Shuffling the cards array is now a textbook problem.

This could work or not, depending on how you plan to pass around the individual colors (arrays).

Card[] cards = new Card[52];
...

getCard(int i, int j){
   // Perhaps check ranges for i,j first.
   return cards[j+i*13]
}

Of course you need to put all this in its own class, perhaps Deck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜