📄 eightqueendlg.cpp
字号:
// EightQueenDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EightQueen.h"
#include "EightQueenDlg.h"
#include ".\eightqueendlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE g_hQhandle = NULL;
BOOL g_bThreadQuit = FALSE;
HANDLE g_hSeqshow = NULL;
BOOL g_bSeqshow = TRUE;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CEightQueenDlg dialog
CEightQueenDlg::CEightQueenDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEightQueenDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEightQueenDlg)
m_nSumofQueens = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nNumofQueens = 0;
m_nSumofQueens = 8;
m_nShowQueenindex = 0;
m_pQueenArray = new int[8];
m_bStartCount = FALSE;
}
CEightQueenDlg::~CEightQueenDlg()
{
if(m_pQueenArray)
{
delete []m_pQueenArray;
m_pQueenArray = NULL;
}
}
void CEightQueenDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEightQueenDlg)
DDX_Control(pDX, IDC_QUEEN_STATIC, m_QueenStatic);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEightQueenDlg, CDialog)
//{{AFX_MSG_MAP(CEightQueenDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_KILLFOCUS(IDC_SUMQUEEN_EDIT, OnKillfocusSumqueenEdit)
ON_BN_CLICKED(IDC_STEPCOUNT_BTN, OnStepcountBtn)
ON_BN_CLICKED(IDC_SEQUENCECOUNT_BTN, OnSequenceCountBtn)
ON_BN_CLICKED(IDC_BEGINCOUNT_BTN, OnBeginCountBtn)
ON_BN_CLICKED(IDC_RESHOW_BTN, OnReShowBtn)
//}}AFX_MSG_MAP
ON_STN_CLICKED(IDC_QUEEN_STATIC, OnStnClickedQueenStatic)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEightQueenDlg message handlers
BOOL CEightQueenDlg::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
SetDlgItemText(IDC_SUMQUEEN_EDIT, "8");
return TRUE; // return TRUE unless you set the focus to a control
}
void CEightQueenDlg::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 CEightQueenDlg::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 CEightQueenDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/**************************计算皇后位置的线程**************************/
UINT CEightQueenDlg::GetQueenThread(LPVOID pThis)
{
CEightQueenDlg *This = (CEightQueenDlg *)pThis;
This->GetQueenSpace(0);
SetEvent(g_hQhandle);
AfxEndThread(0);
return 0;
}
/**************************递归计算皇后位置**************************/
void CEightQueenDlg::GetQueenSpace(int index)
{
if(g_bThreadQuit)
{
return ;
}
if(index == m_nSumofQueens)
{
for(int i = 0; i < m_nSumofQueens; i++)
m_pQueenArrays[m_nNumofQueens][i] = m_pQueenArray[i];
m_nNumofQueens++;
CString t_cs;
t_cs.Format("共%d条结果", m_nNumofQueens);
GetDlgItem(IDC_SUMANSWER_STATIC)->SetWindowText(t_cs);
return ;
}
for(int i = 0; i < m_nSumofQueens; i++)
{
if(IsQueenRight(index, i))
{
m_pQueenArray[index] = i;
GetQueenSpace(index + 1);
}
}
}
BOOL CEightQueenDlg::IsQueenRight(int index, int value)
{
for(int i = 0; i < index; i++)
{
if(m_pQueenArray[i] == value || abs(index - i) == abs(m_pQueenArray[i] - value))
return FALSE;
}
return TRUE;
}
void CEightQueenDlg::OnKillfocusSumqueenEdit()
{
CString str;
GetDlgItem(IDC_SUMQUEEN_EDIT)->GetWindowText(str);
int sum = atoi(str);
//4以下没有成立的结果,12以上计算的结果不能存储到m_pQueenArray数组中去
if(sum < 4 || sum > 12)
{
MessageBox("输入数据不在许可范围内,请重新输入!");
str.Format("%d", m_nSumofQueens);
GetDlgItem(IDC_SUMQUEEN_EDIT)->SetWindowText(str);
return ;
}
GetDlgItem(IDC_BEGINCOUNT_BTN)->EnableWindow(TRUE);
m_nSumofQueens = sum;
m_nNumofQueens = 0;
m_nShowQueenindex = 0;
m_bStartCount = FALSE;
delete []m_pQueenArray;
m_pQueenArray = new int[m_nSumofQueens];
ASSERT(m_pQueenArray);
m_QueenStatic.SetNumofQueens(m_nSumofQueens);
}
void CEightQueenDlg::OnStepcountBtn()
{
if(g_bSeqshow)
{
g_bSeqshow = FALSE;
}
if(m_nShowQueenindex >= m_nNumofQueens)
{
GetDlgItem(IDC_STEPCOUNT_BTN)->SetWindowText("单步");
return ;
}
GetDlgItem(IDC_STEPCOUNT_BTN)->SetWindowText("下一步");
CString t_cs;
t_cs.Format("第%d个结果", m_nShowQueenindex + 1);
GetDlgItem(IDC_SHOWANSWER_STATIC)->SetWindowText(t_cs);
m_QueenStatic.GetQueenPlace(m_pQueenArrays[m_nShowQueenindex++]);
}
/**************************显示皇后所在位置线程**************************/
UINT CEightQueenDlg::ShowSequenceQueen(LPVOID pThis)
{
CEightQueenDlg *This = (CEightQueenDlg *)pThis;
g_bSeqshow = TRUE;
while(This->m_nShowQueenindex < This->m_nNumofQueens && g_bSeqshow)
{
CString t_cs;
t_cs.Format("第%d个结果", This->m_nShowQueenindex + 1);
This->GetDlgItem(IDC_SHOWANSWER_STATIC)->SetWindowText(t_cs);
This->m_QueenStatic.GetQueenPlace(This->m_pQueenArrays[This->m_nShowQueenindex++]);
Sleep(200);
}
SetEvent(g_hSeqshow);
AfxEndThread(0);
return 0;
}
void CEightQueenDlg::OnSequenceCountBtn()
{
if(g_bSeqshow || m_nShowQueenindex >= m_nNumofQueens)
return ;
g_bSeqshow = TRUE;
GetDlgItem(IDC_STEPCOUNT_BTN)->SetWindowText("单步");
g_hSeqshow = CreateEvent(NULL, FALSE, FALSE, NULL);
AfxBeginThread(&ShowSequenceQueen, this);
}
void CEightQueenDlg::OnBeginCountBtn()
{
GetDlgItem(IDC_BEGINCOUNT_BTN)->EnableWindow(FALSE);
m_bStartCount = TRUE;
g_bThreadQuit = TRUE;
//确保线程结束
if(g_hQhandle != NULL)
{
WaitForSingleObject(g_hQhandle, INFINITE);
g_hQhandle = NULL;
}
if(g_hSeqshow != NULL)
{
WaitForSingleObject(g_hSeqshow, INFINITE);
g_hSeqshow = NULL;
}
g_bThreadQuit = FALSE;
g_bSeqshow = TRUE;
g_hQhandle = CreateEvent(NULL, FALSE, FALSE, NULL);
g_hSeqshow = CreateEvent(NULL, FALSE, FALSE, NULL);
GetDlgItem(IDC_SUMANSWER_STATIC)->SetWindowText("计算中...");
GetDlgItem(IDC_SHOWANSWER_STATIC)->SetWindowText("第...个结果");
GetDlgItem(IDC_STEPCOUNT_BTN)->SetWindowText("单步");
AfxBeginThread(&GetQueenThread, this);
AfxBeginThread(&ShowSequenceQueen, this);
}
void CEightQueenDlg::OnReShowBtn()
{
if(!m_bStartCount)
return ;
GetDlgItem(IDC_RESHOW_BTN)->EnableWindow(FALSE);
g_bSeqshow = FALSE;
if(g_hSeqshow != NULL)
{
WaitForSingleObject(g_hSeqshow, INFINITE);
g_hSeqshow = NULL;
}
m_nShowQueenindex = 0;
GetDlgItem(IDC_STEPCOUNT_BTN)->SetWindowText("单步");
g_hSeqshow = CreateEvent(NULL, FALSE, FALSE, NULL);
AfxBeginThread(&ShowSequenceQueen, this);
GetDlgItem(IDC_RESHOW_BTN)->EnableWindow(TRUE);
}
void CEightQueenDlg::OnStnClickedQueenStatic()
{
// TODO: 在此添加控件通知处理程序代码
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -