📄 myminezoneview.cpp
字号:
// MyMineZoneView.cpp : implementation of the CMyMineZoneView class
//
#include "stdafx.h"
#include "MyMineZone.h"
#include "MyMineZoneDoc.h"
#include "MyMineZoneView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyMineZoneView
IMPLEMENT_DYNCREATE(CMyMineZoneView, CView)
BEGIN_MESSAGE_MAP(CMyMineZoneView, CView)
//{{AFX_MSG_MAP(CMyMineZoneView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_TIMER() //时间消息Provided by JimSoft
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyMineZoneView construction/destruction
CMyMineZoneView::CMyMineZoneView():Cur(0),Total(0),Second(0)
{
// TODO: add construction code here
srand( (unsigned) time (NULL) ); //伪随机Provided by JimSoft
int row,col;
for(row=0;row<ZONE_SIZE;++row){
for(col=0;col<ZONE_SIZE;++col){
State[row][col]=((rand()>23000 )? 'A':(++Total,'B'));//大于25000的数使为雷,否则...
}//for
}//for
}
int CMyMineZoneView::MineCountNear(int i,int j){ //周围的雷总数Provided by JimSoft
int count=0;
int row,col;
for(row=i-1;row<=i+1;++row){
if(row>=0 && row<ZONE_SIZE){
for(col=j-1;col<=j+1;++col){
if(col>=0 && col<ZONE_SIZE){
if((State[row][col]-'A')%3==0)//State[row][col]==1 || State[row][col]==4 ||State[i][j]==7
++count; //请看左右键函数是怎么改变State[][]状态的.
}
}//for
}
}//for
return count;
}
int CMyMineZoneView::GetIndex(int & i,int & j,int x,int y){//从X,Y坐标得到二维数组的索引.Provided by JimSoft
int start=START;
int end=START+ZONE_SIZE*MINE_SIZE;
int size=MINE_SIZE;
if(x>=start && x<end && y>=start && y<end){
i=(y-start)/size;//索引
j=(x-start)/size;
return 1;//在雷区内
}
else{
return 0;//不在雷区内
}
}
CMyMineZoneView::~CMyMineZoneView()
{
}
BOOL CMyMineZoneView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyMineZoneView drawing
void CMyMineZoneView::OnDraw(CDC* pDC)
{
CMyMineZoneDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
SetTimer(1,1000,NULL); //Provided by JimSoft
for(int n=1;n<=ZONE_SIZE+1;++n){//画雷区横竖各ZONE_SIZE+1条线
pDC->MoveTo(START,n*MINE_SIZE);
pDC->LineTo(START+ZONE_SIZE*MINE_SIZE,n*MINE_SIZE);
pDC->MoveTo(n*MINE_SIZE,START);
pDC->LineTo(n*MINE_SIZE,START+ZONE_SIZE*MINE_SIZE);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMyMineZoneView printing
BOOL CMyMineZoneView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyMineZoneView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyMineZoneView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyMineZoneView diagnostics
#ifdef _DEBUG
void CMyMineZoneView::AssertValid() const
{
CView::AssertValid();
}
void CMyMineZoneView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyMineZoneDoc* CMyMineZoneView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyMineZoneDoc)));
return (CMyMineZoneDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyMineZoneView message handlers
void CMyMineZoneView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int i,j;
if(GetIndex(i,j,point.x,point.y)){//在雷区内//Provided by JimSoft
CDC * pDC=CMyMineZoneView::GetWindowDC();
CString strmsg;
int row,col;
if(State[i][j]=='A'){//在为雷且没有被置"?" 或者"!!"的情况下
KillTimer(1);//停住时间
for(row=0;row<ZONE_SIZE;++row){
for(col=0;col<ZONE_SIZE;++col){
if((State[row][col]-'A')%3==0){//1,4,7
CRect rect(START+col*MINE_SIZE,START+row*MINE_SIZE,//构造一个rectangle
2*START+col*MINE_SIZE,2*START+row*MINE_SIZE);
rect.DeflateRect(5,5);//以rect来画圆
pDC->Ellipse(rect);//圆
}//if
}//for
}//for
strmsg="触雷了!";
MessageBox(strmsg);
}//以上是触雷的情况
else{
if(State[i][j]=='B'){ //以前没有访问过的格子
State[i][j]+=1; //表示此雷已经被扫掉的状态
++Cur; //扫雷数加一
if(Cur>=Total){ //若胜利
strmsg="胜利了!";
MessageBox(strmsg);
}
strmsg.Format("%d",MineCountNear(i,j));
pDC->TextOut(START+j*MINE_SIZE+MINE_SIZE/2,//output at point(x,y)
START+i*MINE_SIZE+MINE_SIZE/2,strmsg);//输出周围的雷数
}
}//没有触雷的情况
strmsg.Format("得分:%f",(double)Cur/Total*100);//目前的得分
pDC->TextOut(5*START+MINE_SIZE*ZONE_SIZE,10*START,strmsg);
ReleaseDC(pDC);
}//if(!GetIndex(i,j,point.x,point.y))
CView::OnLButtonDown(nFlags, point);
}
void CMyMineZoneView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC * pDC=CMyMineZoneView::GetWindowDC();//Provided by JimSoft
CString strmsg;
int i,j;
if(GetIndex(i,j,point.x,point.y)){//能否得到索引
switch(State[i][j]){
case 'B':
case 'A':
State[i][j]+=3;//原始状态右键后
strmsg="?";//置?
pDC->TextOut(START+j*MINE_SIZE+MINE_SIZE/2,
START+i*MINE_SIZE+MINE_SIZE/2,strmsg);
break;
case 'B'+3:
case 'A'+3:
State[i][j]+=3;//从?置到!!
strmsg="!!";
pDC->TextOut(START+j*MINE_SIZE+MINE_SIZE/2,
START+i*MINE_SIZE+MINE_SIZE/2,strmsg);
break;
case 'B'+6:
case 'A'+6:
State[i][j]-=6;//从!!恢复原态,以空格" "代替!!
strmsg=" ";
pDC->TextOut(START+j*MINE_SIZE+MINE_SIZE/2,
START+i*MINE_SIZE+MINE_SIZE/2,strmsg);
}
}//if
ReleaseDC(pDC);
CView::OnRButtonDown(nFlags, point);
}
void CMyMineZoneView::OnTimer(UINT nIDEvent,CDC * pDC)
{
// TODO: Add your message handler code here and/or call default
pDC=CMyMineZoneView::GetWindowDC();//Provided by JimSoft
CString strtime;
++Second;//类的私有成员: 秒
int second=Second%60,minute=Second/60,hour=Second/3600;//具体到时间
CString ss,mm,hh;
ss.Format(second<10? "0%d":"%d",second);//格式化时间
mm.Format(minute<10? "0%d:":"%d:",minute);
hh.Format(hour<10? "0%d:":"%d:",hour);
strtime=hh+mm+ss;//时间合成
UpdateData(false);//保存Second 到类中
pDC->TextOut(5*START+MINE_SIZE*ZONE_SIZE,5*START,strtime);
//UpdateWindow();
ReleaseDC(pDC);
CView::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -