如何创建只会选择一个数字1次的数字生成器?

编程入门 行业动态 更新时间:2024-10-20 13:49:00
本文介绍了如何创建只会选择一个数字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次的数字生成器?

本文发布于:2023-11-30 20:52:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   只会   生成器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!