简体   繁体   English

使用随机数填充2D数组

[英]Filling a 2D array with randomised numbers

I have started a project trying to create a Ken Ken puzzle. 我已经开始尝试创建Ken Ken拼图。 If you are not sure what Ken Ken is, it is similar to Sudoku in the way that there can be no duplicated integer values in a row or column. 如果你不确定Ken Ken是什么,它就像Sudoku一样,行或列中没有重复的整数值。

I am trying to fill a 2D Array with numbers from an Array List that is created for every new row. 我正在尝试使用为每个新行创建的数组列表中的数字填充2D数组。 I make checks to see whether or not the number taken from the Array List does not match any numbers within its own row and column. 我进行检查以查看从数组列表中获取的数字是否与其自己的行和列中的任何数字都不匹配。

When I run my code I get an "Index Out Of Bounds" exception when I try removing the integer value from the list. 当我运行我的代码时,当我尝试从列表中删除整数值时,我得到一个“Index Out Of Bounds”异常。 I'm not sure why this is happening because I think I am getting the right element. 我不确定为什么会这样,因为我认为我得到了正确的元素。

Here is my code: 这是我的代码:

int GRID_SIZE = 4; int[][] grid = new int[GRID_SIZE][GRID_SIZE]; List<Integer> nums = new ArrayList<Integer>();

private void populateGrid() {

    for (int row = 0; row < GRID_SIZE; row ++) {

        // Creates an array of values from 1 to grid size.
        for (int i = 1; i <= GRID_SIZE; i++) nums.add(i);

        for (int col = 0; col < GRID_SIZE; col++) {

            while (nums.size() > 0) {

                // Gets a random number from the Array List
                int ranNum = nums.get(numGen.nextInt(GRID_SIZE));

                // Checks to see if the number is placeable.
                if (canPlace(ranNum, row, col)) {

                    // Places the number in the 2D Array
                    grid[row][col] = ranNum;
                    break;

                } else {

                    // Removes duplicate element from the Array List.
                    nums.remove(ranNum); <------{Index Out Of Bounds Exception]
                }
            }
        }
    } 
}

private boolean canPlace(int ranNum, int row, int col) {

    for (int i = 0; i < GRID_SIZE; i++) {

        // Checks if the specified number is already in the row/column.
        if (grid[col][i] == ranNum) return false;
        if (grid[i][row] == ranNum) return false;
    }

    return true;
}

I have a few questions about this: 我有几个问题:

First of all, why am I getting the error that I am ? 首先, 为什么我得到的错误是我

Secondly is there anything better to use than a 2D Array for the grid and the way I place my numbers ? 其次,对于网格和我放置数字的方式,有什么比2D数组更好用吗?

Lastly, am I using the break correctly ? 最后, 我正确使用了休息吗?

Thanks in advance for your answers. 提前感谢您的回答。

The IndexOutOFBoundsException happens because of a failure (IMO) in the List API. 由于List API中的失败(IMO)而发生IndexOutOFBoundsException It has a remove(Object element) method, which is what you want to call, and a remove(int index) method, which is what you are actually calling. 它有一个remove(Object element)方法,它是你想要调用的方法,还有一个remove(int index)方法,这是你实际调用的方法。 The latter tries to remove the element at the given index, which isn't there as your argument is probably greater than the list size. 后者试图删除给定索引处的元素,因为您的参数可能大于列表大小。 You can cast your ranNum variable to either Integer or Object to make sure you call the correct method. 您可以将ranNum变量ranNum转换为IntegerObject ,以确保调用正确的方法。

for (int i = 0; i <= GRID_SIZE; i++) nums.add(i);

This doesn't make much sense to me. 这对我来说没什么意义。 You're adding numbers from 0-4. 你正在添加0-4的数字。 You only have indexes up to 3 in your arrays. 数组中只有索引最多3个。 0-1-2-3... 0-1-2-3 ...

Without actually seeing more code, or knowing exactly where you're getting your index out of bounds... it's a shot in the dark. 没有真正看到更多的代码,或者确切地知道你的索引在哪里超出界限......这是一个黑暗的镜头。

How about a different approach to the problem? 如何解决问题的方法呢? Start with a valid square and transform it. 从有效的方块开始并对其进行转换。 The two operations, 'exchange two rows' and 'exchange two columns' preserve the properties of the square. 这两个操作“交换两行”和“交换两列”保留了广场的属性。 This allows you to do two Fisher-Yates shuffles, one on the rows and one on the columns which would give you a valid randomised square as long as you start from a valid square. 这允许你做两个Fisher-Yates shuffle,一个在行上,一个在列上,只要你从一个有效的方块开始就可以给你一个有效的随机方块。 Constructing an initial valid square is trivial: 构造一个初始有效方是微不足道的:

123456
234561
345612
456123
561234
612345

After giving my code a good look over again I realised that my main error was to do with the canPlace(int ranNum, int row, int col) method. 在仔细查看我的代码后,我意识到我的主要错误是使用canPlace(int ranNum, int row, int col)方法。

All I did was swap the col and row values and it worked. 我所做的就是交换colrow并且它有效。

Thank you all for your help. 感谢大家的帮助。

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

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