int columns = 25; // 縦方向 - y int rows = 25; // 横方向 - x int[][] current = new int[columns][rows]; int[][] next = new int[columns][rows]; void keyPressed() { for (int y = 0; y < columns; y++) { for (int x = 0; x < rows; x++) { // Initialize each cell with a 0 or 1. current[y][x] = int(random(2)); } } } void setup() { size(500, 500); frameRate(5); for (int y = 0; y < columns; y++) { for (int x = 0; x < rows; x++) { // Initialize each cell with a 0 or 1. current[y][x] = int(random(2)); } } } void draw() { background(0); println(frameRate); int x, y; for ( y = 1; y < columns-1; y++ ) { for ( x = 1; x < rows-1; x++ ) { int neighbors = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { neighbors += current[y+i][x+j]; } } neighbors -= current[y][x]; if ( (current[y][x] == 1) && (neighbors < 2 )) { next[y][x] = 0; } else if ( (current[y][x] == 1)&&(neighbors > 3 )) { next[y][x] = 0; } else if ( (current[y][x] ==0 )&& (neighbors == 3 )) { next[y][x] = 1; } else { next[y][x] = current[y][x]; } } } current = next; float width_cell = width/rows; float height_cell = height/columns; noStroke(); for ( y = 0; y < columns; y+=1 ) { for ( x = 0; x < rows; x+=1 ) { fill(255*current[y][x]); rect(x*width_cell,y*height_cell,width_cell,height_cell); } } }