gpt4 book ai didi

java - 随机将 2D 数组中的 'a' 移动到指定位置,同时跟踪它通过每个点的时间数

转载 作者:行者123 更新时间:2023-12-01 15:18:36 26 4
gpt4 key购买 nike

我有这个二维数组(比如 double[10][10]),其中包含一些 1.0 和 10.0,其余的都在 0.0 秒内。我试图循环遍历这个数组来找到 1.0(起点),从那里随机“移动”它(使用 random.nextInt(4))向上、向下、向左或向右,直到达到 10.0。我创建了一个emptyArray来跟踪它移动通过每个点的次数(或者至少我认为我做到了)。编译时没有任何结果,但当我尝试将其显示到框架中时没有得到任何结果。知道我哪里出错或丢失了吗?

{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}

二维数组示例。

    double[][] getPath(double[][] dataIn) {
double[][] emptyArray = new double[dataIn.length][dataIn[0].length];
double[][] drunkLoc = new double[dataIn.length][dataIn[0].length];
for (int i = 0; i < dataIn.length; i++) {
for (int j = 0; j < dataIn[i].length; j++) {
if (dataIn[i][j] == 1.0) {

double drunkHome = 10.0;
drunkLoc[i][j] = dataIn[i][j];
do {
int dir = getDirection();
switch(dir) {
case 0:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i-1][j];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 1:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i][j-1];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 2:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i+1][j];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 3:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i][j+1];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
default:
}
} while (drunkLoc[i][j] != drunkHome);
}
}
}
return emptyArray;
}

如果您需要更多说明,请告诉我。这只是我的第二篇文章,所以仍在学习我的提问技巧。提前致谢。

最佳答案

好的,为了供您学习,这里有一些示例代码,它将为您提供您正在寻找的答案(在“棋盘”上从 (xpos,ypos) 到 (destX,destY) 的移动次数,尺寸为 penDimension x penDimension:

        int penDimension = 10;
int destX = 2;
int destY = 2;
int xpos = 5;
int ypos = 5;

// Add this to keep track of no moves through each square
int[][] moveCounts = new int[penDimension][penDimension];

Random r = new SecureRandom();
long noMoves = 0;
while (xpos != destX || ypos != destY) {
switch (r.nextInt(4)) {
case 0 : xpos++; break;
case 1 : xpos--; break;
case 2 : ypos++; break;
case 3 : ypos--; break;
}
if (xpos < 0) xpos = 0;
if (ypos < 0) ypos = 0;
if (xpos > penDimension) xpos = penDimension;
if (ypos > penDimension) ypos = penDimension;
noMoves++;

// Add this to keep track of no moves through each square
moveCounts[ypos][xpos]++;

}
System.out.println("Number of moves: " + noMoves);

您也可以这样写(在现实生活中可能会这样写),而不是先执行++ 或 -- 然后再检查边界,例如:

xpos = Math.max(0, xpos - 1);

我只是按照上面的方式编写它,因为我认为这样会更容易理解。

除了编写“new SecureRandom()”之外,您还可以编写“new Random()”,这可能就是您所学到的。但 SecureRandom 是一个质量更高(但速度更慢)的随机数生成器。一般来说,在编写重复生成大量随机数的“模拟”时,最好避免使用标准 Random 类并使用更高质量的生成器。

关于java - 随机将 2D 数组中的 'a' 移动到指定位置,同时跟踪它通过每个点的时间数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com