Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

shuffle创建卡片洗牌程序Java

我试图在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方法中

    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

有人有什么想法吗?非常感谢大家的帮助,谢谢。另外,我尽量避免使用quick collection类方法,我是java新手,希望在使用collection类之前先充分了解情况


共 (0) 个答案