所以我最近一直忙于在一个 400 个图块的世界中使用 50 个对象创建多个世界。
这是一种方法的代码,该方法使用随机坐标初始化 50 个对象的数组,然后检查重复项 - 根据参数的值,它会执行该次数,我输入 1000000 以获得良好的概率计算所以它运行这个方法 1000000 次。- 这是代码:
public void initializeAndExecute(int runAmount) {
tiles = new String[49]; //array size
for (int k = 0; k < runAmount; k++) { //amount of generating new array
for (int i = 0; i < 49; i++) { //put 50 objects in array
tile = new Tile();
tiles[i] = (tile.printXandY()); //put coordinates at this index
}//eind for
checkDupes(); //check collisions
if (duplicates != 0) { //if collision , counter++, duplicates reset.
runWithCollision++; //for probability calculation
duplicates = 0;
}
run++; //run always ++ even if no duplicates, voor probability calculation.
}
}
所以我现在想要做的是有一个 400 个图块(20x20)的字段,如果对象等于字段中的那个位置,则根据坐标放置一个 [*]。如果同一地点有多个对象,请执行 [**] 之类的操作。这是我的field()方法的代码:
public void field() {
for (int p = 0; p < 20; p++) { //for 20 rows
for (int o = 0; o < 19; o++) { //to fill rows
if (tile.getX() == o && tile.getY() == p) {
System.out.print("[*]");
} else {
System.out.print("[_]");
}
if (o == 18) {
System.out.println("[_]"); //newline
}
}
}
这显然不起作用,但它是为了测试它的外观,它只打印一个坐标,因为它不是在 initializeAndExecute()方法中动态执行的。
有任何想法吗?也许是一个二维数组?(我不知道如何实现它)
非常感谢您的帮助!