VC 扫雷 源码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// dynamic arrayDlg.cpp : implementation file
//
#include "stdafx.h"
#include "dynamic array.h"
#include "dynamic arrayDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDynamicarrayDlg dialog
CDynamicarrayDlg::CDynamicarrayDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDynamicarrayDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDynamicarrayDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDynamicarrayDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDynamicarrayDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDynamicarrayDlg, CDialog)
//{{AFX_MSG_MAP(CDynamicarrayDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDynamicarrayDlg message handlers
BOOL CDynamicarrayDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initializatio
n here
N=8;
M=6;
::MoveWindow(this->m_hWnd,0,0,40*M+20,40*N+60,FALSE);
InitButtonPanel();
InitMineLocal();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDynamicarrayDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CDynamicarrayDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDynamicarrayDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CDynamicarrayDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
int i=0;
int msg1,msg2;
msg1 = HIWORD(wParam);//???¢
msg2 = LOWORD(wParam);//???tID
for(i=0;i
if (p_MyBut[i/M][i%N].m_flag)
{
return true;
}
p_MyBut[i/M][i%N].SetState(true);
if (m_field_click[i]==0)
{
m_field_click[i]=1;
}
else
{
return true;
}
int tmp=0;
for (int index=0;index
if (m_field_click[index]==0)
{
tmp+=1;
}
}
if (tmp==N)
{
// MessageBox("OK");
// DeleteButton();
// InitMineLocal();
// InitButtonPanel();
// return true;
}
// for (int k=0;k
// p_MyBut[k/N][k%N].EnableWindow(false);
// }
switch (state[i/M][i%N])
{
case -1:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON10)) );
// MessageBox("Failed!");
// DeleteButton();
// InitMineLocal();
// InitButtonPanel();
// return true;
break;
case 1:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON1)) );
break;
case 2:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON2)) );
break;
case 3:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3)) );
break;
case 4:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON4)) );
break;
case 5:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGe
tApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON5)) );
break;
case 6:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON6)) );
break;
case 7:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON7)) );
break;
case 8:
p_MyBut[i/M][i%N].SetIcon(LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON8)) );
break;
case 0:
ClickNONE(i);
break;
}
return true;
}
return CDialog::OnCommand(wParam, lParam);
}
BOOL CDynamicarrayDlg::InitMineLocal()
{
//mine field init
int i=0;
int m=0;
int n=0;
int k=0;
int j=0;
time_t t;
time(&t);
srand(t);
int tmp;
//init m_flag 1:set flag
m_flag=new int[N*M];
for (i=0;i
m_flag[i]=0;
}
mine = new int[N];
state = new int*[M];
for(i=0; i
for (i=0;i
mine[i] = -1;
}
bool repeat=false;
for (i=0;i
tmp=rand()%(N*M);
for(j=0;j
if (tmp==mine[j])
{
i--;
repeat=true;
break;
}
}
if (repeat)
{
repeat=false;
continue;
}
mine[i]=tmp;
}
for (i=0;i
for (j=0;j
state[i][j]=0;
}
}
int horizontal;
int vertical;
for (i=0;i
horizontal = mine[i]/M;
vertical = mine[i]%N;
if (horizontal!=0 && horizontal!=(M-1)
&& vertical!=0 && vertical!=(N-1))
{
state[horizontal-1][vertical-1]+=1;
state[horizontal-1][vertical]+=1;
state[horizontal-1][vertical+1]+=1;
state[horizontal+1][vertical-1]+=1;
state[horizontal+1][vertical]+=1;
state[horizontal+1][vertical+1]+=1;
state[horizontal][vertical-1]+=1;
state[horizontal][vertical]+=1;
state[horizontal][vertical+1]+=1;
}
else if (horizontal==0 && horizontal!=(M-1)
&& vertical!=0 && vertical!=(N-1))
{
state[horizontal+1][vertical-1]+=1;
state[horizontal+1][vertical]+=1;
state[horizontal+1][vertical+1]+=1;
state[horizontal][vertical-1]+=1;
state[horizontal][vertical]+=1;
state[horizontal][vertical+1]+=1;
}
else if (horizontal!=0 && horizontal==(M-1)
&& vertical!=0 && vertical!=(N-1))
{
state[horizontal-1][vertical-1]+=1;
state[horizontal-1][vertical]+=1;
state[horizontal-1][vertical+1]+=1;
state[horizontal][vertical-1]+=1;
state[horizontal][vertical]+=1;
state[horizontal][vertical+1]+=1;
}
else if (horizontal!=0 && horizontal!=(M-1)
&& vertical==0 && vertical!=(N-1))
{
state[horizontal-1][vertical]+=1;
state[horizontal-1][vertical+1]+=1;
state[horizontal+1][vertical]+=1;
state[horizontal+1][vertical+1]+=1;
state[horizontal][vertical]+=1;
state[horizontal][vertical+1]+=1;
}
else if (horizontal!=0 && horizontal!=(M-1)
&& vertical!=0 && vertical==(N-1))
{
state[horizontal-1][vertical-1]+=1;
state[horizontal-1][vertical]+=1;
state[horizontal+1][verti
cal-1]+=1;
state[horizontal+1][vertical]+=1;
state[horizontal][vertical-1]+=1;
state[horizontal][vertical]+=1;
}
if (horizontal==0 && vertical==0)
{
state[horizontal+1][vertical]+=1;
state[horizontal+1][vertical+1]+=1;
state[horizontal][vertical+1]+=1;
}
if (horizontal==0 && vertical==(N-1))
{
state[horizontal+1][vertical-1]+=1;
state[horizontal+1][vertical]+=1;
state[horizontal][vertical-1]+=1;
}
if (horizontal==(M-1) && vertical==0)
{
state[horizontal-1][vertical]+=1;
state[horizontal-1][vertical+1]+=1;
state[horizontal][vertical+1]+=1;
}
if (horizontal==(M-1) && vertical==(N-1))
{
state[horizontal-1][vertical-1]+=1;
state[horizontal-1][vertical]+=1;
state[horizontal][vertical-1]+=1;
}
}
for (i=0;i
state[mine[i]/M][mine[i]%N]=-1;
}
m_field_click=new int[N*M];
for (i=0;i
m_field_click[i]=0;
}
return true;
}
BOOL CDynamicarrayDlg::InitButtonPanel()
{
int i=0;
int m=0;
int n=0;
int k=0;
int j=0;
p_MyBut = new CMyButton*[N];
for(i=0; i
for(j=0;j
for(k=0;k
p_MyBut[j][k].Create( "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_ICON,\
CRect(10+40*k,10+j*40,50+40*k,50+j*40), this, IDC_MYBUTTON0+j*N+k);
//state[j][k]=0;
}
}
return true;
}
BOOL CDynamicarrayDlg::DeleteButton()
{
int j=0;
int i=0;
for(i=0; i
if (p_MyBut[i])
{
delete[] p_MyBut[i];
p_MyBut[i]=NULL;
}
}
if (p_MyBut)
{
delete[] p_MyBut;
p_MyBut =NULL;
}
return true;
}
BOOL CDynamicarrayDlg::ClickNONE(int i)
{
//return true;
int horizontal = i/M;
int vertical = i%N;
if (horizontal!=0 && horizontal!=(M-1)
&& vertical!=0 && vertical!=(N-1))
{
OnCommand((horizontal-1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical+1+IDC_MYBUTTON0,0);
}
else if (horizontal==0 && horizontal!=(M-1)
&& vertical!=0 && vertical!=(N-1))
{
OnCommand((horizontal+1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical+1+IDC_MYBUTTON0,0);
}
else if (horizontal!=0 && horizontal==(M-1)
&& vertical!=0 && vertical!=(N-1))
{
OnCommand((horizontal-1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+1+IDC_MYBUTTON0,0);
On
Command((horizontal)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical+1+IDC_MYBUTTON0,0);
}
else if (horizontal!=0 && horizontal!=(M-1)
&& vertical==0 && vertical!=(N-1))
{
OnCommand((horizontal-1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical+1+IDC_MYBUTTON0,0);
}
else if (horizontal!=0 && horizontal!=(M-1)
&& vertical!=0 && vertical==(N-1))
{
OnCommand((horizontal-1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical-1+IDC_MYBUTTON0,0);
}
if (horizontal==0 && vertical==0)
{
OnCommand((horizontal+1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical+1+IDC_MYBUTTON0,0);
}
if (horizontal==0 && vertical==(N-1))
{
OnCommand((horizontal+1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal+1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical-1+IDC_MYBUTTON0,0);
}
if (horizontal==(M-1) && vertical==0)
{
OnCommand((horizontal-1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+1+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical+1+IDC_MYBUTTON0,0);
}
if (horizontal==(M-1) && vertical==(N-1))
{
OnCommand((horizontal-1)*N+vertical-1+IDC_MYBUTTON0,0);
OnCommand((horizontal-1)*N+vertical+IDC_MYBUTTON0,0);
OnCommand((horizontal)*N+vertical-1+IDC_MYBUTTON0,0);
}
return true;
}