title: Mine Sweeper algorithm in C
body: <p>So I got a project in my class, have to make a 8X8 Mine Sweeper program in C.
  We didn't study recursion yet, so it's not the full game right now...</p>
  <p>Rules: You type a coordinate, if you hit a bomb - you lose. If you don't hit a bomb, a number is shown - the number of bombs around this specific cell.
  You win if you clear the board without hitting a bomb.
  In addition, our teacher had us include a &quot;cheat&quot; in the code - and that's where I started to get confused a little bit.
  If the coordinate starts with -1, the next value he types in will be the first number of cells which are not a mine.
  So, my idea was to make a counter and <code>++</code> it whenever a cell is not a bomb. The problem is, when he types in -1, x and -1, x again the second one will not open as intended. This is the code, relevant block is the one in the if (cordChecker(col, row) == 10).</p>
  <p>By the way, for some reason it prints &quot;:&quot; when not intended...
  Thanks in advance. I'm sorry if anything is wrong in my post, it's my first time posting here.</p>
  <pre><code>`#define _CRT_SECURE_NO_WARNINGS
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  #include &lt;string.h&gt;
  #define COLS_1 8
  #define ROWS_1 8
  void printBoard(char arr[][ROWS_1]); // gets a minesweeper board and prints it 
  void boardInit(char arr[][ROWS_1]); // initializing a board with 'X's.
  void gameOn(int col, int row, char arr[][ROWS_1], char arr2[][ROWS_1]); // gets coordinates from the user, and gets 2 arrays: a hidden board
                                                                          // with the bombs in it, and a board the user can see. 
                                                                          // the function uses other functions in order to get the game going.
  int welcomeUser(); // welcome message
  void minePlacer(char arr[][ROWS_1], int arrLength); // gets an empty board and mines it.
  int cordChecker(int col, int row); // gets column and row from user. Returns 1 if valid and 0 if not. (0&lt;=x&lt;=7 are valid options)
  int mineCounter(int col, int row, char arr[][ROWS_1]);  // gets an array and a coordinate, and counts the nearby mines.
  void youLose(char arr[][ROWS_1]); // prints the hidden board and delivers a you lose message.
  void printNumMines(char arr[][ROWS_1], char arr2[][ROWS_1], int col, int row); // printing the number of mines surrounding a cell.
  
  void main()
  {
      int userChoice, row, col;
      userChoice = welcomeUser();
      srand(time(NULL));
      srand(time(NULL));
      while (userChoice != 1)
      {
          if (userChoice == 0)
              break;
          userChoice = welcomeUser();
      }
      if (userChoice == 1)
      {
          char arr[COLS_1][ROWS_1];                   // Creating the hidden board (the one with bombs)
          char arr2[COLS_1][ROWS_1];                  // Creating the board the user will see
          boardInit(arr);                             // initializing both boards with 'X'
          boardInit(arr2);
          minePlacer(arr, ROWS_1);                    // mining the hidden board
          printBoard(arr2);
          printf(&quot;Please enter your move, column and row:\n&quot;); 
          scanf(&quot;%d%d&quot;, &amp;col, &amp;row);
          gameOn(col, row, arr, arr2);
      }
  }
  
  void minePlacer(char arr[][ROWS_1], int arrLength)
  {
      int i;
      if (arrLength == ROWS_1)
      {
          for (i = 0; i &lt; ROWS_1; i++)
              arr[i][rand() % (ROWS_1-1)] = '*';
      }
  }
  int cordChecker(int col, int row)
  {
      if (col == -1) // If the user wants to win fast
      {
          return 10;
      }
          if ((col &gt;= COLS_1 || col &lt; 0) || (row &gt;= ROWS_1 || row &lt; 0))              // user chose invalid coordinates
              return 0;
          else if ((col &lt; COLS_1 - 1 &amp;&amp; col &gt;= 1) &amp;&amp; (row &lt; ROWS_1 - 1 &amp;&amp; row &gt;= 1)) // checking if the coordinates are not on boundaries.
              return 1;
          else if (col == 0 &amp;&amp; (row != 0 &amp;&amp; row != ROWS_1 - 1))                      // user chose (0,x) 0&lt;x&lt;7 
              return 2;
          else if ((col != 0 &amp;&amp; col != COLS_1 - 1) &amp;&amp; row == 0)                      // user chose (x,0) 0&lt;x&lt;7 
              return 3;
          else if (col == 0 &amp;&amp; row == 0)                                             // user chose (0,0)
              return 4;
          else if (col == COLS_1 - 1 &amp;&amp; row == ROWS_1 - 1)                           // user chose (7,7)
              return 5;
          else if (col == COLS_1 - 1 &amp;&amp; (row != ROWS_1 - 1 &amp;&amp; row != 0))             // user chose (7,x) 0&lt;x&lt;7 
              return 6;
          else if ((col != COLS_1 - 1 &amp;&amp; row != 0) &amp;&amp; row == ROWS_1 - 1)             // user chose (x,7) 0&lt;x&lt;7 
              return 7;
          else if (col == COLS_1 - 1 &amp;&amp; row == 0)                                    // user chose (7,0)
              return 8;
          else if (col == 0 &amp;&amp; row == ROWS_1 - 1)                                    // user chose (0,7)
              return 9;
  
  
  }
  
  void printBoard(char arr[][ROWS_1])
  {
      int i, j;
      printf(&quot;\n&quot;);
      printf(&quot;       0   1   2   3   4   5   6   7  \n&quot;);
      printf(&quot;   ___________________________________\n&quot;);
      for (i = 0; i &lt; COLS_1; i++)
      {
          printf(&quot;   %d | &quot;, i);
          for (j = 0; j &lt; ROWS_1; j++)
          {
              printf(&quot;%c&quot;, arr[i][j]);
              printf(&quot; | &quot;);
          }
          printf(&quot;\n&quot;);
  
      }
      printf(&quot;   ___________________________________\n&quot;);
  }
  int welcomeUser()
  {
      int userChoice;
      printf(&quot;Welcome to Minesweeper!\n\n&quot;);
      printf(&quot;Please choose one of the following options and enter its number:\n\n&quot;);
      printf(&quot;1 - for size 8X8\n\n&quot;);
      printf(&quot;2 - for size 12X12\n\n&quot;);
      printf(&quot;3 - for size 15X15\n\n&quot;);
      printf(&quot;4 - for custom size\n\n&quot;);
      printf(&quot;0 - Exit\n\n&quot;);
      scanf(&quot;%d&quot;, &amp;userChoice);
      return userChoice;
  }
  void boardInit(char arr[][ROWS_1])
  {
      int i, j;
      for (i = 0; i &lt; COLS_1; i++)
      {
          for (j = 0; j &lt; ROWS_1; j++)              // Creating a minesweeper board with &quot;X&quot;s.
          {
              arr[i][j] = 'X';
          }
      }
  
  
  }
  void gameOn(int col, int row, char arr[][ROWS_1], char arr2[][ROWS_1])
  {
      int countSquares = 0, count = 0, temp = row;
      while (cordChecker(col, row) == 0)
      {
          printf(&quot;Invalid coordinates, please try again\n&quot;);
          scanf(&quot;%d%d&quot;, &amp;col, &amp;row);
      }
      while (cordChecker(col, row) != 0)
      {
          if ((arr2[col][row] != 'X')&amp;&amp;(col!=-1))
              printf(&quot;Invalid move, please enter another move!\n&quot;);
          if (col != -1)
          {
              if (arr[col][row] == '*')
              {
                  youLose(arr);
                  break;
              }
          }
          else if (countSquares &gt;= 56)
          {
              printf(&quot;YOU WIN\n&quot;);
              break;
          }
          printNumMines(arr, arr2, col, row);
          printBoard(arr2);
          if (cordChecker(col, row) == 10)
          {
              int i, j;
              printf(&quot;\n&quot;);
              printf(&quot;       0   1   2   3   4   5   6   7  \n&quot;);
              printf(&quot;   ___________________________________\n&quot;);
              for (i = 0; i &lt; COLS_1; i++)
              {
                  printf(&quot;   %d | &quot;, i);
                  for (j = 0; j &lt; ROWS_1; j++)
                  {
                      if (count &lt; temp)
                      {
                          if (arr[i][j] == '*')
                          {
                              printf(&quot;%c&quot;, arr2[i][j]);
                              printf(&quot; | &quot;);
                              
                          }
                          else
                          {
                              printNumMines(arr, arr2, i, j);
                              printf(&quot;%c&quot;, arr2[i][j]);
                              printf(&quot; | &quot;);
                              count++;
                          }
                      }
                      else
                      {
  
                              printf(&quot;%c&quot;, arr2[i][j]);
                              printf(&quot; | &quot;);
                              count++;
                      }
                      
                      
  
                  }
                  printf(&quot;\n&quot;);
  
              }
              printf(&quot;   ___________________________________\n&quot;);
              countSquares = countSquares+row;
              
  
          }
          
          countSquares++;
          if (countSquares &gt; 56)
          {
              printf(&quot;YOU WIN\n&quot;);
              break;
          }
          printf(&quot;Please enter your move, column and row:\n&quot;);
          scanf(&quot;%d%d&quot;, &amp;col, &amp;row);
          temp = temp + row;
      }
  }
  void printNumMines(char arr[][ROWS_1], char arr2[][ROWS_1], int col, int row)
  {
      if (mineCounter(col, row, arr) == 0)
          arr2[col][row] = ' ';
      else
          arr2[col][row] = (char)(mineCounter(col, row, arr) + '0');
  
  }
  void youLose(char arr[][ROWS_1])
  {
      int i, j;
      printf(&quot;\n&quot;);
      printf(&quot;       0   1   2   3   4   5   6   7  \n&quot;);
      printf(&quot;   ___________________________________\n&quot;);
      for (i = 0; i &lt; COLS_1; i++)
      {
          printf(&quot;   %d | &quot;, i);
          for (j = 0; j &lt; ROWS_1; j++)
          {   
              if ((arr[i][j] != '*') &amp;&amp; mineCounter(i, j, arr) == 0)        // if the cell is not a mine and is not surrounded by mines, replace it with ' '
                  arr[i][j] = ' ';
              else if (arr[i][j] != '*')                                    // if the cell is not a mine but surrounded by mines, count them
                  arr[i][j] = (char)(mineCounter(i, j, arr) + '0');
              printf(&quot;%c&quot;, arr[i][j]);                                      // print the cell
              printf(&quot; | &quot;);  
          }
          printf(&quot;\n&quot;);
      }
      printf(&quot;   ___________________________________\n&quot;);
      printf(&quot;YOU LOSE\n&quot;);
  }
  
  int mineCounter(int col, int row, char arr[][ROWS_1])
  {
      int mines = 0;
      switch (cordChecker(col, row))
      {
      case 1:
  
          // Checking the following directions: up, down, left, right
          if (arr[col - 1][row] == '*')
              mines++;
          if (arr[col + 1][row] == '*')
              mines++;
          if (arr[col][row - 1] == '*')
              mines++;
          if (arr[col][row + 1] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col - 1][row + 1] == '*')
              mines++;
          if (arr[col - 1][row - 1] == '*')
              mines++;
          if (arr[col + 1][row + 1] == '*')
              mines++;
          if (arr[col + 1][row - 1] == '*')
              mines++;
  
          return mines;
  
      case 2:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row - 1] == '*')
              mines++;
          if (arr[col][row + 1] == '*')
              mines++;
          if (arr[col + 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col + 1][row - 1] == '*')
              mines++;
          if (arr[col + 1][row + 1] == '*')
              mines++;
          return mines;
  
      case 3:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row + 1] == '*')
              mines++;
          if (arr[col - 1][row] == '*')
              mines++;
          if (arr[col + 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col + 1][row + 1] == '*')
              mines++;
          if (arr[col - 1][row + 1] == '*')
              mines++;
          return mines;
  
      case 4:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row + 1] == '*')
              mines++;
          if (arr[col + 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col + 1][row + 1] == '*')
              mines++;
          return mines;
  
      case 5:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row - 1] == '*')
              mines++;
          if (arr[col - 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col - 1][row - 1] == '*')
              mines++;
          return mines;
  
      case 6:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row - 1] == '*')
              mines++;
          if (arr[col][row + 1] == '*')
              mines++;
          if (arr[col - 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col - 1][row - 1] == '*')
              mines++;
          if (arr[col - 1][row + 1] == '*')
              mines++;
  
          return mines;
  
      case 7:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row - 1] == '*')
              mines++;
          if (arr[col - 1][row] == '*')
              mines++;
          if (arr[col + 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col + 1][row - 1] == '*')
              mines++;
          if (arr[col - 1][row - 1] == '*')
              mines++;
          return mines;
  
      case 8:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row + 1] == '*')
              mines++;
          if (arr[col - 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
  
          if (arr[col - 1][row + 1] == '*')
              mines++;
          return mines;
  
      case 9:
  
          // Checking the following directions: up, down, left, right
          if (arr[col][row - 1] == '*')
              mines++;
          if (arr[col + 1][row] == '*')
              mines++;
  
          // Checking diagonal directions
          if (arr[col + 1][row - 1] == '*')
              mines++;
          return mines;
  
      }
  }
      
  `
  </code></pre>