The C++ Program: mines.cpp

  1 // minesweeper.cpp : Defines the entry point for the console application.
  2 // Tom Hain, Sept 8, 2009
  3 #include <iostream>
  4 #include <vector>
  5 using namespace std;
  6 
  7 void main()
  8 {
  9         for (int nRows, nCols; cin >> nRows >> nCols, nRows * nCols; )
 10         {
 11                 vector<vector<char> > board(nRows+2, vector<char>(nCols+2, '.'));
 12                 for (int i = 1; i < nRows+1; ++i)
 13                         for (int j = 1; j < nCols+1; ++j)
 14                                 cin >> board[i][j];
 15 
 16                 const int displ[][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
 17                 for (int i = 1; i < nRows+1; ++i, cout << endl)
 18                         for (int count, j = 1; count = 0, j < nCols+1; cout << static_cast<char>(board[i][j++]=='*'? '*': '0' + count))
 19                                 for (int k = 0; k < 8; ++k)
 20                                         count += (board[ i + displ[k][0] ][ j + displ[k][1] ] == '*'? 1 : 0);
 21         }
 22 }
 23