二维数组中的开元和闭元

发布于 2025-01-20 07:00:18 字数 1403 浏览 4 评论 0原文

我正在尝试使用具有打开和封闭元素的2D阵列创建网格。开放元素将由表示,封闭元素将由*表示。

在开放元素中,代表颜色的字母可以放在每个元素中。一个信件将一次放置。当一个字母放在封闭元件旁边的开放元件内时,封闭的元素将变得打开。

然后,玩家可以在下一步的新元素中放置一个字母。不得将任何封闭元素放置在任何封闭元素中。这将继续重复。

下面的图像显示了更好的视觉理解。

我使用嵌套循环创建了一个2D阵列。在此嵌套循环中,我使用IF语句用打开第一列。

我一直在尝试在此嵌套以外的循环外创建这些开放和封闭的元素,但是我似乎只能一次访问一个元素,但是我尝试访问多个元素。

我并不是在寻找可以向我发送代码或任何东西的人,但我正在寻求帮助了解如何创建这些开放和封闭的元素。

public class MatrixGrid {

    public static void main(String[] args) {
        System.out.println("Move row?");
        int r = StdIn.readInt();
        System.out.println("Move Colomn?");
        int c = StdIn.readInt();
        int m =8;
        String p = "G";
        
        
        String[][] grid = new String[m][m];
            
        for (int i=0; i<grid.length; i++) {
            
            for (int j=0; j<grid[i].length; j++) {
                if (j > 0) {
                    grid[i][j] = "*";
                } else {
                    grid[i][j] = ".";
                }
                grid[r][c] = p;
                    
                StdOut.print(grid[i][j]);
                    
            }
            StdOut.println();
        }
    }
}

I am trying to create a grid using a 2D array that has open and closed elements. The open elements will be represented by a . and closed elements will be represented by *.

In the open elements, letters representing colors are able to be placed in each element. A letter will be placed one at a time. When a letter is placed inside the open element which is next to a closed element, the closed element will then become open.

The player can then place a letter in this new open element on their next move. No letter should be allowed to be placed in any closed element. This will continue to repeat.

The image attached below shows a better visual understanding.

I have created a 2D array using a nested for loop. Within this nested loop I have used an if statement to make the first column open with ..

I have been trying to create these open and closed elements outside of this nested for loop however I can only seem to access one element at a time, however I am trying to access multiple.

I am not looking for people to send me code or anything but I am aksing for help in understanding how to create these open and closed elements.

public class MatrixGrid {

    public static void main(String[] args) {
        System.out.println("Move row?");
        int r = StdIn.readInt();
        System.out.println("Move Colomn?");
        int c = StdIn.readInt();
        int m =8;
        String p = "G";
        
        
        String[][] grid = new String[m][m];
            
        for (int i=0; i<grid.length; i++) {
            
            for (int j=0; j<grid[i].length; j++) {
                if (j > 0) {
                    grid[i][j] = "*";
                } else {
                    grid[i][j] = ".";
                }
                grid[r][c] = p;
                    
                StdOut.print(grid[i][j]);
                    
            }
            StdOut.println();
        }
    }
}

Image showing the progression of open and closed elements in a 2D array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2025-01-27 07:00:18

通过访问嵌套数组 (grid[i][j]) 元素的方式,您一次只能访问一个元素。您还可以访问完整的子数组grid[i] - 这将是一个一维数组:String[]

要访问多维数组的多个元素(例如,围绕玩家输入的位置,rc),您必须使用循环或多个单一操作,就像使用 grid[r-1][c]grid[r][c+1] 等来到达邻居。请注意不要超出数组边界(但无论如何,您都必须为网格边缘的特殊情况做好准备)。

否则,您可能有兴趣进行矩阵运算,但这是一个全新的主题,在这里可能太多了。 (核心 Java 也不直接支持它,您必须使用额外的库。)

With the way you access the elements of the nested array (grid[i][j]) you can only access one element at a time. You could also access the complete sub-array grid[i] - that would be a one-dimensional array: String[].

To access multiple elements of the multi-dimensional array (e.g. around the entered position of the player, r and c) you will have to use either loops or multiple single operations, like with grid[r-1][c], grid[r][c+1] and so on, to reach the neighbours. Take care that you do not exceed the array boundaries (but you have to prepare for special cases at the edges of the grid anyways).

Otherwise you might be interested to do matrix operations, but that is a completely new topic and probably too much for here. (It is also not directly supported by core Java, you would have to use extra libraries.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文