⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clearbombview.cpp

📁 实现了windows自带小游戏扫雷的简单功能
💻 CPP
字号:
// ClearBombView.cpp : implementation of the CClearBombView class
//

#include "stdafx.h"
#include "ClearBomb.h"


#include "ClearBombDoc.h"
#include "ClearBombView.h"
#include "CBombGame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const int StartX = 10;
const int StartY = 10;
const int Lenth = 25;

/////////////////////////////////////////////////////////////////////////////
// CClearBombView

IMPLEMENT_DYNCREATE(CClearBombView, CFormView)

BEGIN_MESSAGE_MAP(CClearBombView, CFormView)
	//{{AFX_MSG_MAP(CClearBombView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_MOVE()
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClearBombView construction/destruction

CClearBombView::CClearBombView()
	: CFormView(CClearBombView::IDD)
{
	InitClearBombGame();
}

CClearBombView::~CClearBombView()
{
}

void CClearBombView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClearBombView)

	//}}AFX_DATA_MAP
}

BOOL CClearBombView::PreCreateWindow(CREATESTRUCT& cs)
{
	return TRUE;
}

void CClearBombView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CClearBombView printing

BOOL CClearBombView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CClearBombView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CClearBombView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CClearBombView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CClearBombView diagnostics

#ifdef _DEBUG
void CClearBombView::AssertValid() const
{
	CFormView::AssertValid();
}

void CClearBombView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CClearBombDoc* CClearBombView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClearBombDoc)));
	return (CClearBombDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CClearBombView message handlers

void CClearBombView::OnDraw(CDC* pDC) 
{
	CString str;
	CRect rect;
	CBrush pBr1(RGB(0,0,255));
	CBrush pBr2(RGB(255,255,255));
	CClientDC dc(this);

	for(int i=0;i<NXN;i++)
		for(int j=0;j<NXN;j++)
		{
			rect = new CRect(StartX+1+Lenth*i,StartY+1+Lenth*j,StartX+24+Lenth*i,StartY+24+Lenth*j); 
			pDC->FillRect(rect,&pBr1);
			if(true == m_cGame.GetXYSelect(i,j))
			{
				dc.FillRect(rect,&pBr2);	
				if(0 == m_cGame.GetXYState(i,j))
					str.Format(" ");
				else if(9 == m_cGame.GetXYState(i,j))
					str.Format("X");
				else
					str.Format("%d",m_cGame.GetXYState(i,j));
				dc.TextOut(StartX+5+Lenth*i,StartY+5+Lenth*j,str);
			}
		}
}

void CClearBombView::OnLButtonDown(UINT nFlags, CPoint point) 
{
}

void CClearBombView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	int i,j;
	CRect rect;
	CString str;
	CBrush pBr(RGB(255,255,255));
	CClientDC dc(this);
	Invalidate(TRUE);
	for(i=0;i<NXN;i++)
		for(j=0;j<NXN;j++)
		if(!m_cGame.GetXYSelect(i,j))
		{

			rect = new CRect(StartX+1+Lenth*i,StartY+1+Lenth*j,StartX+24+Lenth*i,StartY+24+Lenth*j); 
			if(rect.PtInRect(point))	
			{	
				
				if(m_cGame.SelectXY(i,j) == FALSE )
				{	
					int k,l;
					for(k=0;k<NXN;k++)
						for(l=0;l<NXN;l++)
							m_cGame.SetXYSelected(k,l);
					Invalidate(FALSE);
					str.Format("失败!");
					AfxMessageBox(str);
					{
						str.Format("继续?!");
						AfxMessageBox(str);
						InitClearBombGame();
						Invalidate(TRUE);
						return;
					}
				}

			}

		}
	if(IsFinished())
	{
		str.Format("成功!");
		Invalidate(TRUE);	
		AfxMessageBox(str);
		for(i=0;i<NXN;i++)
			for(j=0;j<NXN;j++)
				m_cGame.SetXYSelected(i,j);
			Invalidate(FALSE);
			{
				str.Format("继续?!");
				AfxMessageBox(str);
				InitClearBombGame();
				OnDraw(&dc);
				return;
			}
	}

}

void CClearBombView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	int i,j;

	CRect rect;
	CString str;
	CBrush pBr(RGB(255,255,255));
	CClientDC dc(this);

	for(i=0;i<NXN;i++)
		for(j=0;j<NXN;j++)
		if(!m_cGame.GetXYSelect(i,j))
		{	
			rect = new CRect(StartX+1+Lenth*i,StartY+1+Lenth*j,StartX+24+Lenth*i,StartY+24+Lenth*j); 
			if(rect.PtInRect(point))
			{	
				
				str.Format("F");
					dc.TextOut(StartX+5+Lenth*i,StartY+5+Lenth*j,str);
				if(9 == m_cGame.GetXYState(i,j))
					m_nBombClear++;

			}
		}

	if(IsFinished())
	{
		str.Format("成功!");
		Invalidate(TRUE);
		AfxMessageBox(str);
		for(i=0;i<NXN;i++)
			for(j=0;j<NXN;j++)
				m_cGame.SetXYSelected(i,j);	

			{
				str.Format("继续?!");
				Invalidate(FALSE);
				AfxMessageBox(str);
				InitClearBombGame();
				OnDraw(&dc);
				return;
			}
	}

}

BOOL CClearBombView::IsFinished()
{
	int i,j;
	int sta = 0;
	for(i=0;i<NXN;i++)
		for(j=0;j<NXN;j++)
		if(!m_cGame.GetXYSelect(i,j))
			sta++;
	sta = sta - m_nBombClear;
	if(m_nBombClear + sta == BombNum)
		return TRUE;
	return FALSE;
}


void CClearBombView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);

	
}

void CClearBombView::OnPaint() 
{
	CPaintDC dc(this); 
	OnDraw(&dc);
	
}

void CClearBombView::OnMove(int x, int y) 
{
	CFormView::OnMove(x, y);
}

void CClearBombView::InitClearBombGame()
{
			m_nBombClear = 0;
			m_cGame.InitBombArray();
			m_cGame.CalcBombArray();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -