home *** CD-ROM | disk | FTP | other *** search
- /* bomb.c, by Eric Olson. */
- /* version 1.0 */
- /* This program is copyright 1991 by Eric Olson. You are welcome to
- distribute it, but this notice should remain intact and you should
- include all the files that were distributed with it. This program is
- not to be sold, although a fee may be charged to cover distribution
- expenses (if there are any). If you wish to modify the code and
- distribute it, please feel free to do so, but label the code as
- modified here and change the version accordingly. */
-
- #include <stdio.h>
- #include <curses.h>
-
- #ifdef VMS
- unsigned char _getch() /* kent d ramier 20 feb 88 */ /* eo ruined spacing */
- { struct { unsigned short length; unsigned char dtype; unsigned char
- class; char *pointer; } device = { 9, 14, 1, "SYS$INPUT"}; static
- unsigned short a_channel = 0; unsigned char the_key; if (!(a_channel))
- SYS$ASSIGN(&device,&a_channel,0,0); SYS$QIOW
- (0,a_channel,625,0,0,0,&the_key,1,1,0,0,0); return(the_key); }
- # define GETCH (_getch())
- #else
- /* if you're not on VAX/VMS, you may need to refit this. */
- /* it does occasionally leave numbers on my screen. */
- # define GETCH (getch())
- #endif VMS
-
- #define MAX 16 /* boardsize */
-
- /* this is a terrible random generator. Make a better one. */
- #include <math.h>
- #include <time.h>
- #define RANDOM ((int)((float)rand()/((float)((unsigned)(-1)))*2.0*(float)(MAX)))
- #define RANDOM_SEED srand(time((time_t *)0))
-
- short think[MAX][MAX],grid[MAX][MAX],boom;
- int xx=0,yy=0;
-
- #define NOTYET 9
- #define BOMB 10
- #define OKAY 11
- static char icon[]=" 12345678.*o";
-
- drawscr()
- {
- int i,j;
-
- for (i=0;i<MAX;i++)
- for (j=0;j<MAX;j++)
- {
- move(j,i*2);
- /* printw("(%c)",icon[think[i][j]]); */
- printw(" %c",icon[think[i][j]]);
- }
- /* mvaddch(yy,xx*2+1,'#');
- mvaddch(yy,xx*2,'['); mvaddch(yy,xx*2+2,']');
- mvaddch(yy,xx*2,'>'); */
- }
-
- short invalid(i,j)
- int i,j;
- {
- if ((i==j) && ((i==0) || (i==MAX-1))) return (1);
- if ((i<2) && (j<2)) return (1);
- if ((i==MAX) || (j==MAX)) return (1);
- if (grid[i][j]) return (1);
- return (0);
- }
-
- analyze(x,y)
- int x,y;
- {
- int i,j,t=0;
-
- for (i= -1;i<2;i++)
- for (j= -1;j<2;j++)
- if ((i+x>=0) && (j+y>=0) && (i+x<MAX) && (j+y<MAX))
- if (grid[i+x][j+y]) t++;
- think[x][y]=t;
- if (!t)
- for (i= -1;i<2;i++)
- for (j= -1;j<2;j++)
- if ((i+x>=0) && (j+y>=0) && (i+x<MAX) && (j+y<MAX))
- if (think[i+x][j+y]==NOTYET) analyze(i+x,j+y);
- }
-
- toggle(x,y)
- int x,y;
- {
- switch (think[x][y])
- {
- case NOTYET: think[x][y]=BOMB; break;
- case BOMB: think[x][y]=OKAY; break;
- case OKAY: think[x][y]=NOTYET; break;
- }
- }
-
- moveto(x,y)
- {
- int i,j,q=0;
-
- for (i= -1;i<2;i++)
- for (j= -1;j<2;j++)
- if ((i+x>=0) && (i+x<MAX) && (j+y<MAX) && (j+y>=0))
- if (think[i+x][j+y]<NOTYET) q=1;
- if (!q) return(0);
-
- xx=x; yy=y; analyze(x,y);
-
- if (grid[x][y]) boom=2;
- if ((y==MAX-1) && (x==MAX-1)) boom=1;
- }
-
- main(argc,argv)
- int argc; char **argv;
- {
- int i,j,k,nb=30;
- char c;
-
- if (argc>1)
- {
- sscanf(argv[1],"%d",&nb);
- if ((nb<1) || (nb>(MAX*(MAX-2))))
- {
- printf ("Invalid number of bombs."); exit(0);
- }
- }
-
- initscr();
- RANDOM_SEED;
-
- for (i=0;i<MAX;i++)
- for (j=0;j<MAX;j++)
- think[i][j]=NOTYET,grid[i][j]=0;
- for (k=0;k<nb;k++)
- {
- for (i=j=0;invalid(i,j);)
- i=RANDOM,j=RANDOM;
- grid[i][j]=1;
- }
-
- analyze(0,0);
- printf("%c>",27);
-
- for (i=j=boom=0;!boom;)
- {
- drawscr();
- move(j,i*2+1); refresh();
- c=GETCH;
- switch (c)
- {
- case '1': i--;
- case '2': j++; break;
- case '7': j--;
- case '4': i--; break;
- case '9': i++;
- case '8': j--; break;
- case '3': j++;
- case '6': i++; break;
- case ' ': toggle(i,j); break;
- case 12: clear(); refresh(); break;
- /* case '\n': */ case 13: case 10: moveto(i,j);
- default: break;
- }
- if (i<0) i=0; if (i>=MAX) i=MAX-1; if (j<0) j=0; if (j>=MAX) j=MAX-1;
- }
-
- endwin();
- if (boom==1) printf ("Good job!\n");
- else printf ("***BOOM***\n");
- for (j=0;j<MAX;j++,printf("\n"))
- for (i=0;i<MAX;i++)
- {
- if (think[i][j]<NOTYET)
- if (grid[i][j]) printf ("* ");
- else printf ("o ");
- else
- if (think[i][j]==NOTYET)
- if (grid[i][j])
- printf ("*. ");
- else printf ("o. ");
- else
- if (think[i][j]==BOMB)
- if (grid[i][j])
- printf ("*! ");
- else printf ("o? ");
- else
- if (grid[i][j])
- printf ("*? ");
- else printf ("o! ");
- }
- printf ("\n*=bomb, o=okay .=no guess, !=right, ?=wrong\n\n");
- }
-