Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* IDE-Eclipse Neon
- * Toolchain-MinGW32-->GCC
- * Minesweeper.cpp
- * Format-C++11
- * Created on: Sep 23, 2016
- * Author: Kartikey
- */
- #include<iostream>
- using namespace std;
- //class definition begins...
- class box
- {
- int neighbor; // keeps count of the number of mines in the 8 adjacent boxes to a selected box.
- bool mine, disp; //mine will be true if the box in question contains a mine. disp will be true, if the contents of a box are to be printed during the next time the grid is displayed.
- public:
- box() //Constructor
- {
- mine = false;
- disp = false;
- neighbor = 0;
- }
- void inc_neighbor() //On detecting a mine in vicinity, inspect() increments the value of neighbor by 1.
- {
- neighbor++;
- }
- bool sweep(void) //Checks whether a mine is present or not
- {
- if(mine == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void set_disp() //Sets disp variable to "true", so that when Displaying() is called, that box in the grid displays it's contents (mines or neighbor)
- {
- disp = true;
- }
- bool check_disp() //Check whether the content of a particular box is to be displayed...
- {
- if(disp==false)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- void generate() //Puts a mine in the box
- {
- mine = true;
- }
- void display() //Displays the value of the neighbor
- {
- if(sweep()==false)
- {
- cout << neighbor;
- }
- else
- {
- cout << "*";
- }
- }
- };
- //class definition ends.
- void inspect(box grid[][11], int x, int y)//Counts of the number of mines in the 8 adjacent boxes to a selected box.
- {
- for(int i=x-1; i<=i+1; i++)
- {
- for(int j=y-1; j<=j+1; j++) //Logic to eliminate the checking of certain boxes, when dealing with the boxes that are at the edge of the grid.
- {
- if((j==0)||(j==11)||(i==0)||(j==11)||((i==x)&&(j==y)))
- {
- continue;
- }
- else
- {
- if(grid[i][j].sweep()==true)
- {
- grid[x][y].inc_neighbor();
- }
- }
- }
- }
- }
- void Displaying(box grid[][11]) //Function for displaying the grid.
- {
- for(int i=0; i<11; i++)
- {
- for(int j=0; j<11; j++)
- {
- if((i == 0)&&( j != 0))
- {
- cout << j << " ";
- continue;
- }
- else if(j == 0)
- {
- cout << i << " ";
- continue;
- }
- else
- {
- if(grid[i][j].check_disp()==false)
- {
- cout << " " << " ";
- }
- else
- {
- grid[i][j].display();
- cout << " ";
- }
- }
- }
- cout << endl << endl;
- }
- }
- void Generate_Field(box grid[][11]) //Mines are placed.
- {
- int values[10][2] = {{1, 3}, {3, 6}, {5, 5}, {7, 9}, {2, 3}, {8, 5}, {2, 1}, {6, 7}, {9, 1}, {8, 4}}; //Using pre-defined co-ordinates till the time I'm testing the logic of the program. Will use srand() to generate random coordinates in the final stage.
- for(int i=0; i<10; i++)
- {
- grid[(values[i][0])][(values[i][1])].generate();
- }
- }
- void Disp_Mine(box grid[][11]) //Displays the location of mines in the grid.
- {
- for(int i=0; i<11; i++)
- {
- for(int j=0; j<11; j++)
- {
- if((i == 0)&&( j != 0))
- {
- cout << j << " ";
- continue;
- }
- else if(j == 0)
- {
- cout << i << " ";
- continue;
- }
- else
- {
- if(grid[i][j].sweep()==false)
- {
- cout << " " << " ";
- }
- else
- {
- grid[i][j].display();
- cout << " ";
- }
- }
- }
- cout << endl << endl;
- }
- }
- int main()
- {
- box grid[11][11];
- //int chances = 0, x, y;
- //bool game_over = false;
- Displaying(grid);
- cout << endl;
- Generate_Field(grid);
- Disp_Mine(grid);
- cout << endl;
- for(int i=0; i<=10; i++)
- {
- for(int j=0; j<=10; j++)
- {
- grid[i][j].set_disp();
- }
- }
- //inspect(grid, 1, 1);
- Displaying(grid);
- /*while(game_over==false)
- {
- Displaying(grid);
- cout << "Enter the x coordinate : ";
- cin >> x;
- cout << "Enter the y coordinate : ";
- cin >> y;
- grid[x][y].set_disp();
- if(grid[x][y].sweep()==true)
- {
- cout << "BOOM! You're dead.";
- chances++;
- break;
- }
- else
- {
- }
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement