import processing.core.PApplet;
public class gl extends PApplet {
static int neighborCount;
static int screenRows;
int tNC; // Temporary Neighbor Count
int newState;
int columns = 960;
int rows = 477;
int[][] cells = new int[columns][rows];
int[][] newGen = new int[columns][rows];
public static void main(String[] args) {
PApplet.main("gl");
}
public void settings() {
size(1920, 955);
}
public void setup() {
// Set background white and all of cells[][] to 0 or 1
screenRows = 0;
background(255);
for (int j = 0; j < (rows/2); j++) {
for (int i = 0; i < (columns/2); i++) {
cells[i][j] = (int) random(0, 2);
}
}
}
public void draw() {
// If program has finished generating this frame, reset everything and set cells[][] equal to newGen[][]
if (screenRows > (height/2)) {
screenRows = 0;
System.out.println("End of generation reached");
background(255);
cells = newGen.clone();
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
newGen[i][j] = 0;
}
}
}
// Go through every element in cells[][], determine it's value, and display it
for (int x = 1; x < (width/2) - 1; x++) {
for (int y = 1; y < (height/2) - 1; y++) {
printCell(x, y);
}
}
screenRows++;
}
public void printCell(int x, int y) {
setCellState(x, y);
if (newGen[x][y] == 0) {
stroke(255);
fill(255);
} else if (newGen[x][y] == 1) {
stroke(0);
fill(0);
}
System.out.println(x + ", " + y);
rect(x, y, 2, 2);
}
public void setCellState(int x, int y) {
tNC = getNeighborCount(x, y);
neighborCount = 0;
System.out.println(tNC);
if (tNC < 2) { // If less than 2 neighbors, cell dead
newGen[x][y] = 0;
} else if (tNC > 3) { // If more than 3 neighbors, cell dead
newGen[x][y] = 0;
} else if ((tNC == 2 || tNC == 3) && cells[x][y] == 1) { // If 2 or 3 neighbors and cell is alive, do nothing (unnecessary statement but makes visualizing easier)
} else if (tNC == 3 && cells[x][y] == 0) { // If 3 neighbors and cell is dead, cell is alive
newGen[x][y] = 1;
} else if (tNC == 2 && cells[x][y] == 0) { // If 2 neighbors and cel is dead, do nothing (also unnecessary)
} else {
System.out.println("Error in setCellState(int, int);"); // In event of none of the conditions being met
}
tNC = 0; // Reset variable (probably unnecessary but might as well)
}
public int getNeighborCount(int x, int y) {
// Go through each cell adjacent or diagonal to the cell and add it's value (0 or 1) to neighborCount
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
neighborCount += cells[i + x][j + y];
}
}
// Subtract the value of the cell being evaluated from neighborCount as that is not a factor in the sum of the neighbors
neighborCount -= cells[x][y];
return neighborCount;
}
}
私はちょうど今のところ、スピードの上に機能性のためつもりです。
私はConwayのGame of LifeをEclipseでの処理を使ってコーディングしようとしています。上記のコードは複数の点で機能不全です:
表示されている世代は、私がしたいと思うよりはるかに小さいです。各セルを2×2ピクセルにし、ウィンドウの高さと幅が半分になるようにすることで、これを相殺する努力をしているにもかかわらず、ウィンドウのほんの一部しか占めていません。
また、数秒後に最初の世代が表示された後、ウィンドウで世代が更新されないように見えます。
私はそれはあなたは三つの主要な問題を持っている0〜7
私はあなたの提案を試みて、新たに始めました。それは確かにきれいで、よりコンパクトで、読みやすいですが、まだ機能していません。さらなるアドバイスのためにここに戻ってくる前に、私はしばらく試し続けます。私はそのクラスの人ではありませんが、私はダニエル・シフマンの指導の章を読んだことがあります(しかし、私のコードの大半はオリジナルであると思いますが、少なくとも私は個別に考えました)。 – sirmax224
もう一度立ち往生した場合は、新しいコードで新しい投稿を作成することをおすすめします。私は手伝ってくれることを楽しみにしています。ああ、私はコードや何かを盗んだことを非難していませんでしたが、私はちょうどそれが面白い偶然だったと思いました! –
私は進歩したと信じていますが、私のGame of Lifeは機能的ではありません。それは何とか近隣を間違って数えたり、誤って和を解釈したり、それらの行に沿って何かを解釈したりするようです。助けてくれませんか?ありがとうございましたhttp://stackoverflow.com/questions/38382356/why-are-the-generations-in-my-game-of-life-using-processing-out-of-order – sirmax224