int num = 100; Bug[] buf_bugs; Bug[][] bugs; void setup() { size(2*num, 2*num); fill(255); bugs = new Bug[num][num]; buf_bugs = new Bug[8]; for(int x = 0; x < num; x++){ for(int y = 0; y < num; y++){ bugs[x][y] = new Bug(2*x, 2*y); } } frameRate(1); } void draw() { background(0); int[] idx_buf; int x_m, x_p, y_m, y_p; for(int x = 0; x < num; x++){ for(int y = 0; y < num; y++){ x_m = v(x - 1); x_p = v(x + 1); y_m = v(y - 1); y_p = v(y + 1); buf_bugs[0] = bugs[x_m][y_m]; buf_bugs[1] = bugs[x][y_m]; buf_bugs[2] = bugs[x_p][y_m]; buf_bugs[3] = bugs[x_m][y]; buf_bugs[4] = bugs[x_p][y]; buf_bugs[5] = bugs[x_m][y_p]; buf_bugs[6] = bugs[x][y_p]; buf_bugs[7] = bugs[x_p][y_p]; bugs[x][y].update(buf_bugs); } } for(int x = 0; x < num; x++){ for(int y = 0; y < num; y++){ bugs[x][y].draw(); } } } int v(int i){ if(i >= 0){ if(i < 100){ return i; }else{ return 0; } }else{ return 99; } } class Bug { int idx, idy; int live_flag, buf_live_flag; int bug_color = 0; Bug(int x, int y){ idx = x; idy = y; buf_live_flag = live_flag = (random(0, 10) < 1) ? 1 : 0; } public int living(){ return buf_live_flag; } public void update(Bug[] bugs){ int count_living = 0; for(int i = 0 ; i < 8 ; i++){ count_living += bugs[i].living(); } if(live_flag == 1 && (count_living < 2 || count_living > 3)){ live_flag = 0; }else if(live_flag == 0 && count_living == 3){ live_flag = 1; } } public void draw(){ bug_color = (live_flag == 1) ? 255 : 0; fill(bug_color); rect(idx, idy, idx+2, idy+2); buf_live_flag = live_flag; } }