Java 我如何计算John Conway中活细胞的数量';使用布尔多维数组,If语句和全局int?
我正在制作John Conway的《生命的游戏》,我在游戏中使用了2个布尔2D数组以及If语句来确定下一步的动作,即根据当前动作和每个单元格的邻居数,游戏中的单元格应该如何复制。第一个布尔2D数组称为current move,它通过将网格上的单元格显示为true(如果在网格上)和false(如果在网格上没有单元格,则显示为false)来跟踪网格上有多少单元格及其特定位置第二个布尔2D数组称为next move,if语句确定下一个当前移动应该是什么通过if语句确定它们是否足够相邻以形成新单元,或者单元是否将保持在同一点,以及是否可以形成新单元或单元保持在同一点,下一步变为真,使当前移动变为真,以供下一代使用,并且同一过程重复一次又一次。我的问题是我如何操作我的代码来计算游戏中活细胞的数量,我添加了一个名为intpopulation的变量,通过在细胞出生时增加值,在细胞死亡时减少值来计算特定点的细胞数,但我知道我做得不对,因为它没有显示正确的细胞数,我无法解决我的问题。此外,如果鼠标单击网格上的网格正方形,使当前移动在该特定位置变为真,则当前移动在该特定位置变为真。我在jtextfield中显示intpopulation值,有一个jbutton,它重置intpopulation的值,并通过创建一个新的当前move 2d数组来删除网格上的所有单元格。有人能帮我解决这个问题吗Java 我如何计算John Conway中活细胞的数量';使用布尔多维数组,If语句和全局int?,java,multidimensional-array,2d-games,conways-game-of-life,cellular-automata,Java,Multidimensional Array,2d Games,Conways Game Of Life,Cellular Automata,我正在制作John Conway的《生命的游戏》,我在游戏中使用了2个布尔2D数组以及If语句来确定下一步的动作,即根据当前动作和每个单元格的邻居数,游戏中的单元格应该如何复制。第一个布尔2D数组称为current move,它通过将网格上的单元格显示为true(如果在网格上)和false(如果在网格上没有单元格,则显示为false)来跟踪网格上有多少单元格及其特定位置第二个布尔2D数组称为next move,if语句确定下一个当前移动应该是什么通过if语句确定它们是否足够相邻以形成新单元,或者
public class GameOfLife extends javax.swing.JFrame {
int timelength = 100;
int wid = 500, hei = 250;
boolean[][] currentMove = new boolean[hei][wid], nextMove = new boolean[hei][wid];
boolean play;
public static int intpopulation = 0;
Image offScrImg;
Graphics offScrGraph;
public static int intGeneration = 0;
public GameOfLife() {
initComponents();
offScrImg = createImage(jPanel2.getWidth(), jPanel2.getHeight());
offScrGraph = offScrImg.getGraphics();
Timer time = new Timer();
TimerTask task = new TimerTask(){
public void run() {
if(play == true){
intGeneration++;
for(int i = 0; i < hei; i++){
for(int j = 0; j < wid; j++){
nextMove[i][j] = decide(i,j);
}
}
for(int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
currentMove[i][j] = nextMove[i][j];
}
}
repain();
}
}
};
time.scheduleAtFixedRate(task, 0,timelength);
repain();
}
private boolean decide(int i, int j){
int neighbors = 0;
if(j > 0){
if(currentMove[i][j-1]) neighbors++;
if(i>0) if(currentMove[i-1][j-1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j-1]) neighbors++;
}
if(j < wid - 1){
if(currentMove[i][j+1]) neighbors++;
if(i>0) if(currentMove[i-1][j+1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j+1]) neighbors++;
}
if(i>0) if(currentMove[i-1][j]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j]) neighbors++;
if(currentMove[i][j] && neighbors < 2) intpopulation--;
if(neighbors > 3) intpopulation--;
if(neighbors == 3) return true; intpopulation++;
if(currentMove[i][j] && neighbors == 2) return true;intpopulation++;
return false;
}
private void repain(){
offScrGraph.setColor(Color.BLACK);
offScrGraph.fillRect(0, 0, jPanel2.getWidth(), jPanel2.getHeight());
for (int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
if(currentMove[i][j])
{
offScrGraph.setColor(Color.getHSBColor((float) Math.random(), .8f, .8f));
int x = j * jPanel2.getWidth()/wid;
int y = i * jPanel2.getHeight()/hei;
offScrGraph.fillRect(x, y, jPanel2.getWidth()/wid, jPanel2.getHeight()/hei);
}
}
}
offScrGraph.setColor(Color.BLACK);
for (int i = 1; i < hei; i++){
int y = i * jPanel2.getHeight()/hei;
offScrGraph.drawLine(0, y, jPanel2.getWidth(), y);
}
for (int j = 1; j < wid; j++){
int x = j * jPanel2.getWidth()/wid;
offScrGraph.drawLine(x, 0, x, jPanel2.getHeight());
}
jPanel2.getGraphics().drawImage(offScrImg, 0, 0, jPanel2);
}
private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
currentMove[i][j] = !currentMove[i][j];
intpopulation++;
repain();
}
private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
if(SwingUtilities.isLeftMouseButton(evt)){
currentMove[i][j] = true;
intpopulation++;
}else currentMove[i][j] = false;
}
private void jFormattedTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
if(play)
{
String myString = Integer.toString(intpopulation);
jFormattedTextField2.setText( "population: " + myString);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
currentMove = new boolean[hei][wid];
intGeneration = 0;
intpopulation = 0;
jFormattedTextField1.setText( "Generation: " + intGeneration );
jFormattedTextField2.setText( "population: " + intpopulation );
repain();
}
公共类GameOfLife扩展了javax.swing.JFrame{
整数时间长度=100;
int wid=500,hei=250;
布尔[][]当前移动=新布尔[hei][wid],下一步移动=新布尔[hei][wid];
布尔游戏;
公共静态整数整数总体=0;
图像消隐;
图形;
公共静态int生成=0;
公共生活游戏(){
初始化组件();
offScrImg=createImage(jPanel2.getWidth(),jPanel2.getHeight());
offScrGraph=offScrImg.getGraphics();
定时器时间=新定时器();
TimerTask任务=新的TimerTask(){
公开募捐{
如果(播放==真){
intGeneration++;
对于(int i=0;i0){
if(currentMove[i][j-1])邻居++;
if(i>0)if(currentMove[i-1][j-1])邻居++;
if(i0)if(currentMove[i-1][j+1])邻居++;
if(i0)if(currentMove[i-1][j])邻居++;
如果(i3)为整数--;
if(邻居==3)返回true;intpopulation++;
if(currentMove[i][j]&&neights==2)返回true;intpopulation++;
返回false;
}
私有无效repain(){
offScrGraph.setColor(Color.BLACK);
fillRect(0,0,jPanel2.getWidth(),jPanel2.getHeight());
对于(int i=0;i
找到了我问题的解决方案
private boolean decide(int i, int j){
int neighbors = 0;
if(j > 0){
if(currentMove[i][j-1]) neighbors++;
if(i>0) if(currentMove[i-1][j-1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j-1]) neighbors++;
}
if(j < wid - 1){
if(currentMove[i][j+1]) neighbors++;
if(i>0) if(currentMove[i-1][j+1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j+1]) neighbors++;
}
if(i>0) if(currentMove[i-1][j]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j]) neighbors++;
if(currentMove[i][j]&& neighbors < 2)intpopulation--;
if(currentMove[i][j]&& neighbors > 3)intpopulation--;
if(currentMove[i][j] && neighbors == 3)intpopulation--;
if(neighbors == 3) intpopulation++;
jLabel5.setText("Population: " + Integer.toString(intpopulation));
if(neighbors == 3) return true;
if(currentMove[i][j] && neighbors == 2)return true;
return false;
}
private boolean decid