Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: C  |  size: 5.67 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /***************************************************************************
  2.  * Name: Nathan Shain ID: 206026478 *
  3.  *
  4.  * Name: Noam Chen ID: 315675934 *
  5.  *
  6.  * Assignment number: 7
  7.  * Exercise number: 1
  8.  *
  9.  **************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. #define N 10
  16. #define M 15
  17.  
  18. void newBoard(char board[][N]);
  19. void randomBombs(int bombs[][N]);
  20. int game(char board[][N], int bombs[][N]);
  21. void openSquare(char board[][N], int bombs[][N], int row, int col);
  22.  
  23. int main()
  24. {
  25.     char board[N][N];
  26.     int bombs[N][N]={{0}};
  27.     int win;
  28.     newBoard(board);
  29.     printf("\n");
  30.     randomBombs(bombs);
  31.     win=game(board, bombs);
  32.     if (win==0)
  33.         printf("Boom!!! You have lost");
  34.     else if(win==1)
  35.             printf("Congratulations!! You have won!!");
  36.     return 0;
  37. }
  38.  
  39. /*בניית הלוח המוסתר שיוצג למשתמש*/
  40. void newBoard(char board[][N])
  41. {
  42.     int i,j;
  43.     for (i=0; i<N; i++)
  44.         for (j=0; j<N; j++)
  45.             board[i][j]=(char)178;
  46. }
  47.  
  48. /*בניית מערך הפצצות*/
  49. void randomBombs(int bombs[][N])
  50. {
  51.     int i,j,bombRow,bombCol,mines=0,b=0;
  52.     srand(time(NULL));
  53.     while(mines<M)
  54.     {
  55.         bombRow=rand()%N;
  56.         bombCol=rand()%N;
  57.         if(bombs[bombRow][bombCol]!=-1)
  58.         {
  59.             bombs[bombRow][bombCol]=-1;
  60.             mines++;
  61.         }
  62.     }
  63.     for(i=0;i<N;i++)
  64.     {
  65.         for(j=0;j<N;j++)
  66.         {
  67.             if(bombs[i][j]!=-1)
  68.             {
  69.                 if(i!=0&&j!=0)
  70.                 {
  71.                     if(bombs[i-1][j-1]==-1)
  72.                         b++;
  73.                 }
  74.                 if(i!=0)
  75.                 {
  76.                     if(bombs[i-1][j]==-1)
  77.                         b++;
  78.                 }
  79.                 if(i!=0&&j<N-1)
  80.                 {
  81.                     if(bombs[i-1][j+1]==-1)
  82.                         b++;
  83.                 }
  84.                 if(j!=0)
  85.                 {
  86.                     if(bombs[i][j-1]==-1)
  87.                         b++;
  88.                 }
  89.                 if(j<N-1)
  90.                 {
  91.                     if(bombs[i][j+1]==-1)
  92.                         b++;
  93.                 }
  94.                 if(i<N-1&&j!=0)
  95.                 {
  96.                     if(bombs[i+1][j-1]==-1)
  97.                         b++;
  98.                 }
  99.                 if(i<N-1)
  100.                 {
  101.                     if(bombs[i+1][j]==-1)
  102.                         b++;
  103.                 }
  104.                 if(i<N-1&&j<N-1)
  105.                 {
  106.                     if(bombs[i+1][j+1]==-1)
  107.                         b++;
  108.                 }
  109.                 bombs[i][j]=b;
  110.                 b=0;
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. int game(char board[][N],int bombs[][N])
  117. {
  118.     int i,j,cMines=M, option, row, col,squares=N*N,temp=0;
  119.      for (i=0; i<N; i++)
  120.      {
  121.         for (j=0; j<N; j++){printf("%c", board[i][j]);}
  122.             printf("\n");
  123.      }
  124.     while(squares>M)
  125.     {
  126.        printf("There are %d mines left.\nWhat would you like to do now:\n", cMines);
  127.        printf("1.Open a new square.\n2.Flag a square as a mine\n");
  128.        if(scanf("%d", &option)!=1){printf("Input error\n"); exit(1);}
  129.        if (option==1)
  130.        {
  131.              printf("Please insert row and column number:\n");
  132.              if(scanf("%d%d", &row, &col)!=2){printf("Input error\n"); exit(1);}
  133.              if(board[row][col]=='F')
  134.              {
  135.                   printf ("square is flagged as a mine. Are you sure you wanted to open it?\n Enter your choice: 1. Yes or 2. No");
  136.                   scanf("%d", &option);
  137.                   if (option==1)
  138.                   {
  139.                       openSquare(board,bombs,row,col);
  140.                       if (board[row][col]=='X')
  141.                       {
  142.                           return 0;
  143.                       }
  144.                   }
  145.              }
  146.             else if(board[row][col]!=(char)178){printf("These square is already opened\n");}
  147.             else
  148.             {
  149.                 openSquare(board,bombs,row,col);
  150.                 for (i=0; i<N; i++)
  151.                 {
  152.                     for (j=0; j<N; j++){printf("%c", board[i][j]);}
  153.                         printf("\n");
  154.                 }
  155.                 if(board[row][col]=='X')
  156.                    return 0;
  157.             }
  158.            for(i=0;i<N;i++)
  159.            {
  160.                for(j=0;j<N;j++)
  161.                {
  162.                    if(board[i][j]==(char)178){temp++;}
  163.                }
  164.            }
  165.            squares-=temp;
  166.            temp=0;
  167.        }
  168.        else
  169.        {
  170.            printf("Please insert row and column number:\n");
  171.            if(scanf("%d%d", &row, &col)!=2){printf("Input error\n"); exit(1);}
  172.            if (board[row][col]=='F')
  173.             {
  174.                 board[row][col]=(char)178;
  175.                 cMines++;
  176.             }
  177.            else
  178.            {
  179.                board[row][col]='F';
  180.                cMines--;
  181.            }
  182.            for (i=0; i<N; i++)
  183.            {
  184.               for (j=0; j<N; j++){printf("%c", board[i][j]);}
  185.               printf("\n");
  186.            }
  187.        }
  188.     }
  189.     return 1;
  190. }
  191.  
  192. void openSquare(char board[][N], int bombs[][N], int row, int col)
  193. {
  194.     if(row<0||col<0||row>=N||col>=N){return;}
  195.     if(bombs[row][col]==-1)
  196.     {
  197.         board[row][col]='X';
  198.         return;
  199.     }
  200.     else
  201.     {
  202.         board[row][col]=(char)(bombs[row][col]+'0');
  203.         if(board[row][col]=='0')
  204.         {
  205.             openSquare(board,bombs,row+1,col);
  206.             openSquare(board,bombs,row-1,col);
  207.             openSquare(board,bombs,row,col+1);
  208.             openSquare(board,bombs,row,col-1);
  209.         }
  210.     }
  211. }