Code:
#include<iostream>
#include<cstdlib>
#include<ctime>
#include "lvp\matrix.h"
using namespace std;
bool proveExistence(matrix<char>&a, int x, int y);
void generateRand(matrix<char>&grid, int x, int y);
void display(const matrix<char> &m);
int reveal(matrix<char>&grid, int x, int y);
bool losecond(matrix<char>&grid, int x, int y);
char convert(int x);
void changeStuff(matrix<char>&grid);
bool checkAsterisk(matrix<char>&grid, int x, int y);
bool playAgain();
int main(){
srand(time(0));
int x=0,y=0,a=0,b=0,j=0;
bool cond=false;
char v=' ';
do{
do{
system("CLS");
cout<<"Enter a valid dimension size: ";
cin>>x;
}while(!(x>=3&&x<=10));
matrix<char>copy(x,x,' ');
matrix<char>copy1(x,x,' ');
do{
cout<<"Enter number of mines to hide: ";
cin>>y;
}while(!(y<=x*x&&y>=2));
matrix<char>grid(x,x,' ');
generateRand(grid,x,y);
copy1=grid;
do{
system("CLS");
display(copy);
cout<<"Enter desired coordinate, column: ";
cin>>b;
cout<<"Enter desired coordinate, row: ";
cin>>a;
cond=losecond(grid,a,b);
if(cond!=true){
j=reveal(grid,a,b);
j+=48;
grid[a][b]=(char)j;
copy[a][b]=(char)j;
}
}while(cond==false);
}while(playAgain());
return 0;
}
void changeStuff(matrix<char>&grid)
{
int z=0;
for(int i=0;i<grid.numrows();i++)
{
for(int j=0;j<grid.numcols();j++)
{
if(checkAsterisk(grid,i,j)==false){
j=reveal(grid,i,j);
j+=48;
grid[i][j]=(char)j;
}
}
}
}
bool playAgain(){
char a=' ';
cout<<"Play Again? (Y/N): ";
cin>>a;
if(a=='y'||a=='Y')
return true;
else
return false;
}
bool checkAsterisk(matrix<char>&grid, int x, int y){
if(grid[x][y]=='*')
return true;
else
return false;
}
bool losecond(matrix<char>&grid, int x, int y)
{
if(grid[x][y]=='*'){
system("CLS");
changeStuff(grid);
display(grid);
cout<<"BOOM!";
cin.get();
cin.ignore();
return true;
}
else
return false;
}
int reveal(matrix<char>&grid, int x, int y){
int g=0;
for(int a=x-1;a<x+2;a++){
for(int b=y-1;b<y+2;b++)
{
if(proveExistence(grid,a,b))
{
if(grid[a][b]=='*')
g++;
}
}
}
return g;
}
char convert(int x){
for(int i=48;i<=57;i++)
{
if(x==i)
return (char)i;
}
}
bool proveExistence(matrix<char>&grid, int x, int y){
if((x>=0&&x<grid.numrows())&&(y>=0&&y<grid.numcols()))
{
return true;
}
else
return false;
}
void generateRand(matrix<char>&grid, int x,int y){
int n1=0,n2=0,c=0;
do{
n1=rand()%x;
n2=rand()%x;
if(grid[n1][n2]==' '){
c++;
grid[n1][n2]='*';
}
}while(c!=y);
}
const char BRICK=177;
//must use a space for a place with no move in it
void display(const matrix<char> &m)
{
//grid numbering
cout<<" ";
for(int c=0; c<m.numcols(); c++)
{
cout<<" "<<c;
}
cout<<endl;
int z=0;
for(int r=0; r<m.numrows(); r++)
{
cout<<" ";
//horizontal line
for(int i=0; i<m.numcols()*2+1; i++)
cout<<BRICK;
cout<<endl;
//lines containing actual elements
cout<<z;
for(int c=0; c<m.numcols(); c++)
{
cout<<BRICK<<m[r][c];
}
cout<<BRICK<<z<<endl;
z++;
}
//horizontal line
cout<<" ";
for(int i=0; i<m.numcols()*2+1; i++)
cout<<BRICK;
cout<<endl;
//grid numbering
cout<<" ";
for(int c=0; c<m.numcols(); c++)
{
cout<<" "<<c;
}
cout<<endl;
}
Thanks!