Advertisement
Guest User

Blackone

a guest
Nov 15th, 2009
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. // minesweeper.cpp
  2. // Author: Jeffrey Cartagena
  3.  
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. char **matrix;
  10.  
  11. // Prototypes
  12. void readFile(ifstream &, int &, int &);
  13. void writeFile(ofstream &, int &, int &);
  14. void checkMines(int, int);
  15. int check(int i, int j);
  16.  
  17.  
  18.  
  19. int main()
  20. {
  21.     ifstream inFile;
  22.     ofstream outFile;
  23.     int m, n;
  24.  
  25.     try
  26.     {
  27.         inFile.open("input.in");
  28.         outFile.open("output.out");
  29.     }
  30.     catch(...)
  31.     {
  32.         cout << "The were problems opening the files\n";
  33.         cin.ignore();
  34.         return 1;
  35.     }
  36.    
  37.     readFile(inFile, m, n);
  38.     while(!inFile.eof())
  39.     {
  40.         checkMines(m, n);
  41.         writeFile(outFile, m , n);
  42.         for(int i=0; i < m; i++)
  43.             delete[] matrix[i];
  44.         delete[] matrix;
  45.         readFile(inFile, m, n);
  46.     }
  47.     inFile.close();
  48.     outFile.close();
  49.     cout << "Finishing the problem\n";
  50.     cin.ignore();
  51.     return 0;
  52. }
  53.  
  54. void readFile(ifstream & inFile, int & m, int & n)
  55. {
  56.     inFile >> m >> n;
  57.    
  58.     matrix = new char*[m];
  59.    
  60.     for(int x=0; x<m; x++)
  61.         matrix[x] = new char[n];
  62.    
  63.     for(int i=0; i<m; i++)
  64.         for(int j=0; j<n; j++)
  65.         {
  66.             char temp;
  67.             inFile >> temp;
  68.             if(temp == '\n')
  69.                 continue;
  70.             matrix[i][j] = temp;
  71.         }      
  72. }
  73.  
  74. void writeFile(ofstream & outFile, int & m, int & n)
  75. {
  76.     for(int i=0; i<m; i++)
  77.     {
  78.         outFile << endl;
  79.         for(int j=0; j<n; j++)
  80.             outFile << matrix[i][j];
  81.     }
  82.     outFile << endl;       
  83. }
  84.  
  85. void checkMines(int m, int n)
  86. {
  87.     int c;
  88.    
  89.     for(int i=0; i<m; i++)
  90.     {  
  91.         for(int j=0; j<n; j++)
  92.         {
  93.             c = 0;
  94.            
  95.             if(matrix[i][j] == '*')
  96.                 continue;
  97.            
  98.             if(i == 0 && j == 0)
  99.             {
  100.                 c += check(i+1,j); // Check Down
  101.                 c += check(i,j+1); // Check Right
  102.                 c += check(i+1,j+1); // Check Down Right
  103.             }
  104.             else if(i == 0 && j == n-1)
  105.             {
  106.                 c += check(i+1,j); // Check Down
  107.                 c += check(i,j-1); // Check Left
  108.                 c += check(i+1,j-1); // Check Down Left
  109.             }          
  110.             else if(i == m-1 && j == 0)
  111.             {
  112.                 c += check(i-1,j); // Check Up
  113.                 c += check(i,j+1); // Check Right
  114.                 c += check(i-1,j+1); // Check Up Right
  115.             }
  116.             else if(i == m-1 && j == n-1)
  117.             {
  118.                 c += check(i-1,j); // Check Up
  119.                 c += check(i,j-1); // Check Left
  120.                 c += check(i-1,j-1); // Check Up Left
  121.             }
  122.             else if(i == 0)
  123.             {
  124.                 c += check(i,j-1); // Check Left
  125.                 c += check(i,j+1); // Check Right
  126.                 c += check(i+1,j); // Check Down
  127.                 c += check(i+1,j-1); // Check Down Left
  128.                 c += check(i+1,j+1); // Check Down Right
  129.             }
  130.             else if(i == m-1)
  131.             {
  132.                 c += check(i,j-1); // Check Left
  133.                 c += check(i,j+1); // Check Right
  134.                 c += check(i-1,j); // Check Up
  135.                 c += check(i-1,j-1); // Check Up Left
  136.                 c += check(i-1,j+1); // Check Up Right
  137.             }
  138.             else if(j == 0)
  139.             {
  140.                 c += check(i-1,j); // Check Up
  141.                 c += check(i+1,j); // Check Down
  142.                 c += check(i,j+1); // Check Right
  143.                 c += check(i-1,j+1); // Check Up Right
  144.                 c += check(i+1,j+1); // Check Down Right
  145.             }
  146.             else if(j == n-1)
  147.             {
  148.                 c += check(i-1,j); // Check Up
  149.                 c += check(i+1,j); // Check Down
  150.                 c += check(i,j-1); // Check Left
  151.                 c += check(i-1,j-1); // Check Up Left
  152.                 c += check(i+1,j-1); // Check Down Left
  153.             }
  154.             else
  155.             {
  156.                 c += check(i-1,j); // Check Up
  157.                 c += check(i+1,j); // Check Down
  158.                 c += check(i,j-1); // Check Left
  159.                 c += check(i,j+1); // Check Right
  160.                 c += check(i-1,j-1); // Check Up Left
  161.                 c += check(i-1,j+1); // Check Up Right
  162.                 c += check(i+1,j-1); // Check Down Left
  163.                 c += check(i+1,j+1); // Check Down Right
  164.             }
  165.            
  166.             matrix[i][j] = c + 0x30 ;                      
  167.         }
  168.     }
  169. }
  170.  
  171. int check(int i, int j)
  172. {
  173.     if(matrix[i][j] == '*')
  174.         return 1;
  175.        
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement