Esempio n. 1
0
static void evolve(char *field, int w, int h) {
	int i, j, alive, cell;

	for (i = 0; i < h; i++) {
		for (j = 0; j < w; j++) {
			alive = countAlive(field, i, j, w, h);
			cell = CELL(i, j, w);

			if (cell == ALIVE) {
				if (alive > 3 || alive < 2) {
					KILL(i, j, w);
				} else {
					VIVIFY(i, j, w);
				}
			} else {
				if (alive == 3) {
					VIVIFY(i, j, w);
				} else {
					KILL(i, j, w);
				}
			}
		}
	}
}
int main(int argc, char **argv) {
	int x, y, z, i, j, it;
	int w, h, d;
	int MAXITER;
	
	//scanf("%d %d %d", &w, &h, &d);
	w = atoi(argv[1]); h = atoi(argv[2]); d = atoi(argv[3]), MAXITER = atoi(argv[4]);
	unsigned char *A = (unsigned char*) malloc((w+2)*(h+2)*(d+2)*sizeof(unsigned char));
	unsigned char *B = (unsigned char*) malloc((w+2)*(h+2)*(d+2)*sizeof(unsigned char));
	int *coords = (int*) malloc(NC*sizeof(int));

	srand(time(NULL));
	generateRandom(w, h, d, 0.3, A);

	// Read shit
	/*
	for (z = 0; z < d; z++) {
		for (y = 0; y < h; y++) {
			for (x = 0; x < w; x++) {
				scanf("%hhu", A + z*w*h + y*w + x);
			}
		}
	}
	*/
	

	printf("%d %d %d %d\n", MAXITER+1, w, h, d);
	//printMap(A, w, h, d);

	clock_t start = clock();

	for (it = 0; it < MAXITER; it++) {
		//fprintf(stderr, "%d\n", it);
		printf("%d %f\n", it, (clock() - start)/(CLOCKS_PER_SEC*1.0));
		for (z = 1; z <= d; z++) {
			for (y = 1; y <= h; y++) {
				for (x = 1; x <= w; x++) {
		
					int count = countAlive(x, y, z, w, h, d, A, coords);
					int idx = z*w*h + y*w + x;

					if (A[idx] == 1) {		// Is alive
						if (count == 4 || count == 5) {
							B[idx] = 1;
						} else {
							B[idx] = 0;
						}
					} else {					// Is dead
						if (count == 4) {
							B[idx] = 1;
						} else {
							B[idx] = 0;
						}
					}
				}
			}
		}

		//printMap(B, w, h, d);
		unsigned char *tmp = A;
		A = B;
		B = tmp;
	}

	printf("%f\n", (clock() - start) / (CLOCKS_PER_SEC * 1.0));

	


	free(A);
	free(B);
	free(coords);
}