爪哇的单元格自动

发布于 2025-01-25 06:45:20 字数 1451 浏览 3 评论 0原文

我试图创建一个类似的康威在Java生活游戏的模型,但适应了一些特定的条件和细胞行为。我在编程方面没有足够的经验,因此,如果您能给我一些答案,我将非常好。

到目前为止,我已经拥有此代码:

package project_cells;

import java.util.Random;

public class Field {

    public static void main(String[] args) {
        int size = 4;
        char[][] field = new char[size][size];          // field 
        char[] cell_type = {'A','B','a','b'};          // fill out vector with cell type

        for(int i = 0; i <size; i++ ) {                 // checking the matrix positions
            for (int j = 0; j <size; j++) {
                Random rand = new Random();  
                int celltype_option = rand.nextInt((3-0)+1)+0;       // creating a random number from 0 to 3 
                field[i][j]=cell_type[celltype_option];             // Filling the field with cell types
            }
        }
        System.out.println("Клетки первого поколения");                     // checking the first generation of cells
        for (int x=0; x < field.length; x++) {
            System.out.print("|");
            for (int y=0; y < field[x].length; y++) {
                System.out.print (field[x][y]);
                if (y!=field[x].length-1) System.out.print("\t");

            }
            System.out.println("|");
        }
    }
}

使用此代码,我只会获得4种类型的单元格(a,b,a,b)a和b改变其行为的数组,a和b不会改变。

问题是我需要创建3种验证邻居的方法:1。当单元仅验证4个邻居时(向上,右,左,下)。 2。当细胞验证8个周围的邻居和3时,当细胞验证任何随机邻居时。

我如何获得这样的验证?

I am trying to create a similar model of Conway's game of life in Java, but adapted to some specific conditions and behavoiur of cells. I do not have enough experience in programming so, I will be greateful if you can give me some answers.

I have this code so far:

package project_cells;

import java.util.Random;

public class Field {

    public static void main(String[] args) {
        int size = 4;
        char[][] field = new char[size][size];          // field 
        char[] cell_type = {'A','B','a','b'};          // fill out vector with cell type

        for(int i = 0; i <size; i++ ) {                 // checking the matrix positions
            for (int j = 0; j <size; j++) {
                Random rand = new Random();  
                int celltype_option = rand.nextInt((3-0)+1)+0;       // creating a random number from 0 to 3 
                field[i][j]=cell_type[celltype_option];             // Filling the field with cell types
            }
        }
        System.out.println("Клетки первого поколения");                     // checking the first generation of cells
        for (int x=0; x < field.length; x++) {
            System.out.print("|");
            for (int y=0; y < field[x].length; y++) {
                System.out.print (field[x][y]);
                if (y!=field[x].length-1) System.out.print("\t");

            }
            System.out.println("|");
        }
    }
}

With this code I get only the array with the 4 type of cells (A,B,a,b) A and B change its behaviour, a and b don't change.

The issue is that I need to create 3 ways to verify the neighbors: 1. When cell verifies only 4 neighbors (up, right, left, down). 2. When cell verifies 8 surrounding neighbors and 3. When cell verifies any random neighbor.

How can I get such verifications?

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

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

发布评论

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

评论(1

淡淡的优雅 2025-02-01 06:45:20

x的左右邻居是x-1x+1。它们仅在它们是&gt; = 0&lt; = size -1的情况下存在,

for (int i = -1; i <= 1; i+= 2) { // i = -1, +1
    final int neighborIndex = x + i; 
    if (neighborIndex >= 0 && neighborIndex < size) {
        // ...   
    }
}

可以类似地处理UP/DOWN CASE。

要获取所有八个邻居,请从X-1 到x+1,从y-1y+ 1(再次,仅当&gt; = 0&lt; size)并过滤掉中心(x,x,y)

for (int xNeighbor = Math.max(0, x - 1); xNeighbor <= Math.min(size - 1, x + 1); xNeighbor += 1) {
    for (int yNeighbor = Math.max(0, y - 1); yNeighbor <= Math.min(size - 1, y + 1); yNeighbor += 1) {
        if (!(xNeighbor == x && yNeighbor == y)) {
            // ...
        }
    }
}

要获取一个随机的邻居,请先计算邻居的数量,然后使用rand.nextint(numneighbors)选择一个。

The left and right neighbors of x are x-1 and x+1. They only exist if they are >= 0 and <= size - 1:

for (int i = -1; i <= 1; i+= 2) { // i = -1, +1
    final int neighborIndex = x + i; 
    if (neighborIndex >= 0 && neighborIndex < size) {
        // ...   
    }
}

The up/down case can be handled analogously.

To get all eight neighbors, iterate from x-1 to x+1 and the inside it from y-1 to y+1 (again, only if >=0 and < size) and filter out the center (x, y):

for (int xNeighbor = Math.max(0, x - 1); xNeighbor <= Math.min(size - 1, x + 1); xNeighbor += 1) {
    for (int yNeighbor = Math.max(0, y - 1); yNeighbor <= Math.min(size - 1, y + 1); yNeighbor += 1) {
        if (!(xNeighbor == x && yNeighbor == y)) {
            // ...
        }
    }
}

To get a random neighbor, first count the number of neighbors and then use rand.nextInt(numNeighbors) to select one.

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