如何让每张卡只出现一次? [纸牌游戏]

时间:2016-08-22 14:01:33

标签: java android random

我正在制作一张纸牌游戏,每张卡片应该出现,但只有一次。我已经看到了其他一些解决方案,但不能将它们应用到我的游戏中。

这是我的代码

int max = 32;
int min = 1;
Random r = new Random();
int number = r.nextInt(max - min + 1) + min;
String myString = String.valueOf(number);

final ImageView imgTable = (ImageView) findViewById(R.id.imageView2);

if (myString.equals("1")) {

    final Bitmap card1 = BitmapFactory.decodeResource(getResources(), R.mipmap.herz7);
    imgTable.setImageBitmap(card1);
}
if (myString.equals("2")) {

    final Bitmap card2 = BitmapFactory.decodeResource(getResources(), R.mipmap.herz8);
    imgTable.setImageBitmap(card2);
}

4 个答案:

答案 0 :(得分:0)

你正在艰难地做事,试图随意制作。相反,按顺序制作它们然后洗牌。我们假设您有52张牌。创建一个按顺序保存值0 ... 51的数组。然后你可以通过

进行随机播放
for(int i=0; i<52;i++) {
   int toSwap= Random.nextInt(52);
   swapIndices(cardArray, i, toSwap);
}

其中swapIndices在数组中的2个索引处交换卡片。这将改变套牌并给你一个随机的安排。

答案 1 :(得分:0)

很久以前我也开了一个纸牌游戏。

我使用这样的简单逻辑在卡片中洗牌

int arr[] = new int[52] //card have 52 card without joker
for(int i=1;i<=52;i++){
    arr[i-1] = i; // add number 1-52 to arr
}
Random r = new Random();
for(int i=0;i<52;i++){
    int number1 = r.nextInt(52);
    int number2 = r.nextInt(52);
    int tmp = arr[number1];
    arr[number1] = arr[number2];
    arr[number2] = tmp;
} // this looping for shuffle the array

稍后您可以打印或使用索引0

中的数组

答案 2 :(得分:0)

首先,我会说我认为首先简单地洗牌并从顶部抽出多少'n'牌会更容易/更清洁。如果由于某种原因,你选择随机选择卡并需要检查重复,只需要一个用于存储所看到的字符串的集合并检查以确保你没有随机选择一个已经使用过的卡片。

public static Set<String> seenCards = new HashSet<>();

public static void displayRandomCard() {
    int max = 32;
    int min = 1;
    String myString = null;
    while (myString == null && seenCards.size() < 32) {
        Random r = new Random();
        int number = r.nextInt(max - min + 1) + min;
        myString = String.valueOf(number);
        if (seenCards.contains(myString)) {
            myString = null;
        } else {
            seenCards.add(myString);
        }
    }
    if (null == myString) {
        // throw some exception, etc
    }
    final ImageView imgTable = (ImageView) findViewById(R.id.imageView2);
    if (myString.equals("1")) {
        final Bitmap card1 = BitmapFactory.decodeResource(getResources(), R.mipmap.herz7);
        imgTable.setImageBitmap(card1);
    }
    if (myString.equals("2")) {
        final Bitmap card2 = BitmapFactory.decodeResource(getResources(), R.mipmap.herz8);
        imgTable.setImageBitmap(card2);
    }
}

答案 3 :(得分:0)

我会喜欢这个

int max = 32;
int min = 1;
int generated = 0;
Random r = new Random();
int[] indexes = int[max-min+1];
for(int i=0; i<max-min+1; i++) {
    indexes[i] = i+min;
}

while (generated < max - min + 1) {

    int rand = r.nextInt(max - min + 1 - generated) + min;

    number = indexes[rand-1]; //get card number

    //avoid it will used again by putting it as last generated at the bottom of the indexes array
    int temp = indexes[max-min-generated];
    indexes[max-min-generated] = indexes[rand-1];
    indexes[rand-1] = temp;

    generated++;

    String myString = String.valueOf(number);

    final ImageView imgTable = (ImageView) findViewById(R.id.imageView2);

    if (myString.equals("1")) {

        final Bitmap card1 = BitmapFactory.decodeResource(getResources(), R.mipmap.herz7);
        imgTable.setImageBitmap(card1);
    }
    else if (myString.equals("2")) {

        final Bitmap card2 = BitmapFactory.decodeResource(getResources(), R.mipmap.herz8);
        imgTable.setImageBitmap(card2);
    }
    else if ([...]) {
        [...]
    }
    [...]

}
相关问题