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

📄 扫雷view.cpp

📁 基于MFC的扫雷游戏,与windows自带的扫雷游戏基本相同~
💻 CPP
字号:
// 扫雷View.cpp : implementation of the CMyView class
//
#include <time.h>
#include <stdlib.h> 

#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_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	//}}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
	//循环变量
	int i = 1;
	int j = 1;
	m_Count = 0;

/*	m_XLeft[1][1] = 130;
    m_XRight[1][1] = 130+30;
	m_YTop[1][1] = 130;
	m_YButton[1][1] = 130+30;
	for(i=1;i<=10;i++)
	{
		for(j=1;j<=10;j++)
		{
			if(i!=1 && j!= 1)
			{
				m_XLeft[i][j] = m_XLeft[i][j-1] + 30;
				m_XRight[i][j] = m_XRight[i][j-1] + 30;
				m_YTop[i][j] = m_YTop[i][j-1] + 30;
				m_YButton[i][j] = m_YButton[i][j-1] + 30;
			}
			
		}
	}*/
    InitMap();
	
   	
}

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
	int i = 0;
	int j = 0;	

	CRect re;
    
	CClientDC dc(this);
	
    CPen pen(PS_SOLID,1,RGB(255,0,0));
	
	dc.SelectObject(&pen);
	CBrush rb(RGB(125,125,125));
	re.SetRect(130,130,130+10*30,130+10*30);
//	dc.FillRect(re,&rb);
	//dc.SetBkMode(TRANSPARENT);

	for(j=0;j<=10;j++)
	{	
		dc.MoveTo(130+30*j,130);
		dc.LineTo(130+30*j,130+30*10);
	}	

	for(j=0;j<=10;j++)
	{
		dc.MoveTo(130,130+30*j);
		dc.LineTo(130+30*10,130+30*j);
	}	

}

/////////////////////////////////////////////////////////////////////////////
// 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::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

//	CView::OnLButtonDblClk(nFlags, point);
}

void CMyView::InitMap()
{
	//InitGrid();
	int i = 0;
	int j = 0;
	int tempX = 0;
	int tempY = 0;

	for(i=1;i<11;i++)
	{
		for(j=1;j<11;j++)
		{
			m_show[i][j] = 0;
		}
	}

	srand((unsigned)time(NULL));
	//-2代表墙壁
	//-1代表有泪
	for(i=0;i<12;i++)
	{
		map[0][i] = -100;
		map[11][i] = -100;
		map[i][0] = -100;
		map[i][11] = -100;
	}
	for(i=1;i<=10;i++)
	{
		for(j=0;j<=10;j++)
		{
			map[i][j] = 0;
		}
	}


	map[rand()%10+1][rand()%10+1] = -1;

	for(i=1;i<15;i++)
	{
		tempX = rand()%10+1;
		tempY = rand()%10+1;
		if(HaveTheSame(tempX,tempY))
		{
			i = i-1;
		}
		else
		{
			map[tempX][tempY] = -1;
		}
	}
	ComputOthers();
	m_happy.LoadBitmap(IDB_HAPPY);
	m_sad.LoadBitmap(IDB_SAD);
}

BOOL CMyView::HaveTheSame(int tempX,int tempY)
{
	BOOL retu = FALSE;
	if(map[tempX][tempY] == -1)
	{
		retu = TRUE;		
	}
	return retu;
}

void CMyView::ComputOthers()
{
	int i ;
	int j ;
	int k ;
	int tempX = 0;
	int tempY = 0;
	
	int addX[8] = {0,0,1,1,1,-1,-1,-1};
	int addY[8] = {-1,1,0,1,-1,0,1,-1};
	for(i=1;i<=10;i++)
	{
		for(j=1;j<=10;j++)
		{
			if(map[i][j] == -1)
			{
				for(k=0;k<8;k++)
				{
					tempX = i + addX[k];
					tempY = j + addY[k];
					if(map[tempX][tempY] != -100 && map[tempX][tempY] != -1)
					{
						map[tempX][tempY] ++;
					}
				}
			}
		}
	} 
}

void CMyView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	CString num;
	int i = 0;
	int j = 0;

	int addX[8] = {0,0,1,-1,1,1,-1,-1};
	int addY[8] = {-1,1,0,0,-1,1,-1,1};
	
	int x_n = (point.x-130)/30 + 1;
	int y_n = (point.y-130)/30 + 1;
	CClientDC dc(this);	

	if(x_n <= 10 && x_n>0 && y_n<=10 && y_n>0 &&m_show[x_n][y_n] ==0)
	{
		
		if(map[x_n][y_n] <= 8 && map[x_n][y_n] > 0 &&m_show[x_n][y_n] ==0)
		{
			CBrush br(&m_sad);
            CBrush b(&m_happy);
			CRect re;
			re.SetRect(247,50,290,90);
			dc.FillRect(re,&br);
			clock_t wait = (clock_t)300;
			clock_t goal;
            goal = wait + clock();
            while( goal > clock() )
			{
			}   
			dc.FillRect(re,&b);

			CString y1;	
            m_show[x_n][y_n] = 1;
			num.Format("%d",map[x_n][y_n]);
			CBrush m_br(RGB(255,0,0));
			m_re.SetRect(130+30*(x_n-1),130+30*(y_n-1),130+30*(x_n),130+30*(y_n));
			dc.FillRect(m_re,&m_br);
			dc.SetBkMode(TRANSPARENT);

			dc.TextOut(130+30*(x_n-1)+10,130+30*(y_n-1)+10,num);
			m_Count ++;
			
		
		}

		else if(map[x_n][y_n] == 0 && m_show[x_n][y_n] ==0)
		{
			m_show[x_n][y_n] = 1;
			CBrush br(&m_sad);
            CBrush b(&m_happy);
			CRect re;
			re.SetRect(247,50,290,90);
			dc.FillRect(re,&br);
			clock_t wait = (clock_t)300;
			clock_t goal;
            goal = wait + clock();
            while( goal > clock() )
			{
			}   
			dc.FillRect(re,&b);			

			struct XY *pNew = new struct XY;
			pNew->x = x_n;
			pNew->y = y_n;
			pNew->pLink = NULL;		
			
			struct XY *pHead = pNew;
			struct XY *pTail = pHead;
			struct XY *pDel = NULL; 
			CBrush m_br(RGB(255,0,0));
			m_re.SetRect(130+30*(x_n-1),130+30*(y_n-1),130+30*(x_n),130+30*(y_n));
			dc.FillRect(m_re,&m_br);
			dc.SetBkMode(TRANSPARENT);
			
			num.Format("%d",map[x_n][y_n]);
			dc.TextOut(m_XLeft[x_n][y_n]+10,m_YTop[x_n][y_n]+10,num);

			while(pHead != NULL)
			{
				for(i =0;i<8;i++)
				{
					if(map[pHead->x+addX[i]][pHead->y+addY[i]] == 0 && m_show[pHead->x+addX[i]][pHead->y+addY[i]] ==0 )
					{
						//dc.TextOut(m_XLeft[pHead->x+addX[i]][pHead->y+addY[i]]+10,m_YTop[pHead->x+addX[i]][pHead->y+addY[i]]+10,"0");
						
						pNew = new struct XY;
						pNew->x = pHead->x+addX[i];
						pNew->y = pHead->y+addY[i];
						pNew->pLink = NULL;
						pTail->pLink = pNew;
						pTail = pNew;	
						m_show[pNew->x][pNew->y] = 1;
						
					}
					else if(map[pHead->x+addX[i]][pHead->y+addY[i]] >0 && m_show[pHead->x+addX[i]][pHead->y+addY[i]] ==0)
					{
						CBrush m_br(RGB(255,0,0));
						m_re.SetRect(130+30*(pHead->x+addX[i]-1),130+30*(pHead->y+addY[i]-1),130+30*(pHead->x+addX[i]),130+30*(pHead->y+addY[i]));
						dc.FillRect(m_re,&m_br);
						dc.SetBkMode(TRANSPARENT);

						num.Format("%d",map[pHead->x+addX[i]][pHead->y+addY[i]]);
						m_Count ++;
						dc.TextOut(130+30*(pHead->x+addX[i]-1)+10,130+30*(pHead->y+addY[i]-1)+10,num);
						m_show[pHead->x+addX[i]][pHead->y+addY[i]] = 1;
					}
				}
				pDel = pHead;
				CBrush m_br(RGB(255,255,0));
				m_re.SetRect(130+30*(pHead->x-1),130+30*(pHead->y-1),130+30*(pHead->x),130+30*(pHead->y));
				dc.FillRect(m_re,&m_br);
				dc.SetBkMode(TRANSPARENT);
				m_Count++;
                
				dc.TextOut(130+30*(pHead->x-1)+10,130+30*(pHead->y-1)+10,"0");
				pHead = pHead->pLink;
			
			
			}
		
		}
		else if(map[x_n][y_n] == -1)
		{
		
			CBrush br(&m_sad);
            CBrush b(&m_happy);
			CRect re;
			re.SetRect(247,50,290,90);
		    dc.FillRect(re,&b);
			dc.SetBkMode(TRANSPARENT);
			clock_t wait = (clock_t)3;
			clock_t goal;
            goal = wait + clock();
            while( goal > clock())
			{
			}  
			dc.FillRect(re,&br);

			for(i =1;i<=10;i++)
			{
				for(j = 1;j<=10;j++)
				{
					if(map[i][j] != -1)
					{
						num.Format("%d",map[i][j]);
						dc.TextOut(130+30*(i-1)+10,130+30*(j-1)+10,num);
					}
					else
					{
						
						dc.TextOut(130+30*(i-1)+10,130+30*(j-1)+10,"★");
					}
				}
			}			
		}
		
	}
	if(m_Count >= 100-15)
	{
		MessageBox("You succeed");
	}
	OnDraw(&dc);
	//CView::OnLButtonDown(nFlags, point);
}

void CMyView::InitGrid()
{
	

}

⌨️ 快捷键说明

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