简体   繁体   English

我如何在Java中建立卡片改组方法以使卡片按特定顺序排列?

[英]how can i build a card shuffling method in java for cards to go in a specific order?

the cards have to alternate like top half and bottom half in this order till it goes through a 52 card deck. 卡片必须像上半部和下半部一样依次排列,直到通过52张卡片组为止。 Each deck half has 26 cards they go in the following order: 每个副牌都有26张牌,它们按以下顺序排列:

top1, bottom1, top2, bottom2, top3, bottom3, top4, bottom 4, ..., top n, bottom n

I was thinking of doing this: Card[] topHalf= new Card[cards.length/2]; 我正在考虑这样做: Card[] topHalf= new Card[cards.length/2];

public void shuffle() {
    int index = 0;

    for (int suit = 0; suit <= 1; suit++) {
        for (int value = 1; value <= 13; value++) {
            cards[index] = new Card(value, suit);
            index++;
        }
    }
    Card[] botHalf= new Card[(cards.length+26)/2];

    int index2 = 0;

    for (int suit = 2; suit <= 3; suit++) {
        for (int value = 1; value <= 13; value++) {
            cards[index] = new Card(value, suit);
            index2++;

        }
        for (int row = 0; row < cards.length; row++){

            row++;

            Card [] temp = new Card[( topHalf.length)+botHalf.length];
        //cards[row]= cards[index];
    }

Store the cards in a Sorted Map using the order in which they are to appear as a key. 按照卡片显示顺序将其存储在已排序的地图中。 The Sorted Map will do the sorting for you. Sorted Map将为您进行排序。 You can then iterate through them all in order: 然后,您可以依次遍历它们:

SortedMap map = new SortedMap();

map.put(1, "top1");
map.put(2, "bottom1");
map.put(3, "top2");
...
...
...
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
   Object key = iterator.next();
   System.out.println(" card :" + map.get(key));
}

Just to clarify, it sounds like the shuffle you want would arrange the numbers 只是为了澄清,听起来像是您想要的随机排列数字

1 2 3 4 5 6 7 8

as

1 8 2 7 3 6 4 5 . 1 8 2 7 3 6 4 5

The most efficient way to do this is to create a new array, since swapping elements around in-place isn't easy and won't be fast. 最有效的方法是创建一个新数组,因为就地交换元素并不容易,而且不会很快。 The key observation in this pattern is that every even element (starting with index 0, so the first element is even) counts upwards from the start of the deck, and odd element counts backwards from the end of the deck. 此模式中的主要观察结果是,每个偶数元素(从索引0开始,因此第一个元素为偶数)都从牌组的开始向上计数,奇数元素从牌组的末尾开始向后计数。

Using this information, we can loop over the new array, copying in the values from a specific index the original array, based upon whether the current index is odd or even. 使用此信息,我们可以遍历新数组,根据当前索引是奇数还是偶数,从特定索引中复制原始数组的值。 If the current index in our new array is called i , then 如果我们新数组中的当前索引称为i ,则

  • If i is even, then the element we want is at index i/2 如果i是偶数,那么我们想要的元素位于索引i/2
  • If i is odd, then the element is at index array.length - (i+1)/2 (the +1 is to round up to the nearest even number) 如果i为奇数,则元素位于索引array.length - (i+1)/2+1将舍入为最接近的偶数)

Here is the code: 这是代码:

public static Card[] rifleShuffle(Card[] deck) {
    Card[] newDeck = new Card[deck.length];

    for (int i = 0; i < newDeck.length; i++) {
        // Check whether current index is odd or even by using mod 2
        if (i % 2 == 0) {
            newDeck[i] = deck[i / 2];
        } else {
            newDeck[i] = deck[deck.length - ((i + 1) / 2)];
        }
    }

    return newDeck;
}

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

    int i = 0;
    for (int suite = 0; suite < 4; suite++) {
        for (int value = 1; value <= 13; value++) {
            deck1[i++] = new Card(value, suite);
        }
    }

    System.out.println("Before shuffling: " + Arrays.toString(deck1));

    Card[] deck2 = rifleShuffle(deck1);

    System.out.println("After shuffling:  " + Arrays.toString(deck2));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM