#include <time.h> #include <stack> #include <Windows.h> using namespace std; // TODO: 在此处引用程序要求的附加头文件 struct MapInfo { bool bMine; int nBomboNum; bool bShow; bool bFind; MapInfo() { bMine=bFind=bShow=false; nBomboNum=0; } void Reset() { bMine=bFind=bShow=false; nBomboNum=0; } }; class MineLogic { public: MineLogic(); ~MineLogic(); private: MapInfo** m_pMap; int m_nMapWidth; int m_nMapHeight; int m_nLevel; int m_nBomboNum; stack<int> m_stPoint; public: void InitMap(int nWidth,int nHeight); void DestroyMap(); void ResetMap(); void RandomMap(); void SetLevel(int nLevel); void ShowMap(); bool Clicked(int nRow,int nCol); void ShowUserMap(); void SetFind(int nRow,int nCol,bool b); bool AllFind(); bool GetFind(int nRow,int nCol); };
#include "stdafx.h" // TODO: 在 STDAFX.H 中 //引用任何所需的附加头文件,而不是在此文件中引用 MineLogic::MineLogic() { m_pMap=NULL; m_nMapHeight=m_nMapWidth=0; m_nLevel=0;//初级 20% 中级40% 高级60% m_nBomboNum=0; } MineLogic::~MineLogic() { this->DestroyMap(); } void MineLogic::InitMap(int nWidth,int nHeight) { this->m_nMapWidth=nWidth; this->m_nMapHeight=nHeight; if(!m_pMap) { m_pMap=new MapInfo*[nHeight]; for(int i=0;i<nHeight;++i) { *(m_pMap+i)=new MapInfo[nWidth]; } } } void MineLogic::ResetMap() { if(!m_pMap) return; for(int i=0;i<this->m_nMapHeight;++i) { memset(*(m_pMap+i),0,sizeof(MapInfo)); } } void MineLogic::SetLevel(int nLevel) { this->m_nLevel=nLevel; m_nLevel=(int)m_nMapHeight*m_nMapWidth*nLevel/10; // switch (nLevel) // { // case 0: // { // m_nLevel=(int)m_nMapHeight*m_nMapWidth*0.1f; // } // break; // case 1: // { // m_nLevel=(int)m_nMapHeight*m_nMapWidth*0.3f; // } // break; // case 2: // { // m_nLevel=(int)m_nMapHeight*m_nMapWidth*0.5f; // } // break; // } } void MineLogic::DestroyMap() { for(int i=0;i<this->m_nMapHeight;++i) { delete[](*(m_pMap+i)); } delete[]m_pMap; } void MineLogic::RandomMap() { if(!m_pMap) { return; } this->ResetMap(); //srand((unsigned int)time(NULL)); srand(2); int n=0; int x,y; for(int i=0;i<m_nLevel;++i) { if(!m_pMap[(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))][(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))].bMine) { m_pMap[(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))][(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))].bMine=true; } else { --i; } } for(int i=0;i<m_nMapHeight;++i) { for(int j=0;j<m_nMapWidth;++j) { if(m_pMap[i][j].bMine) { m_pMap[i][j].nBomboNum=n; continue; } else { x=i-1;y=j-1; if(x>=0&&y>=0) { if(m_pMap[x][y].bMine) { ++n; } } x=i-1;y=j; if(x>=0) { if(m_pMap[x][y].bMine) { ++n; } } x=i-1;y=j+1; if(x>=0&&y<m_nMapWidth) { if(m_pMap[x][y].bMine) { ++n; } } x=i;y=j-1; if(y>=0) { if(m_pMap[x][y].bMine) { ++n; } } x=i;y=j+1; if(y<m_nMapWidth) { if(m_pMap[x][y].bMine) { ++n; } } x=i+1;y=j-1; if(x<m_nMapHeight&&y>=0) { if(m_pMap[x][y].bMine) { ++n; } } x=i+1;y=j; if(x<m_nMapHeight) { if(m_pMap[x][y].bMine) { ++n; } } x=i+1;y=j+1; if(x<m_nMapHeight&&y<m_nMapWidth) { if(m_pMap[x][y].bMine) { ++n; } } m_pMap[i][j].nBomboNum=n; } n=0; } } } bool MineLogic::Clicked(int nRow,int nCol) { m_pMap[nRow][nCol].bShow=true; if(m_pMap[nRow][nCol].bMine) { return false; } if(m_pMap[nRow][nCol].nBomboNum!=0) { return true; } int x,y; x=nRow-1;y=nCol-1; if(x>=0&&y>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow-1;y=nCol; if(x>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow-1;y=nCol+1; if(x>=0&&y<m_nMapWidth) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow;y=nCol-1; if(y>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow;y=nCol+1; if(y<m_nMapWidth) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow+1;y=nCol-1; if(x<m_nMapHeight&&y>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow+1;y=nCol; if(x<m_nMapHeight) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } x=nRow+1;y=nCol+1; if(x<m_nMapHeight&&y<m_nMapWidth) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } } int preSize=m_stPoint.size(); while(m_stPoint.size()!=0) { nCol=m_stPoint.top(); m_stPoint.pop(); nRow=m_stPoint.top(); m_stPoint.pop(); preSize=m_stPoint.size(); x=nRow-1;y=nCol-1; if(x>=0&&y>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow-1;y=nCol; if(x>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow-1;y=nCol+1; if(x>=0&&y<m_nMapWidth) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow;y=nCol-1; if(y>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow;y=nCol+1; if(y<m_nMapWidth) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow+1;y=nCol-1; if(x<m_nMapHeight&&y>=0) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow+1;y=nCol; if(x<m_nMapHeight) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } x=nRow+1;y=nCol+1; if(x<m_nMapHeight&&y<m_nMapWidth) { if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum==0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; m_stPoint.push(x); m_stPoint.push(y); } if(!m_pMap[x][y].bShow&& m_pMap[x][y].nBomboNum!=0&& !m_pMap[x][y].bMine) { m_pMap[x][y].bShow=true; } } } return true; } void MineLogic::ShowMap() { for(int i=0;i<m_nMapWidth;++i) { for(int j=0;j<m_nMapHeight;++j) { if(m_pMap[i][j].bMine) { printf(" B"); } else { printf(" %d",m_pMap[i][j].nBomboNum); } } printf("\n"); } printf("\n\n"); } void MineLogic::ShowUserMap() { for(int i=0;i<m_nMapWidth;++i) { for(int j=0;j<m_nMapHeight;++j) { if(m_pMap[i][j].bShow) { if(m_pMap[i][j].bMine) { printf(" B"); } else { printf(" %d",m_pMap[i][j].nBomboNum); } } else { printf(" X"); } } printf("\n"); } printf("\n\n"); } void MineLogic::SetFind(int nRow,int nCol,bool b) { if(m_pMap[nRow][nCol].bMine) { m_pMap[nRow][nCol].bFind=b; } } bool MineLogic::AllFind() { for(int i=0;i<m_nMapHeight;++i) { for(int j=0;j<m_nMapWidth;++j) { if(!m_pMap[i][j].bFind&& m_pMap[i][i].bMine) { return false; } } } return true; } bool MineLogic::GetFind(int nRow,int nCol) { return m_pMap[nRow][nCol].bFind; }