#include <time.h>
#include <stdio.h>
#include <iostream>
using namespace std;
class Algo{
public:
Algo(int w=10,int h=10);
void show();
void flip(int x,int y);
bool satisfy(int x,int y);
private:
int w,h;
int** field;//0,1,2,3,4,5,6,7,8
int** fieldMine;//0,1
int** shadow;//0,1
};
#include "coreAlgo.h"
Algo::Algo(int w,int h){
//initialize h&w.
this->w=w;
this->h=h;
//initialize field&mine array.
field=new int*[w];
fieldMine=new int*[w];
shadow=new int*[w];
for(int i=0;i<w;i++){
field[i]=new int[h];
fieldMine[i]=new int[h];
shadow[i]=new int[h];
}
srand( (unsigned)time( NULL ) );
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
if(rand()%100<50)
fieldMine[i][j]=0;
else
fieldMine[i][j]=1;
shadow[i][j]=1;
}
}
int result=0;
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
result=0;
if(satisfy(i-1,j-1) && fieldMine[i-1][j-1]==1)
result++;
if(satisfy(i,j-1)&& fieldMine[i][j-1]==1)
result++;
if(satisfy(i+1,j-1)&& fieldMine[i+1][j-1]==1)
result++;
if(satisfy(i-1,j)&& fieldMine[i-1][j]==1)
result++;
if(satisfy(i+1,j)&& fieldMine[i+1][j]==1)
result++;
if(satisfy(i-1,j+1)&& fieldMine[i-1][j+1]==1)
result++;
if(satisfy(i,j+1)&& fieldMine[i][j+1]==1)
result++;
if(satisfy(i+1,j+1)&& fieldMine[i+1][j+1]==1)
result++;
field[i][j]=result;
}
}
}
void Algo::show(){
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
cout<<fieldMine[i][j]<<" ";
}
cout<<" ";
for(int j=0;j<h;j++){
cout<<field[i][j]<<" ";
}
cout<<" ";
for(int j=0;j<h;j++){
cout<<shadow[i][j]<<" ";
}
cout<<endl;
}
}
void Algo::flip(int x,int y){
//(x-1,y-1)(x,y-1)(x+1,y-1)
//(x-1,y)(x,y)(x+1,y)
//(x-1,y+1)(x,y+1)(x+1,y+1)
int result=0;
if(satisfy(x,y) && fieldMine[x][y]==0 && shadow[x][y]==1){
shadow[x][y]=0;
flip(x-1,y-1);
flip(x,y-1);
flip(x+1,y-1);
flip(x-1,y);
flip(x+1,y);
flip(x-1,y+1);
flip(x,y+1);
flip(x+1,y+1);
}
}
bool Algo::satisfy(int x,int y){
if(x<w && x>=0 && y<h && y>=0)
return true;
else
return false;
}
作者:doom_ood