/* http://www.mrwadlo.com */ #include #include #include #define MAXX 90 #define MAXY 90 #define SCREENX 80 /* normal is 80 wide */ #define SCREENY 25 /* normal is 25. 50 for double hight */ int posx, posy; int pagex, pagey; int readi = 0; int readmode = -1; long one_sec; int table[MAXX][MAXY]; char crawl[100]; char tempc[120] = { 0 }; void builder(); void initialize(); void displaycursor(); void peekloc(int x, int y); void update(); void calc_time(); void count_100ths(int hund); int SaveScreen(); int LoadScreen(); void fill(); void redraw(); void trigger(char chr); void NextTrigger(); void Scroller(); void IncString(); void SetString( char array[] ); void beep(); int main() { initialize(); builder(); return 0; } void initialize() { FILE *fpCHECKr; int i; char chr; posx = 0; posy = 0; pagex = 0; pagey = 0; textcolor(7); clrscr(); calc_time(); fpCHECKr = fopen("Domino.sav","rb"); fclose(fpCHECKr); if ( fpCHECKr != 0 ) { i = ((SCREENX/2)-13); if (i<2) i=2; gotoxy(i,(SCREENY/2)); printf("Load saved file? [Y]es [N]o"); do { chr = getch(); if ( chr != 'n' && chr != 'N' && chr != 'y' && chr != 'Y' ) beep(); } while ( chr != 'y' && chr != 'Y' && chr != 'n' && chr != 'N' ); i = ((SCREENX/2)-13); if (i<2) i=2; gotoxy(i,(SCREENY/2)); for ( i=0 ; i<27 ; i++ ) printf(" "); if ( chr == 'y' || chr == 'Y' ) { if ( LoadScreen() == 1 ) readmode = 300; else readmode = 301; } } redraw(); IncString(); for ( i=0 ; i< 78 ; i++ ) crawl[i] = ' '; } void builder() { char chr = 'x'; int draw = 0; int i,j; while ( chr != 27 ) { while ( kbhit() ) { chr = getch(); if ( chr == 0 ) /* ExtendedKey */ { chr = getch(); if ( chr == 75 ) /* LeftArrow */ { if ( posx > 0 ) { posx--; peekloc(posx+1, posy); } else if ( posx+pagex == 0 ) beep(); else if ( pagex > 0 ) { pagex-=1; while (posx+pagex < 0 ) pagex++; for ( j=0 ; j<(SCREENY-2) ; j++ ) for ( i=0 ; i<(SCREENX-1) ; i++ ) peekloc(i,j); redraw(); } } else if ( chr == 77 ) /* RightArrow */ { if ( (posx < (SCREENX-2) && posx+pagex < MAXX-1 && pagex==0) || (posx < (SCREENX-1) && posx+pagex < MAXX-1 && pagex>0) ) { posx++; peekloc(posx-1, posy); } else if ( pagex+(SCREENX-1) == MAXX ) beep(); else if ( posx+pagex < MAXX ) { if ( pagex == 0 ) { posx++; if ( draw == 1 ) table[posx+pagex][posy+pagey] = 1; } pagex+=1; while ( posx+pagex > MAXX ) pagex--; if ( posx+pagex > MAXX-1 ) posx--; redraw(); } } else if ( chr == 72 ) /* UpArrow */ { if ( posy > 0 ) { posy--; peekloc(posx, posy+1); } else if ( pagey > 0 ) { pagey-=1; while ( posy+pagey < 0 ) pagey++; redraw(); } else beep(); } else if ( chr == 80 ) /* DownArrow */ { if ( (posy < (SCREENY-3) && posy+pagey < MAXY-1 && pagey==0) || (posy < (SCREENY-2) && posy+pagey < MAXY-1 && pagey>0) ) { posy++; peekloc(posx, posy-1); } else if ( pagey+(SCREENY-2) == MAXY ) beep(); else if ( posy+pagey < MAXY ) { if ( pagey == 0 ) { posy++; if ( draw == 1 ) table[posx+pagex][posy+pagey] = 1; } pagey+=1; while ( posy+pagey > MAXY ) pagey--; if ( posy+pagey > MAXY-1 ) posy--; redraw(); } } else if ( chr == 59 && readmode < 99 ) /* F1 (Help) */ readmode = 100; else if ( chr == 60 ) /* F2 (Save) */ { if ( SaveScreen() == 1 ) readmode = 200; else readmode = 201; } else if ( chr == 61 ) /* F3 (Load) */ { if ( LoadScreen() == 1 ) readmode = 300; else readmode = 301; draw = 0; } else if ( chr == 87 ) /* SF4 (New) */ { for ( j=0 ; j= '0' && chr <= '9' ) /* Trigger SET */ { table[posx+pagex][posy+pagey] = 100 + chr - '0'; draw = 0; } else if ( chr == '!' || chr == '@' || chr == '#' || chr == '$' || chr == '%' || chr == '^' || chr == '&' || chr == '*' || chr == '(' || chr == ')' ) /* Trigger ON */ trigger(chr); else if ( chr == '+' || chr == '=' ) /* Next Trigger */ { NextTrigger(); } /* /////////////////////////// //gotoxy(40,40); printf("posx,posy = %d,%d pagex,pagey = %d,%d",posx,posy,pagex,pagey); //gotoxy(30,30); printf("posy+pagey (%d) > MAXY (%d)",posy+pagey, MAXY); /////////////////////////// */ } displaycursor(); update(); Scroller(); count_100ths(20); } } void calc_time() { time_t time1; time1 = time(NULL); gotoxy((SCREENX/2)-10,(SCREENY/2)); printf("Calculating CPU Speed..."); while( time(NULL) == time1 ) ; time1 = time(NULL); while( time(NULL) < (time1 + 2) ) { one_sec++; printf(" \b"); } clrscr(); one_sec /= 2; } void count_100ths(int hund) { long i = 0; time_t time1; gotoxy (1,(SCREENY)); while ( i < ( one_sec / 100 * hund ) ) { printf(" \b"); time1 = time(NULL); i++; } } void displaycursor() { int x,y; time_t time1; time1 = time(NULL); x = posx; y = posy; if ( time1 % 3 <= 1 ) { if ( pagex == 0 ) x++; if ( pagey == 0 ) y++; textcolor(14); gotoxy(x+1,y+1); cprintf("%c",219); textcolor(7); } else peekloc(x,y); } void peekloc(int x, int y) { int chr; if ( x>=0 && x<=(SCREENX) && y>=0 ) { if ( ( y <= (SCREENY-2) && pagey != 0 && pagey+(SCREENY-2) != MAXY ) || ( y <= (SCREENY-3) && pagey == 0 ) || ( y <= (SCREENY-3) && pagey+(SCREENY-2) == MAXY ) ) { if (( x < (SCREENX) && pagex+(SCREENX-1) != MAXX && pagex != 0 ) || ( x < (SCREENX-1) && pagex+(SCREENX-1) == MAXX) || ( x < (SCREENX-1) && pagex == 0) ) { chr = table[x+pagex][y+pagey]; if ( pagex == 0 ) x++; if ( pagey == 0 ) y++; gotoxy(x+1,y+1); if ( chr == 0 ) cprintf(" "); else if ( chr == 1 || chr == 10 ) cprintf("%c",219); else if ( chr == 2 ) cprintf("%c",178); else if ( chr == 3 ) { textcolor(8); cprintf("%c",219); textcolor(7); } else if ( chr == 4 ) { textcolor(8); cprintf("%c",178); textcolor(7); } else if ( chr == 5 ) { textcolor(8); cprintf("%c",177); textcolor(7); } else if ( chr == 9 ) cprintf("%c",14); else if ( chr >= 100 && chr <= 109 ) cprintf("%d",chr-100); } } } } void update() { int i,j,k; for ( k=5 ; k>0 ; k-- ) { for ( j=0 ; j 1 && table[i][j] == k ) { table[i][j]++; peekloc(i-pagex,j-pagey); if ( table[i][j] > 5 ) { table[i][j] = 0; peekloc(i-pagex,j-pagey); } } else if ( table[i][j] == 1 && ( ( i>0 && table[i-1][j] == 3 ) || ( i<(MAXX-1) && table[i+1][j] == 3 ) || ( j>0 && table[i][j-1] == 3 ) || ( j<(MAXY-1) && table[i][j+1] == 3 ) ) ) { table[i][j]++; peekloc(i-pagex,j-pagey); } else if ( table[i][j] == 9 && ( ( i>0 && table[i-1][j] == 3 ) || ( i<(MAXX-1) && table[i+1][j] == 3 ) || ( j>0 && table[i][j-1] == 3 ) || ( j<(MAXY-1) && table[i][j+1] == 3 ) ) ) { beep(); table[i][j] = 0; peekloc(i-pagex,j-pagey); } } } } } int SaveScreen() { int i,j; FILE *fpSAVEw; fpSAVEw = fopen("Domino.sav","wb"); if ( fpSAVEw == 0 ) { fclose(fpSAVEw); return 0; } for ( j=0 ; j0 && table[i][j-1] == 10 ) || ( j0 && table[i-1][j] == 10 ) || ( i= 100 && table[i][j] <=109 && table[i][j] < low_value ) { low_value = table[i][j]; low_x = i; low_y = j; } if ( low_x != posx+pagex || low_y != posy+pagey ) { posx = (SCREENX/2); pagex = low_x - (SCREENX/2); posy = (SCREENY/2); pagey = low_y - (SCREENY/2); while ( pagex < 0 ) { posx--; pagex++; } while ( pagey < 0 ) { posy--; pagey++; } while ( pagex+(SCREENX-1) > MAXX ) { posx++; pagex--; } while ( pagey+(SCREENY-2) > MAXY ) { posy++; pagey--; } redraw(); } else beep(); } void Scroller() { int i; if ( readi < 0 ) { for ( i=0 ; i<(SCREENX-3) ; i++ ) crawl[i] = crawl[i+1]; crawl[(SCREENX-3)] = ' '; readi++; } else if ( tempc[readi] == 0 ) { IncString(); readi = -15; for ( i=0 ; i<(SCREENX-3) ; i++ ) crawl[i] = crawl[i+1]; crawl[(SCREENX-3)] = ' '; } else { for ( i=0 ; i<(SCREENX-3) ; i++ ) crawl[i] = crawl[i+1]; crawl[(SCREENX-3)] = tempc[readi]; readi++; } gotoxy(2,(SCREENY)); printf("%s",crawl); } void IncString() { readmode = ( readmode + 1 ); if ( readmode > 13 && readmode < 99 ) readmode = 0; switch ( readmode ) { case 0: SetString("[F1] Help$"); break; case 1: SetString("[F2] Save$"); break; case 2: SetString("[F3] Load$"); break; case 3: SetString("[SHIFT F4] Clear All$"); break; case 4: SetString("[F5] Draw ON/OFF$"); break; case 5: SetString("[F6] Fill$"); break; case 6: SetString("[F8] Tone$"); break; case 7: SetString("[F9] Dot ON$"); break; case 8: SetString("[F10] Dot OFF$"); break; case 9: SetString("[0-9] Trigger SET$"); break; case 10: SetString("[SHIFT 0-9] Trigger ON$"); break; case 11: SetString("[SPACE] Free Trigger ON$"); break; case 12: SetString("[+] Next Trigger$"); break; case 13: SetString("[ESC] Quit$"); break; case 101: SetString("To use The Domino Simulator, add several dominos next to each other and then set off a trigger next to a domino.$"); break; case 102: SetString("The viewing window can be scrolled in any direction as long as there is no visible border. Use the arrow keys to scroll.$"); break; case 103: SetString("Triggers are used as starting points for chain reactions. There are ten sets, and each set can contain several triggers.$"); break; case 104: SetString("For a simple example of a chain reaction, press [F6] followed by [SPACE].$"); break; case 105: SetString("To draw, fill, place, and clear dominos, and to manipulate triggers, use the keys provided as follows:$"); readmode = 2; break; case 201: SetString("Configuration Saved to DOMINO.SAV$"); readmode = 2; break; case 202: SetString("Error Saving to DOMINO.SAV$"); readmode = 2; break; case 301: SetString("Configuration Loaded from DOMINO.SAV$"); readmode = 2; break; case 302: SetString("Error Loading from DOMINO.SAV$"); readmode = 2; break; } } void SetString( char array[] ) { int i=0; while ( i<120 && array[i] != '$' ) { tempc[i] = array[i]; i++; } tempc[i] = 0; } void beep() { printf("\a"); }