home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3428 / bomb.c next >
Encoding:
C/C++ Source or Header  |  1991-05-27  |  4.5 KB  |  194 lines

  1. /* bomb.c, by Eric Olson. */
  2. /*    version 1.0 */
  3. /* This program is copyright 1991 by Eric Olson.  You are welcome to
  4.    distribute it, but this notice should remain intact and you should
  5.    include all the files that were distributed with it.  This program is
  6.    not to be sold, although a fee may be charged to cover distribution
  7.    expenses (if there are any).  If you wish to modify the code and
  8.    distribute it, please feel free to do so, but label the code as
  9.    modified here and change the version accordingly. */
  10.  
  11. #include <stdio.h>
  12. #include <curses.h>
  13.  
  14. #ifdef VMS
  15. unsigned char _getch() /* kent d ramier 20 feb 88 */ /* eo ruined spacing */
  16.  { struct { unsigned short length; unsigned char dtype; unsigned char
  17.  class; char *pointer; } device = { 9, 14, 1, "SYS$INPUT"}; static
  18.  unsigned short a_channel = 0; unsigned char the_key; if (!(a_channel))
  19.  SYS$ASSIGN(&device,&a_channel,0,0); SYS$QIOW
  20.  (0,a_channel,625,0,0,0,&the_key,1,1,0,0,0); return(the_key); }
  21. # define GETCH (_getch())
  22. #else
  23.   /* if you're not on VAX/VMS, you may need to refit this. */
  24.   /* it does occasionally leave numbers on my screen. */
  25. # define GETCH (getch())
  26. #endif VMS
  27.  
  28. #define MAX 16 /* boardsize */
  29.  
  30. /* this is a terrible random generator.  Make a better one. */
  31. #include <math.h>
  32. #include <time.h>
  33. #define RANDOM ((int)((float)rand()/((float)((unsigned)(-1)))*2.0*(float)(MAX)))
  34. #define RANDOM_SEED srand(time((time_t *)0))
  35.  
  36. short think[MAX][MAX],grid[MAX][MAX],boom;
  37. int xx=0,yy=0;
  38.  
  39. #define NOTYET 9
  40. #define BOMB 10
  41. #define OKAY 11
  42. static char icon[]=" 12345678.*o";
  43.  
  44. drawscr()
  45. {
  46. int i,j;
  47.  
  48. for (i=0;i<MAX;i++)
  49.   for (j=0;j<MAX;j++)
  50.     {
  51.       move(j,i*2);
  52. /*       printw("(%c)",icon[think[i][j]]); */
  53.       printw(" %c",icon[think[i][j]]);
  54.     }
  55. /* mvaddch(yy,xx*2+1,'#'); 
  56. mvaddch(yy,xx*2,'['); mvaddch(yy,xx*2+2,']');
  57. mvaddch(yy,xx*2,'>'); */
  58. }
  59.  
  60. short invalid(i,j)
  61. int i,j;
  62. {
  63.   if ((i==j) && ((i==0) || (i==MAX-1))) return (1);
  64.   if ((i<2) && (j<2)) return (1);
  65.   if ((i==MAX) || (j==MAX)) return (1);
  66.   if (grid[i][j]) return (1);
  67.   return (0);
  68. }
  69.  
  70. analyze(x,y)
  71. int x,y;
  72. {
  73. int i,j,t=0;
  74.  
  75. for (i= -1;i<2;i++)
  76.   for (j= -1;j<2;j++)
  77.     if ((i+x>=0) && (j+y>=0) && (i+x<MAX) && (j+y<MAX))
  78.       if (grid[i+x][j+y]) t++;
  79. think[x][y]=t;
  80. if (!t)
  81.   for (i= -1;i<2;i++)
  82.     for (j= -1;j<2;j++)
  83.       if ((i+x>=0) && (j+y>=0) && (i+x<MAX) && (j+y<MAX))
  84.         if (think[i+x][j+y]==NOTYET) analyze(i+x,j+y);
  85. }
  86.  
  87. toggle(x,y)
  88. int x,y;
  89. {
  90. switch (think[x][y])
  91.   {
  92.     case NOTYET: think[x][y]=BOMB; break;
  93.     case BOMB: think[x][y]=OKAY; break;
  94.     case OKAY: think[x][y]=NOTYET; break;
  95.   }
  96. }
  97.  
  98. moveto(x,y)
  99. {
  100. int i,j,q=0;
  101.  
  102. for (i= -1;i<2;i++)
  103.   for (j= -1;j<2;j++)
  104.     if ((i+x>=0) && (i+x<MAX) && (j+y<MAX) && (j+y>=0))
  105.       if (think[i+x][j+y]<NOTYET) q=1;
  106. if (!q) return(0);
  107.  
  108. xx=x; yy=y; analyze(x,y);
  109.  
  110. if (grid[x][y]) boom=2;
  111. if ((y==MAX-1) && (x==MAX-1)) boom=1;
  112. }
  113.  
  114. main(argc,argv)
  115. int argc; char **argv;
  116. {
  117. int i,j,k,nb=30;
  118. char c;
  119.  
  120. if (argc>1)
  121.   {
  122.     sscanf(argv[1],"%d",&nb);
  123.     if ((nb<1) || (nb>(MAX*(MAX-2))))
  124.       {
  125.         printf ("Invalid number of bombs."); exit(0);
  126.       }
  127.   }
  128.  
  129. initscr();
  130. RANDOM_SEED;
  131.  
  132. for (i=0;i<MAX;i++)
  133.   for (j=0;j<MAX;j++)
  134.     think[i][j]=NOTYET,grid[i][j]=0;
  135. for (k=0;k<nb;k++)
  136.   {
  137.     for (i=j=0;invalid(i,j);)
  138.       i=RANDOM,j=RANDOM;
  139.     grid[i][j]=1;
  140.   }
  141.  
  142. analyze(0,0);
  143. printf("%c>",27);
  144.  
  145. for (i=j=boom=0;!boom;)
  146.   {
  147.     drawscr();
  148.     move(j,i*2+1); refresh();
  149.     c=GETCH;
  150.     switch (c)
  151.       {
  152.         case '1': i--;
  153.         case '2': j++; break;
  154.         case '7': j--;
  155.         case '4': i--; break;
  156.         case '9': i++;
  157.         case '8': j--; break;
  158.         case '3': j++;
  159.         case '6': i++; break;
  160.         case ' ': toggle(i,j); break;
  161.         case 12: clear(); refresh(); break;
  162.         /* case '\n': */ case 13: case 10: moveto(i,j);
  163.         default: break;
  164.       }
  165.     if (i<0) i=0; if (i>=MAX) i=MAX-1; if (j<0) j=0; if (j>=MAX) j=MAX-1;
  166.   }
  167.  
  168. endwin();
  169. if (boom==1) printf ("Good job!\n");
  170. else printf ("***BOOM***\n");
  171. for (j=0;j<MAX;j++,printf("\n"))
  172.   for (i=0;i<MAX;i++)
  173.     {
  174.       if (think[i][j]<NOTYET)
  175.         if (grid[i][j]) printf ("*  ");
  176.         else printf ("o  ");
  177.       else
  178.         if (think[i][j]==NOTYET)
  179.           if (grid[i][j])
  180.             printf ("*. ");
  181.             else printf ("o. ");
  182.         else
  183.           if (think[i][j]==BOMB)
  184.             if (grid[i][j])
  185.               printf ("*! ");
  186.             else printf ("o? ");
  187.           else
  188.             if (grid[i][j])
  189.               printf ("*? ");
  190.             else printf ("o! "); 
  191.     }
  192. printf ("\n*=bomb, o=okay     .=no guess, !=right, ?=wrong\n\n");
  193. }
  194.