Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // minesweeper.cpp
- // Author: Jeffrey Cartagena
- #include <iostream>
- #include <fstream>
- using namespace std;
- char **matrix;
- // Prototypes
- void readFile(ifstream &, int &, int &);
- void writeFile(ofstream &, int &, int &);
- void checkMines(int, int);
- int check(int i, int j);
- int main()
- {
- ifstream inFile;
- ofstream outFile;
- int m, n;
- try
- {
- inFile.open("input.in");
- outFile.open("output.out");
- }
- catch(...)
- {
- cout << "The were problems opening the files\n";
- cin.ignore();
- return 1;
- }
- readFile(inFile, m, n);
- while(!inFile.eof())
- {
- checkMines(m, n);
- writeFile(outFile, m , n);
- for(int i=0; i < m; i++)
- delete[] matrix[i];
- delete[] matrix;
- readFile(inFile, m, n);
- }
- inFile.close();
- outFile.close();
- cout << "Finishing the problem\n";
- cin.ignore();
- return 0;
- }
- void readFile(ifstream & inFile, int & m, int & n)
- {
- inFile >> m >> n;
- matrix = new char*[m];
- for(int x=0; x<m; x++)
- matrix[x] = new char[n];
- for(int i=0; i<m; i++)
- for(int j=0; j<n; j++)
- {
- char temp;
- inFile >> temp;
- if(temp == '\n')
- continue;
- matrix[i][j] = temp;
- }
- }
- void writeFile(ofstream & outFile, int & m, int & n)
- {
- for(int i=0; i<m; i++)
- {
- outFile << endl;
- for(int j=0; j<n; j++)
- outFile << matrix[i][j];
- }
- outFile << endl;
- }
- void checkMines(int m, int n)
- {
- int c;
- for(int i=0; i<m; i++)
- {
- for(int j=0; j<n; j++)
- {
- c = 0;
- if(matrix[i][j] == '*')
- continue;
- if(i == 0 && j == 0)
- {
- c += check(i+1,j); // Check Down
- c += check(i,j+1); // Check Right
- c += check(i+1,j+1); // Check Down Right
- }
- else if(i == 0 && j == n-1)
- {
- c += check(i+1,j); // Check Down
- c += check(i,j-1); // Check Left
- c += check(i+1,j-1); // Check Down Left
- }
- else if(i == m-1 && j == 0)
- {
- c += check(i-1,j); // Check Up
- c += check(i,j+1); // Check Right
- c += check(i-1,j+1); // Check Up Right
- }
- else if(i == m-1 && j == n-1)
- {
- c += check(i-1,j); // Check Up
- c += check(i,j-1); // Check Left
- c += check(i-1,j-1); // Check Up Left
- }
- else if(i == 0)
- {
- c += check(i,j-1); // Check Left
- c += check(i,j+1); // Check Right
- c += check(i+1,j); // Check Down
- c += check(i+1,j-1); // Check Down Left
- c += check(i+1,j+1); // Check Down Right
- }
- else if(i == m-1)
- {
- c += check(i,j-1); // Check Left
- c += check(i,j+1); // Check Right
- c += check(i-1,j); // Check Up
- c += check(i-1,j-1); // Check Up Left
- c += check(i-1,j+1); // Check Up Right
- }
- else if(j == 0)
- {
- c += check(i-1,j); // Check Up
- c += check(i+1,j); // Check Down
- c += check(i,j+1); // Check Right
- c += check(i-1,j+1); // Check Up Right
- c += check(i+1,j+1); // Check Down Right
- }
- else if(j == n-1)
- {
- c += check(i-1,j); // Check Up
- c += check(i+1,j); // Check Down
- c += check(i,j-1); // Check Left
- c += check(i-1,j-1); // Check Up Left
- c += check(i+1,j-1); // Check Down Left
- }
- else
- {
- c += check(i-1,j); // Check Up
- c += check(i+1,j); // Check Down
- c += check(i,j-1); // Check Left
- c += check(i,j+1); // Check Right
- c += check(i-1,j-1); // Check Up Left
- c += check(i-1,j+1); // Check Up Right
- c += check(i+1,j-1); // Check Down Left
- c += check(i+1,j+1); // Check Down Right
- }
- matrix[i][j] = c + 0x30 ;
- }
- }
- }
- int check(int i, int j)
- {
- if(matrix[i][j] == '*')
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement