The Problem is when I start the code the game kinda works but it doesnt behave like the normal conwys game of life. So I thought my logic section where the neighbours are checked doesnt work as I it should. After long debugging I still couldnt find the mistake. :)
问题是,当我启动代码时,游戏有点起作用,但它的行为不像普通的conwys生活游戏。所以我认为检查邻居的逻辑部分并不能正常工作。经过长时间的调试,我仍然找不到错误。 :)
import java.awt.*;
import javax.swing.*;
public class GUI extends JPanel {
private Field field;
private JFrame frame;
public GUI() {
int width = 800, height = 800;
frame = new JFrame();
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(this);
field = new Field(this);
}
public static void main(String[] args) {
new GUI();
}
public void paint(Graphics g) {
super.paint(g);
int j = 0;
for (int i = 0; i < 400; i++) {
j = 0;
for (j = 0; j < 400; j++) {
if (field.getAlive(i, j)) {
g.drawRect(2 * j, 2 * i, 2, 2);
g.fillRect(2 * j, 2 * i, 2, 2);
}
}
}
check();
}
public void check() {
int j = 0;
for (int i = 1; i < 399; i++) {
j = 0;
for (j = 1; j < 399; j++) {
if (field.getAlive(i, j)) {
int x = 0;
if (field.getAlive(i - 1, j - 1)) {
x++;
}
if (field.getAlive(i, j - 1)) {
x++;
}
if (field.getAlive(i + 1, j - 1)) {
x++;
}
if (field.getAlive(i + 1, j)) {
x++;
}
if (field.getAlive(i - 1, j)) {
x++;
}
if (field.getAlive(i - 1, j + 1)) {
x++;
}
if (field.getAlive(i, j + 1)) {
x++;
}
if (field.getAlive(i + 1, j + 1)) {
x++;
}
if (x < 2 || x > 3) {
field.setAlive(i, j, false);
}
} else {
if (!field.getAlive(i, j)) {
int x = 0;
if (field.getAlive(i - 1, j - 1)) {
x++;
}
if (field.getAlive(i, j - 1)) {
x++;
}
if (field.getAlive(i + 1, j - 1)) {
x++;
}
if (field.getAlive(i + 1, j)) {
x++;
}
if (field.getAlive(i - 1, j)) {
x++;
}
if (field.getAlive(i - 1, j + 1)) {
x++;
}
if (field.getAlive(i, j + 1)) {
x++;
}
if (field.getAlive(i + 1, j + 1)) {
x++;
}
if (x == 3) {
field.setAlive(i, j, true);
}
}
}
}
}
repaint();
}
}
public class Field {
private GUI gui;
private int fieldLength = 1;
private boolean[][] alive;
public Field(GUI gui) {
this.gui = gui;
alive = new boolean[400][400];
for(int i = 100 ; i<110; i++){alive[100][i] = true; }
for(int i = 100 ; i<110; i++){alive[i][100] = true ; }
}
public boolean getAlive(int i, int j) {
return alive[i][j];
}
public void setAlive(int i, int j, boolean alive) {
this.alive[i][j] = alive;
}
}
1 个解决方案
#1
3
Your problem is caused because you are using the same board instance for checking the rules and for updating. Every iteration you should create a new board based off the old one instead of re-using the old board.
您的问题是由于您使用相同的板实例来检查规则和更新而引起的。每次迭代都应该根据旧的板创建一个新板,而不是重新使用旧板。
You should maintain two boards at any given time. One to read from and one to write to then just copy the written one at the end of each iteration
您应该在任何给定时间保留两块板。一个要读取,另一个要写入,然后在每次迭代结束时复制写入的一个
#1
3
Your problem is caused because you are using the same board instance for checking the rules and for updating. Every iteration you should create a new board based off the old one instead of re-using the old board.
您的问题是由于您使用相同的板实例来检查规则和更新而引起的。每次迭代都应该根据旧的板创建一个新板,而不是重新使用旧板。
You should maintain two boards at any given time. One to read from and one to write to then just copy the written one at the end of each iteration
您应该在任何给定时间保留两块板。一个要读取,另一个要写入,然后在每次迭代结束时复制写入的一个