Advertisement
lIlIlIlIIlI

Mines.c

Dec 10th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #define Nmax 30
  8.  
  9. /*
  10.             N.W   N   N.E
  11.               \   |   /
  12.                \  |  /
  13.             W----Cell----E
  14.                  / | \
  15.                /   |  \
  16.             S.W    S   S.E
  17.    
  18.     Cell-->Current Cell (X, Y)
  19.     N -->  North        (X, Y-1)
  20.     S -->  South        (X, Y+1)
  21.     E -->  East         (X+1, Y)
  22.     W -->  West         (X-1, Y)
  23.     N.E--> North-East   (X+1, Y-1)
  24.     N.W--> North-West   (X-1, Y-1)
  25.     S.E--> South-East   (X+1, Y+1)
  26.     S.W--> South-West   (X-1, Y+1)
  27. */
  28.  
  29. struct node{
  30.     char bomb;
  31.     bool click;
  32. };
  33.  
  34. typedef struct node cell;
  35.  
  36. void Mines(cell **g, int n, int m);
  37. void Sweep(cell **g, int n, int x, int y);
  38. void Num(cell **g, int n);
  39. int Count(cell **g, int n, int x, int y);
  40. void Print(cell **g, int n);
  41. void PrintAns(cell **g, int n, bool cheat);
  42.  
  43. int main(int argc, char *argv[]){
  44.     int N = atoi(argv[1] );
  45.     int M = atoi(argv[2] );
  46.     srand(time(NULL) );
  47.    
  48.     if(N > Nmax){
  49.         printf("error\n");
  50.         return 0;
  51.     }
  52.    
  53.     for(int j = 0; j < N; j++){
  54.         for(int i = 0; i < N; i++)
  55.             printf("? ");
  56.            
  57.         printf("\b\n");
  58.     }
  59.    
  60.     printf("\n");
  61.    
  62.     cell **grid = calloc(N + 2, sizeof(cell *) );
  63.     for(int i = 0; i < N + 2; i++)
  64.         *(grid + i) = calloc(N + 2, sizeof(cell) );
  65.    
  66.     for(int i = 0; i < N + 1; i++)  ( *(grid + 0) + i)->click = true;
  67.     for(int i = 1; i < N + 2; i++)  ( *(grid + N + 1) + i)->click = true;
  68.     for(int j = 1; j < N + 2; j++)  ( *(grid + j) + 0)->click = true;
  69.     for(int j = 0; j < N + 1; j++)  ( *(grid + N + 1) + j)->click = true;
  70.    
  71.     Mines(grid, N, M);
  72.     Num(grid, N);
  73.    
  74.     int X, Y;
  75.     const char C1[] = "eagle\0";
  76.     const char C2[] = "eye\0";
  77.     char Sx[7];
  78.     char Sy[5];
  79.     bool cheat = false;
  80.     unsigned int W;
  81.     while(scanf("%s%s", Sx, Sy) == 2){
  82.         if( strcmp(Sx, C1) && strcmp(Sy, C2) ){
  83.             X = atoi(Sx) + 1;
  84.             Y = atoi(Sy) + 1;      
  85.         }else{ 
  86.             if(cheat)   system("clear");
  87.  
  88.             cheat = true;
  89.             PrintAns(grid, N, cheat);
  90.             continue;
  91.         }
  92.        
  93.         if(X > N || Y > N){
  94.             printf("error\n");
  95.             break;
  96.         }
  97.        
  98.         if( ( *(grid + Y) + X )->bomb == 'X' ){
  99.             printf("You are dead!!\n");
  100.             break;
  101.         }
  102.        
  103.         if( ( *(grid + Y) + X)->click ){
  104.             printf("You have clicked!!\n");
  105.             continue;
  106.         }
  107.        
  108.         Sweep(grid, N, X, Y);
  109.        
  110.         W = 0;
  111.         for(int j = 1; j < N + 1; j++){
  112.             for(int i = 1; i < N + 1; i++){
  113.                 if( !( *(grid + j) + i)->click )
  114.                     W++;
  115.             }
  116.         }
  117.    
  118.         if(W == M){
  119.             printf("You win!!\n");
  120.             break;
  121.         }
  122.        
  123.         system("clear");
  124.         PrintAns(grid, N, cheat);  
  125.         Print(grid, N);
  126.     }
  127.    
  128.     free(grid);
  129.     return 0;
  130. }
  131.  
  132. void Mines(cell **g, int n, int m){
  133.     int tempX, tempY;
  134.     for(int i = 0; i < m; i++){
  135.         tempX = rand() % n + 1;
  136.         tempY = rand() % n + 1;
  137.        
  138.         if( ( *(g + tempY) + tempX )->bomb == 'X' )
  139.             i--;
  140.         else
  141.             ( *(g + tempY) + tempX )->bomb = 'X';
  142.     }
  143.  
  144. }
  145.  
  146. void Num(cell **g, int n){  // can be advanced
  147.     for(int j = 1; j < n + 1; j++){
  148.         for(int i = 1; i < n + 1; i++){
  149.             if( ( *(g + j) + i)->bomb != 'X' )
  150.                 ( *(g + j) + i)->bomb = '0' + Count(g, n, i, j);
  151.            
  152.         }
  153.        
  154.     }
  155.    
  156. }
  157.  
  158. void Sweep(cell **g, int n, int x, int y){
  159.     if( ( *(g + y) + x )->click == true)
  160.         return;
  161.    
  162.     ( *(g + y) + x )->click = true;
  163.    
  164.     if( ( *(g + y) + x )->bomb != '0')
  165.         return;
  166.    
  167.     Sweep(g, n, x, y - 1); 
  168.     Sweep(g, n, x + 1, y); 
  169.     Sweep(g, n, x, y + 1); 
  170.     Sweep(g, n, x - 1, y); 
  171.     Sweep(g, n, x + 1, y + 1); 
  172.     Sweep(g, n, x + 1, y - 1); 
  173.     Sweep(g, n, x - 1, y + 1); 
  174.     Sweep(g, n, x - 1, y - 1); 
  175.    
  176.     /*
  177.     for(int j = -1; j <= 1; j++){
  178.         for(int i = -1; i <= 1; i++){
  179.             Sweep(g, n, x + i, y + j); 
  180.             // note that there's a situation where i = 0, j = 0
  181.         }
  182.        
  183.     }
  184.    
  185.     */
  186.    
  187.     return;
  188. }
  189.  
  190. int Count(cell **g, int n, int x, int y){
  191.     int cnt = 0;
  192.     for(int j = -1; j <= 1; j++){
  193.         for(int i = -1; i <= 1; i++){
  194.             if( (*(g + y + j) + x + i)->bomb == 'X' )   cnt++;
  195.             // note that there's a situation where i = 0, j = 0
  196.         }
  197.        
  198.     }
  199.    
  200.     return cnt;
  201. }
  202.  
  203. void Print(cell **g, int n){
  204.     /*
  205.     for(int j = 1; j < n + 1; j++){
  206.         for(int i = 1; i < n + 1; i++)
  207.             printf("%c ", ( *(g + j) + i)->bomb);
  208.        
  209.         printf("\b\n");
  210.     }
  211.     */
  212.  
  213.     for(int j = 1; j < n + 1; j++){
  214.         for(int i = 1; i < n + 1; i++){
  215.             if( ( *(g + j) + i)->click && ( *(g + j) + i)->bomb != 'X'){
  216.                 if( ( *(g + j) + i)->bomb == '0' )
  217.                     printf("- ");
  218.                 else
  219.                     printf("%c ", ( *(g + j) + i)->bomb);
  220.                
  221.             }else printf("? ");
  222.                
  223.         }
  224.        
  225.         printf("\b\n");
  226.     }
  227.    
  228.     printf("\n");
  229. }
  230.  
  231. void PrintAns(cell **g, int n, bool cheat){
  232.     if(cheat){
  233.         for(int j = 1; j < n + 1; j++){
  234.             for(int i = 1; i < n + 1; i++)
  235.                 ( *(g + j) + i)->bomb == 'X' ? printf("* ") : printf("- ");
  236.            
  237.             printf("\b\n");
  238.         }
  239.        
  240.         printf("\n");
  241.     }
  242.    
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement