📄 扫雷view.cpp
字号:
// 扫雷View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "扫雷.h"
#include "扫雷Doc.h"
#include "扫雷View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_START, OnStart)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
m_Over = FALSE;//
//位图初始化
for (int c = 0; c < 11; c++)
{
m_Bmp[c].LoadBitmap(IDB_BITMAP0 + c);
}
//初始化雷区
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Col; j++)
{
m_Lei[i][j].BmpNum = 0;//显示位图初始为空
m_Lei[i][j].LeiNum = 0;//初始化为0
}
}
//随机产生地雷
CTime time = GetCurrentTime();//获取当前时间
int s;
s = time.GetSecond();//获取时间秒数,产生真随机数
int lei = 0;
do
{
int i = (rand() + s) % Row;//
int j = (rand() + s) % Col;//
if (m_Lei[i][j].LeiNum != -1)
{
m_Lei[i][j].LeiNum = -1;//随机分配地雷
lei++;
}
} while(lei != LeiNum);
//给非地雷的方块赋值
for (int a = 0; a < Row; a++)
for (int b = 0; b < Col; b++)
if (m_Lei[a][b].LeiNum != -1)
{
for (int c = a-1; c < a+2; c++)
for (int d = b-1; d < b+2; d++)
if (c>=0 && c<Row && d>0 && d<Col)
if (m_Lei[c][d].LeiNum == -1)
{
m_Lei[a][b].LeiNum++;
}
}
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//画背景
CBrush my_brush;
my_brush.CreateSolidBrush(RGB(192,192,192));
CRect my_rect(0,0,1200,800);
pDC->FillRect(my_rect,&my_brush);
//画雷区
//CPen my_pen;
// my_pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
// pDC->SelectObject(&my_pen);
for (int i = 0; i <= Row; i++)
{
pDC->MoveTo(10+i*15,10);
pDC->LineTo(10+i*15,250);
}
for (int j = 0; j <= Col; j++)
{
pDC->MoveTo(10, 10+j*15);
pDC->LineTo(385,10+j*15);
}
//每次刷新显示
/************************************************************************/
/* BmpNum = 0 时,显示框;
/* BmpNum = 1 时,显示对应数字图;
/* BmpNum = 2 时,显示旗子; */
/************************************************************************/
CDC Dc;
if (Dc.CreateCompatibleDC(pDC) ==FALSE)
{
AfxMessageBox("Can't create DC");
}
for(int a = 0; a < Row; a++)
for(int b = 0; b < Col; b++)
{
if(m_Lei[a][b].BmpNum == 1)
{
Dc.SelectObject(m_Bmp[m_Lei[a][b].LeiNum]);
pDC->BitBlt(a*15+10,b*15+10,160,160,&Dc,0,0,SRCCOPY);
}
else if(m_Lei[a][b].BmpNum == 2)
{
Dc.SelectObject(m_Bmp[9]);
pDC->BitBlt(a*15+10,b*15+10,160,160,&Dc,0,0,SRCCOPY);
}
//如果over
if(m_Over && m_Lei[a][b].LeiNum == -1)
{
Dc.SelectObject(m_Bmp[10]);
pDC->BitBlt(a*15+10,b*15+10,160,160,&Dc,0,0,SRCCOPY);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC = GetDC();
CDC DC;
if (DC.CreateCompatibleDC(pDC) == FALSE)
{
AfxMessageBox("Can't create DC");
}
if (point.x>10 && point.x<385 &&
point.y>10 && point.y<250)
{
if (m_Over)
{
return;//结束
}
//鼠标坐标转换为数组坐标
int a=(point.x-10)/15;
int b=(point.y-10)/15;
if (m_Lei[a][b].BmpNum == 0)
{
if (m_Lei[a][b].LeiNum == -1)
{
m_Over = TRUE;
Invalidate();//全屏刷新
}
else
{
m_Lei[a][b].BmpNum = 1;
CRect rect;
rect.left = a*15 + 10;
rect.right = a*15 + 25;
rect.top = b*15 + 10;
rect.bottom = b*15 + 25;
InvalidateRect(&rect);
}
}
}
CView::OnLButtonDown(nFlags,point);
}
void CMyView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (point.x>10 && point.x<385 &&
point.y>10 && point.y<250)
{
if (m_Over)
{
return;
}
//鼠标坐标转换为数组坐标
int a=(point.x-10)/15;
int b=(point.y-10)/15;
if (m_Lei[a][b].BmpNum == 0 || m_Lei[a][b].BmpNum == 2)
{
if (m_Lei[a][b].BmpNum == 0)
{
m_Lei[a][b].BmpNum = 2;
}
else
{
m_Lei[a][b].BmpNum = 0;
}
CRect rect;
rect.left = a*15 + 10;
rect.right = a*15 + 25;
rect.top = b*15 + 10;
rect.bottom = b*15 + 25;
InvalidateRect(&rect);
}
}
CView::OnRButtonDown(nFlags, point);
}
void CMyView::OnStart()
{
// TODO: Add your command handler code here
m_Over = FALSE;
//初始化雷区
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Col; j++)
{
m_Lei[i][j].BmpNum = 0;//显示位图初始为空
m_Lei[i][j].LeiNum = 0;//初始化为0
}
}
//随机产生地雷
CTime time = GetCurrentTime();//获取当前时间
int s;
s = time.GetSecond();//获取时间秒数
int lei = 0;
do
{
int i = (rand() ) % Row;//* s
int j = (rand() ) % Col;//* s
if (m_Lei[i][j].LeiNum != -1)
{
m_Lei[i][j].LeiNum = -1;//随机分配地雷
lei++;
}
} while(lei != LeiNum);
//给非地雷的方块赋值
for (int a = 0; a < Row; a++)
for (int b = 0; b < Col; b++)
if (m_Lei[a][b].LeiNum != -1)
{
for (int c = a-1; c < a+2; c++)
for (int d = b-1; d < b+2; d++)
if (c>=0 && c<Row && d>0 && d<Col)
if (m_Lei[c][d].LeiNum == -1)
{
m_Lei[a][b].LeiNum++;
}
}
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -