#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();
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;
};
Miner::Miner()
{
m_Row=8;
m_Col=8;
m_MineNum=10;
}
Miner::m_PlateSet(int a,int b,int c)
{
m_Row=a;
m_Col=b;
m_MineNum=c;
m_Area=m_Row*m_Col;
}
//Miner::m_FiledGen()
Miner::m_MineLoc()
{
srand(time(NULL));
for(int q=0;q<m_MineNum;q++)
{
// 1D location of the mines ->
//*(m_LocArray+q)=(rand()%(m_Area));
*(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
// 2D location of the mines ->
m_IndexRow=(*(m_LocArray+q)-1)/m_Col;
m_IndexCol=(*(m_LocArray+q)-1)%m_Col;
// <- 2D location of the mines
// Set as 'Mine's. ->
// 2 lines added to both Row & Col,...
// ...to avoid an "edge" mine.
*(m_PlateStateP+(m_IndexRow+1)*m_Col+(m_IndexCol+1))=9;
//MineLoc2D[tempR+1][tempC+1]=9;
// "Squares" of this vector are abandon.
// To be modificated here.
if(*(m_PlateStateP+(m_IndexRow)*m_Col+(m_IndexCol))<9)
++*(m_PlateStateP+(m_IndexRow)*m_Col+(m_IndexCol));
//if(MineLoc2D[tempR][tempC]<9) ++MineLoc2D[tempR][tempC];
if(*(m_PlateStateP+(m_IndexRow)*m_Col+(m_IndexCol+1))<9)
++*(m_PlateStateP+(m_IndexRow)*m_Col+(m_IndexCol+1));
//if(MineLoc2D[tempR][tempC+1]<9) ++MineLoc2D[tempR][tempC+1];
if(*(m_PlateStateP+(m_IndexRow)*m_Col+(m_IndexCol+2))<9)
++*(m_PlateStateP+(m_IndexRow)*m_Col+(m_IndexCol+2));
//if(MineLoc2D[tempR][tempC+2]<9) ++MineLoc2D[tempR][tempC+2];
if(*(m_PlateStateP+(m_IndexRow+1)*m_Col+(m_IndexCol))<9)
++*(m_PlateStateP+(m_IndexRow+1)*m_Col+(m_IndexCol));
//if(MineLoc2D[tempR+1][tempC]<9) ++MineLoc2D[tempR+1][tempC];
if(*(m_PlateStateP+(m_IndexRow+1)*m_Col+(m_IndexCol+2))<9)
++*(m_PlateStateP+(m_IndexRow+1)*m_Col+(m_IndexCol+2));
//if(MineLoc2D[tempR+1][tempC+2]<9) ++MineLoc2D[tempR+1][tempC+2];
if(*(m_PlateStateP+(m_IndexRow+2)*m_Col+(m_IndexCol))<9)
++*(m_PlateStateP+(m_IndexRow+2)*m_Col+(m_IndexCol));
//if(MineLoc2D[tempR+2][tempC]<9) ++MineLoc2D[tempR+2][tempC];
if(*(m_PlateStateP+(m_IndexRow+2)*m_Col+(m_IndexCol+1))<9)
++*(m_PlateStateP+(m_IndexRow+2)*m_Col+(m_IndexCol+1));
//if(MineLoc2D[tempR+2][tempC+1]<9) ++MineLoc2D[tempR+2][tempC+1];
if(*(m_PlateStateP+(m_IndexRow+2)*m_Col+(m_IndexCol+2))<9)
++*(m_PlateStateP+(m_IndexRow+2)*m_Col+(m_IndexCol+2));
//if(MineLoc2D[tempR+2][tempC+2]<9) ++MineLoc2D[tempR+2][tempC+2];
}
}
Miner::Print()
{
for(int k=0;k<m_Row+2;k++)
{
for(int j=0;j<m_Col+2;j++)
{
printf("%d ",*(m_PlateStateP+k*m_Col+j));
}
printf("\n\n\n");
}
}
void main()
{
Miner m1;
//m1.Print();
m1.m_PlateSet(8,8,10);
m1.m_MineLoc();
m1.Print();
system("pause");
}
/*
// "Mines" or "Not Mines" are marked by numbers.
// 9 for mines,
// and other numbers from 0 to 8 stands ...
// for the number of the 8 surrownding locations.
int main(void)
{
int MineLoc1D[nMine]={0};
int MineLoc2D[nRow+2][nCol+2]={0};
srand(time(NULL));
for(int p=0;p<nMine;p++)
{
MineLoc1D[p]=rand()%(nRow*nCol); // 1D
// Avoid overlap
for(int t=0;t<p;t++)
{
if(MineLoc1D[p]==MineLoc1D[t])
{
p--;
}
}
}
for(int i=0;i<nMine;i++)
{
int tempR=0,tempC=0; // Converted to 2D
tempR=(MineLoc1D[i]-1)/nCol;
tempC=(MineLoc1D[i]-1)%nCol; //
// Avoid an "edge" mine.
MineLoc2D[tempR+1][tempC+1]=9; // Set as 'Mine's.
// "Squares" of this vector are abandon.
// To be modificated here.
if(MineLoc2D[tempR][tempC]<9) ++MineLoc2D[tempR][tempC];
if(MineLoc2D[tempR][tempC+1]<9) ++MineLoc2D[tempR][tempC+1];
if(MineLoc2D[tempR][tempC+2]<9) ++MineLoc2D[tempR][tempC+2];
if(MineLoc2D[tempR+1][tempC]<9) ++MineLoc2D[tempR+1][tempC];
if(MineLoc2D[tempR+1][tempC+2]<9) ++MineLoc2D[tempR+1][tempC+2];
if(MineLoc2D[tempR+2][tempC]<9) ++MineLoc2D[tempR+2][tempC];
if(MineLoc2D[tempR+2][tempC+1]<9) ++MineLoc2D[tempR+2][tempC+1];
if(MineLoc2D[tempR+2][tempC+2]<9) ++MineLoc2D[tempR+2][tempC+2];
}
for(int k=0;k<nRow+2;k++)
{
for(int j=0;j<nCol+2;j++)
{
printf("%d ",MineLoc2D[k][j]);
}
printf("\n\n\n");
}
// for(int j=0;j<nMine;j++) printf("%d\t",MineLoc1D[j]);
system("PAUSE");
return 0;
}
*/
编译链接通过,运行出错。
错误信息: