简体   繁体   English

使用特定和随机整数/字符填充2D数组

[英]Filling a 2D array with specific and random integers / chars

I have to create a 2D array that I print out, however I am struggling trying to include all the specific details. 我必须创建一个要打印的2D数组,但是我在努力包含所有特定细节方面都在努力。 I am given the number of rows and columns via user input, then must print an array with "S" at the top left and "D" at the bottom right, filled with random "#"'s and random numbers. 我通过用户输入获得了行数和列数,然后必须打印一个数组,该数组的左上角为“ S”,右下角为“ D”,并填充有随机的“#”和随机数。 Numbers are randomly generated within the range of [1, 100] inclusively, while the total number of "#" cannot exceed the 1/3 of the total size of the matrix. 在[1,100](含)范围内随机产生数字,而“#”的总数不能超过矩阵总大小的1/3。

However, I'm rather stuck and don't know where to go on my code... 但是,我很困惑,不知道我的代码在哪里...

Code: 码:

public void drawMaze(int numRows, int numCols) {

    int[][] mazeArray = new int[numRows][numCols];
    Random  random = new Random(); 

    // Starting Point
    mazeArray[0][0] = "S";
    // Destination Point
    mazeArray[numRows][numCols] = "D";

    for (int i = 0; i < mazeArray.length; i++) {
        for (int j = 0; j < mazeArray[i].length; j++) {
            mazeArray[i][j] = (int)(Math.random()*10);
        }
    }

}

Help would be appreciated! 帮助将不胜感激!

This could be something like this: 可能是这样的:

public void drawMaze(int numRows, int numCols) {
    String[][] mazeArray = new String[numRows][numCols];
    Random random = new Random();
    for (int i = 0; i < mazeArray.length; i++) {
        for (int j = 0; j < mazeArray[i].length; j++) {
            int result = 1 + random.nextInt(200);
            if (result > 100) {
                mazeArray[i][j] = "#";
            } else {
                mazeArray[i][j] = Integer.toString(result);
            }
        }
    }
    //starting point
    mazeArray[0][0] = "S";
    //destination point
    mazeArray[numRows-1][numCols-1] = "D";
    for (int i = 0; i < mazeArray.length; i++) {
        for (int j = 0; j < mazeArray[i].length; j++) {
            System.out.print(String.format("%4s", mazeArray[i][j]));
        }
        System.out.println();
    }
}

200 is just a hardcoded value. 200只是一个硬编码的值。 You may make it dependent on arguments of the method. 您可以使其依赖于方法的参数。
4 in System.out.print(String.format("%4s", mazeArray[i][j])); System.out.print(String.format("%4s", mazeArray[i][j])); is responsible for spacing (can be changed to another integer, of course). 负责间距(当然可以更改为另一个整数)。

You should not go for each field after another. 您不应该一个一个接一个地去。 Better way could be to fill the fields randomly and leaving 1/3 of your fields empty to be filled with #. 更好的方法可能是随机填充字段,而将1/3的字段留空以填充#。

  • Fill complete array (mazeArray) with "#" 用“#”填充完整的数组(mazeArray)
  • Calculate your overall fields to fill with numbers (numRows*numCols*2/3)=maxFields 计算整个字段以填充数字(numRows * numCols * 2/3)= maxFields
  • Setup a 1-dimensional array (fieldsToFill) filled with 0 to maxFields, incrementing 设置一维数组(fieldsToFill),其中0填充到maxFields,递增
  • Now choose randomly a number of the fieldsToFill and fill up this field pos in your 2-dim array mazeArray and delete that element from fieldsToFill 现在,随机选择多个fieldsToFill并将其填充在2-dim数组mazeArray中,然后从fieldsToFill中删除该元素

This way you get your maze filled more randomly. 这样,您的迷宫就会变得更加随机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM