爪哇的单元格自动
我试图创建一个类似的康威在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
x
的左右邻居是x-1
和x+1
。它们仅在它们是&gt; = 0
和&lt; = size -1
的情况下存在,可以类似地处理UP/DOWN CASE。
要获取所有八个邻居,请从X-1 到
x+1
,从y-1
到y+ 1
(再次,仅当&gt; = 0
和&lt; size
)并过滤掉中心(x,x,y)
:要获取一个随机的邻居,请先计算邻居的数量,然后使用
rand.nextint(numneighbors)
选择一个。The left and right neighbors of
x
arex-1
andx+1
. They only exist if they are>= 0
and<= size - 1
:The up/down case can be handled analogously.
To get all eight neighbors, iterate from
x-1
tox+1
and the inside it fromy-1
toy+1
(again, only if>=0
and< size
) and filter out the center(x, y)
:To get a random neighbor, first count the number of neighbors and then use
rand.nextInt(numNeighbors)
to select one.