#include #include #include #include "param.h" #define BIAS 10 static unsigned char v[LEV_X][LEV_Y]; static unsigned char order[LEV_X*LEV_Y][2]; static int n; static unsigned char na[3]; static void dump(void) { #ifdef DETAIL int x; int y; int i; for (y=0;y=0;x--) for (y=LEV_Y-1;y>=0;y--) { order[i][0] = x; order[i][1] = y; i ++; v[x][y] = 2; } if (i != LEV_X*LEV_Y) abort(); n = LEV_X * LEV_Y; for (i=n-1;i>=0;i--) { dump(); j = random() % n; x = order[j][0]; y = order[j][1]; if (j != n-1) { order[j][0] = order[n-1][0]; order[j][1] = order[n-1][1]; } n --; #ifdef DETAIL printf("n=%d j=%d x=%d y=%d\n",n,j,x,y); #endif if (v[x][y] != 2) abort(); na[0] = 0; na[1] = 0; na[2] = 0; for (dx=-1;dx<=1;dx++) { if ((x+dx < 0) || (x+dx >= LEV_X)) continue; for (dy=-1;dy<=1;dy++) { if ((y+dy < 0) || (y+dy >= LEV_Y) || ((dx == 0) && (dy == 0))) continue; #ifdef DETAIL printf("adjacent [%d,%d] = %d\n",dx,dy,v[x+dx][y+dy]); #endif na[v[x+dx][y+dy]] ++; } } #ifdef DETAIL printf("counts %d %d %d\n",na[0],na[1],na[2]); #endif if ((na[0] > na[1]) && (random() % BIAS)) { j = 0; } else if ((na[0] < na[1]) && (random() % BIAS)) { j = 1; } else { j = random() & 1; } #ifdef DETAIL printf("picking %d\n",j); #endif v[x][y] = j; } dump(); for (y=0;y