#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define nMine 10 // Number of mines
#define nRow 10
#define nCol 8
class Miner
{
public:
Miner(int,int,int);
Miner();
//m_PlateSet(int,int,int);
m_MineLoc();
Print();
private:
int m_Row;
int m_Col;
int m_Area;
int m_MineNum;
int *m_LocArray;
int *m_PlateStateP;
int m_IndexRow,m_IndexCol;
};
//default parameters
Miner::Miner()
{
m_Row=10;
m_Col=10;
m_MineNum=10;
m_Area=m_Row*m_Col;
}
// parameters set by user.
Miner::Miner(int a,int b,int c)
{
// The length of each "square" should be added with 2, ...
// ... e.g. , if we want a 3-rowed matrix, the "m_Row" should be defined "5". ...
// ... for in the following "generating" part ignores the data on the 1st and last rows/columns, ...
// ... and the "print" part will hide those.
m_Row=a;
m_Col=b;
m_MineNum=c;
m_Area=m_Row*m_Col;
}
//Miner::m_FiledGen()
Miner::m_MineLoc()
{
srand(time(NULL));
int *(m_LocArray)=new int [m_MineNum*sizeof(int)];
int *m_PlateStateP=new int [(m_Row+2)*(m_Col+2)*sizeof(int)];
for (int OriPSi=0;OriPSi<(m_Row+2)*(m_Col+2);OriPSi++) // 初始化 is necessary,
{ // for room 分配ed by 动态 will holds value !=0.
*(m_PlateStateP+OriPSi)=0;
}
for(int q=0;q<m_MineNum;q++)
{
// 1D location of the mines ->
//*(m_LocArray+q)=(rand()%(m_Area));
//(m_LocArray+q)=new int [sizeof(int)];
*(m_LocArray+q)=(rand()%(m_Row*m_Col));
// Avoid overlap
{
for(int t=0;t<q;t++)
{
if(*(m_LocArray+q)==*(m_LocArray+t))
{
q--;
}
}
}
// <- 1D location of the mines
//坐标转化,加一圈边
printf("Mine Location of small size:%d\n",*(m_LocArray+q));
m_IndexRow=(*(m_LocArray+q))/m_Col;
m_IndexCol=(*(m_LocArray+q))%m_Col;
(*(m_LocArray+q))=(m_IndexRow+1)*(m_Col+2)+(m_IndexCol+1);
printf("Mine Location of large size:%d\n",*(m_LocArray+q));
// Set "Mine Point" .
*(m_PlateStateP+(*(m_LocArray+q)))=9;
for(int w=0;w<(m_Row+2)*(m_Col+2);w++)
{
printf("%d ",*(m_PlateStateP+w));
if(0==(w+1)%(m_Col+2))
{printf("\n\n\n");}
}
// 2D location of the mines ->
// m_IndexRow=(*(m_LocArray+q))/m_Col;
// m_IndexCol=(*(m_LocArray+q))%m_Col;
// <- 2D location of the mines
// Set neighbours of 'Mine's. ->
// 2 lines added to both Row & Col,...
// ...to avoid an "edge" mine.
// "Squares" of this vector abandon.
// To be modificated here.
if(*(m_PlateStateP+(*(m_LocArray+q))-1)<9) // left
{++(*(m_PlateStateP+(*(m_LocArray+q))-1));}
if(*(m_PlateStateP+(*(m_LocArray+q))+1)<9) // right
{++(*(m_PlateStateP+(*(m_LocArray+q))+1));}
if(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-2)<9) // up
{++(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-2));}
if(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+2)<9) // down
{++(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+2));}
if(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-3)<9) // leftup
{++(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-3));}
if(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-1)<9) // rightup
{++(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-1));}
if(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+1)<9) // leftdown
{++(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+1));}
if(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+3)<9) // rightdown
{++(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+3));}
}
for(int z=0;z<(m_Row+2)*(m_Col+2);z++)
{
printf("%d ",*(m_PlateStateP+z));
if(0==(z+1)%(m_Col+2))
{printf("\n\n\n");}
}
}
Miner::Print()
{
for(int k=0;k<m_Row;k++)
{
for(int j=0;j<m_Col;j++)
{
printf("%d ",*(m_PlateStateP+m_Col+1+j+2*k));
//printf("%d ",*(m_PlateStateP+k*m_Col+j));
}
printf("\n\n\n");
}
}
void main()
{
Miner m1=Miner(3,3,3);
m1.m_MineLoc();
// m1.Print();
system("pause");
}