创建卡洗牌程序Java

问题描述 投票:0回答:1

我正在尝试在Java中创建一个卡片混洗器,通过同时执行内和外混洗来执行“完美的混洗”。我有以下代码来执行随机播放。

public class Shuffle {

private static final int shuffleCount = 2;

private static final int valueCount = 52;

public static void main(String[] args){
    System.out.println("Result of " + shuffleCount + "perfect shuffles" );

    int[] value1 = new int[valueCount];
    for (int i = 0; i < value1.length; i++){
    value1[i] = i;
}//ends valueCount for loop
    for (int j = 1; j <= shuffleCount; j++){
        riffleShuffle(value1);
        System.out.println(" " + j +":\n");
        for(int k = 0; k < value1.length; k++){
            System.out.println(" " + value1[k]);
        }
        System.out.println();
    }//ends shuffleCount for loop
    System.out.println();
}

public static void riffleShuffle(int[] value){

    int[] temp = new int[value.length];
    int startFirst =  0;
    int startSecond = value.length / 2;
    for (int i = 0; i < value.length; i+=2){
        temp[i] = value[startFirst];
        temp[i+1] =value[startSecond] ;
        startFirst ++;
        startSecond ++;
}
    for (int i =0; i< value.length; i++)
        value[i] = temp[i];

}

}

我正在尝试将其实施到我的套牌类中,而不是洗牌0-52,而是洗牌。我的套牌类如下;

public class Deck {


private List<Card> cards;


private int size;//number of cards in deck


public Deck(String[] faces, String[] suits, int[] values){
   cards = new ArrayList<Card>();
   for(String suit: suits) 
       for(int value = 0; value < values.length && value < faces.length; value++){
           Card a = new Card(faces[value], suit, values[value]);
           cards.add(a);
       }
   size = cards.size();
   shuffle();

}//ends deck

public boolean isEmpty(){
    return cards.isEmpty() && size == 0;
}//ends isEmpty

public int size(){
        return cards.size();
}//ends size

public void shuffle(){
    for (int i = cards.size() - 1; i > 0; i--){
    int pos =  0;
    Card a = cards.remove(pos);
    cards.add(i, a);
    }

}//ends shuffle

public Card deal(){
    size --;
    Card a = cards.get(size);
    return a;
}//ends Card deal

@Override
public String toString(){
    String deck_rtn = "\nsize = " + size + "\n\nUndealt cards: \n\n";

    for(int k = size -1; k >= 0; k--){
        deck_rtn = deck_rtn + cards.get(k);
            if (k != 0){
            deck_rtn = deck_rtn + ", ";
            }//ends first if

            if ((size - k) % 2 == 0) {
            deck_rtn = deck_rtn + "\n";
            }//ends second if

    }

    deck_rtn = deck_rtn + "\nDealt cards: \n";
    for (int k = cards.size() - 1; k >= size; k--){
        deck_rtn = deck_rtn + cards.get(k);
        if (k != size){
        deck_rtn = deck_rtn + ", ";
        }
        if ((k - cards.size()) % 2 ==0){
        deck_rtn = deck_rtn + ", ";
        }
    }

    deck_rtn = deck_rtn + "\n";
    return deck_rtn;

我发现我的问题源于试图将其实现到我的甲板类中的shuffle方法中,有什么想法吗?感谢所有帮助,谢谢。

java shuffle
1个回答
1
投票
    List<Card> cards = new ArrayList<>(52);
    Collections.shuffle(cards);
© www.soinside.com 2019 - 2024. All rights reserved.