如何创建只会选择一个数字1次的数字生成器? [英] How to create a number generator that will only pick a number 1 time?

查看:74
本文介绍了如何创建只会选择一个数字1次的数字生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作专注游戏.

我有一个缓冲的图像数组,可以在其中加载25张图像精灵表.

I have an buffered image array where I load in a 25 image sprite sheet.

public static BufferedImage[] card = new BufferedImage[25];

0索引为卡背面.和1-24是用于检查卡片是否匹配的卡片正面值.

0 index being the card back. and 1 - 24 being the values for the face of the cards to check against if the cards match.

我想做的是,我将有4个困难,简单,正常,困难和极端.每个难度都有一定数量的纸牌,需要先将其抽出,然后再将其选择的张数翻倍.例如,默认级别为NORMAL(正常),即12个匹配项,因此它需要从Buffered Image数组中随机选择12张唯一的牌,然后将每个值加倍,以便每个牌中只有2张牌,然后对结果进行洗牌.

What I am tying to do is this I will have 4 difficulties Easy, Normal, Hard, and Extreme. Each difficulty will have a certain amount of cards it will need to draw and then double the ones it chosen. for example the default level will be NORMAL which is 12 matches so it need to randomly choose 12 unique cards from the Buffered Image array and then double each value so it will only have 2 of each cards and then shuffle the results.

这是我到目前为止得到的,但是似乎总是有大约99%的时间重复.

This is what I got so far but it always seems to have duplicates about 99% of the time.

//generate cards
                    Random r = new Random();

                    int j = 0;


                    int[] rowOne = new int[12];
                    int[] rowTwo = new int[12];
                    boolean[] rowOneBool = new boolean[12];

                    for(int i = 0; i < rowOneBool.length; i++)
                        rowOneBool[i] = false;


                    for(int i = 0; i < rowOne.length; i++){
                        int typeId = r.nextInt(12)+1;
                        while(rowOneBool[typeId]){
                            typeId = r.nextInt(12)+1;
                            if(rowOneBool[typeId] == false);
                        }

                        rowOne[i] = typeId;
                        j=0;
                    }

我需要生成的3个数量是Easy 6,Normal 12和Hard 18 Extreme,它们将使用除索引0(卡片背面)以外的所有图像.

the 3 amounts I will be needing to generate is Easy 6, Normal 12, and Hard 18 extreme will use all of the images except index 0 which is the back of the cards.

推荐答案

根据我对您的问题的理解,答案应该类似于以下内容: 有2个类,一个叫Randp,另一个叫Main.运行Main,然后编辑代码以适合您的需求.

From what I understand from your question, the answer should look something like this: Have 2 classes, one called Randp and the other called Main. Run Main, and edit the code to suit your needs.

package randp;


public class Main {

    public static void main(String[] args) {
        Randp randp = new Randp(10);
        for (int i = 0; i < 10; i++) {
            System.out.print(randp.nextInt());
        }
    } 

}


package randp;

public class Randp {

private int numsLeft;
private int MAX_VALUE;
int[] chooser;

public Randp(int startCounter) {
    MAX_VALUE = startCounter; //set the amount we go up to
    numsLeft = startCounter;
    chooser = new int[MAX_VALUE];
    for (int i = 1; i <= chooser.length; i++) {
        chooser[i-1] = i; //fill the array up

    }
}

public int nextInt() {
    if(numsLeft == 0){
        return 0; //nothing left in the array
    }
    int a = chooser[(int)(Math.random() * MAX_VALUE)]; //picking a random index
    if(a == 0) {
        return this.nextInt(); //we hit an index that's been used already, pick another one!
    }
    chooser[a-1] = 0; //don't want to use it again
    numsLeft--; //keep track of the numbers
    return a;

}
}

这篇关于如何创建只会选择一个数字1次的数字生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆