/* http://www.mrwadlo.com */ #include #include #include #include int spaces[81][26]; long counter = 0; char ext = 'n'; int pri_red=1; int pri_blue=1; int pri_green=1; int pri_yellow=1; char view_type = '.'; char make_log = '.'; FILE *fpWRITE; int randomize(); void initialize(); void display(); void cycle(); void countdisplay(); void gettype(); int main() { clrscr(); gettype(); initialize(); if ( make_log == 'Y' || make_log == 'y' ) fpWRITE = fopen("colorlog.txt", "w"); while ( ext == 'n' && !kbhit() ) { display(); cycle(); if ( counter % 50 == 0 ) { countdisplay(); } counter++; } if ( make_log == 'Y' || make_log == 'y' ) fclose(fpWRITE); getch(); return 0; } int randomize() { return (( rand() + time(NULL)) % 5 + 1); } void initialize() { int i, j; for ( j=1 ; j<25 ; j++ ) { for ( i=1 ; i<81 ; i++ ) { if ( randomize() != 1 ) spaces[i][j] = 0; else if ( i == 1 || i == 80 || j == 1 || j == 25 ) spaces[i][j] = 0; else { switch ( randomize() ) { case 1: spaces[i][j] = 9; /* blue */ break; case 2: spaces[i][j] = 10; /* green */ break; case 3: spaces[i][j] = 12; /* red */ break; case 4: spaces[i][j] = 14; /* yellow */ break; case 5: spaces[i][j] = 0; /* black */ break; } } } } } void display() { int i, j; gotoxy(1,1); for ( i=2 ; i<25 ; i++ ) { gotoxy(2,i); for ( j=2 ; j<80 ; j++ ) { textcolor(spaces[j][i]); cprintf("%c", 219); } } } void cycle() { int i, j; int try; int seq; char go; for ( i=1 ; i<81 ; i++ ) for ( j=1 ; j<25 ; j++ ) { if ( spaces[i][j] >= 9 ) { go = 'f'; if (( (spaces[i][j] == 9 && pri_blue >= counter % 4 + 1) || (spaces[i][j] == 10 && pri_green >= counter % 4 + 1) || (spaces[i][j] == 12 && pri_red >= counter % 4 + 1) || (spaces[i][j] == 14 && pri_yellow >= counter % 4 + 1) ) && (view_type == 'p' || view_type == 'P')) go = 't'; if (( (spaces[i][j] == 9 && pri_blue <= counter % 4 + 1) || (spaces[i][j] == 10 && pri_green <= counter % 4 + 1) || (spaces[i][j] == 12 && pri_red <= counter % 4 + 1) || (spaces[i][j] == 14 && pri_yellow <= counter % 4 + 1) ) && (view_type == 'i' || view_type == 'I')) go = 't'; if (( view_type == 'n' ) || ( view_type == 'N' )) go = 't'; if ( go == 't' ) { seq = randomize(); try = 0; while ( try < 4 && seq != 5 ) { if ( seq == 1 && spaces[i+1][j] != spaces[i][j] && i<79 ) { spaces[i+1][j] = spaces[i][j]; } else if ( seq == 2 && spaces[i][j-1] != spaces[i][j] && j>2 ) { spaces[i][j-1] = spaces[i][j]; } else if ( seq == 3 && spaces[i-1][j] != spaces[i][j] && i>2 ) { spaces[i-1][j] = spaces[i][j]; } else if ( seq == 4 && spaces[i][j+1] != spaces[i][j] && j<24) { spaces[i][j+1] = spaces[i][j]; } try++; if ( seq > 4 ) seq = 1; } } } } } void countdisplay() { int i, j; int blue=0, green=0, red=0, yellow=0; for ( i=2 ; i<80 ; i++ ) { gotoxy(2,i); for ( j=2 ; j<25 ; j++ ) { if ( spaces[i][j] == 9 ) { blue++; } else if ( spaces[i][j] == 10 ) { green++; } else if ( spaces[i][j] == 12 ) { red++; } else if ( spaces[i][j] == 14 ) { yellow++; } } } gotoxy(10,25); for ( i=10 ; i<70 ; i++) printf(" "); gotoxy(2,25); textcolor(1); cprintf("%ld", counter/100); if ( blue > 0 ) { gotoxy(10,25); textcolor(9); cprintf("Blue: %4d", blue); } if ( green > 0 ) { gotoxy(25,25); textcolor(10); cprintf("Green: %4d", green); } if ( red > 0 ) { gotoxy(40,25); textcolor(12); cprintf("Red: %4d", red); } if ( yellow > 0 ) { gotoxy(55,25); textcolor(14); cprintf("Yellow %4d", yellow); } if ( blue==1794 || green==1794 || red==1794 || yellow==1794 ) { textcolor(7); cprintf(" <-- Winner"); ext = 'y'; } pri_yellow = yellow * 4 / 1794 + 1; pri_blue = blue * 4 / 1794 + 1; pri_green = green * 4 / 1794 + 1; pri_red = red * 4 / 1794 + 1; /* gotoxy(20,26); printf("pri_yellow = %d", pri_yellow); gotoxy(20,27); printf("pri_blue = %d", pri_blue); gotoxy(20,28); printf("pri_green = %d", pri_green); gotoxy(20,29); printf("pri_red = %d", pri_red); */ if ( make_log == 'Y' || make_log == 'y' ) fprintf(fpWRITE, "%d\t%d\t%d\t%d\n", blue, green, red, yellow); } void gettype() { textcolor(7); gotoxy(30,8); cprintf("Enter View Type:"); gotoxy(32,10); cprintf("[N]ormal"); gotoxy(32,11); cprintf("[P]riority"); gotoxy(32,12); cprintf("[I]nvert Priority"); do { view_type = getch(); }while ( view_type != 'n' && view_type != 'N' && view_type != 'p' && view_type != 'P' && view_type != 'i' && view_type != 'I' ); gotoxy(30,14); cprintf("Would You A Log?"); gotoxy(32,16); cprintf("[Y]es"); gotoxy(32,17); cprintf("[N]o"); do { make_log = getch(); }while ( make_log != 'n' && make_log != 'N' && make_log != 'y' && make_log != 'Y' ); }