NQueenDlg.cpp
资源名称:NQueen.rar [点击查看]
上传用户:hncsjd
上传日期:2022-07-08
资源大小:3772k
文件大小:9k
源码类别:
其他智力游戏
开发平台:
Visual C++
- // NQueenDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "NQueen.h"
- #include "NQueenDlg.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()
- /////////////////////////////////////////////////////////////////////////////
- // CNQueenDlg dialog
- CNQueenDlg::CNQueenDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CNQueenDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CNQueenDlg)
- m_N = 8;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CNQueenDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CNQueenDlg)
- DDX_Text(pDX, IDC_BOARD_SIZE, m_N);
- DDV_MinMaxLong(pDX, m_N, 1, 20);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CNQueenDlg, CDialog)
- //{{AFX_MSG_MAP(CNQueenDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_ABOUT, OnAbout)
- ON_BN_CLICKED(IDC_CHANGEN, OnChangeN)
- ON_BN_CLICKED(IDC_JUDGE, OnJudge)
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CNQueenDlg message handlers
- BOOL CNQueenDlg::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 initialization here
- w = 400-400%m_N; //棋盘宽度(如果N不能整除400时,取宽度为最接近于400的、N的倍数)
- h = 400-400%m_N; //棋盘高度
- cell = w / m_N; //每格的宽度和高度
- for( int i=0; i<21; i++ )
- {
- queen[i] = new int[21];
- memset(queen[i],0,21*sizeof(int));
- }
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CNQueenDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- //参数:size-棋盘的规模,cell-每格的大小(宽度和高度)
- void DrawBoard(CDC *pDC, int size, int cell)
- {
- int i, j;
- CBrush w_brush, b_brush;
- b_brush.CreateSolidBrush(RGB(0, 0, 0));
- w_brush.CreateSolidBrush(RGB(255, 255, 255));
- for(i=0; i<size; i++)
- {
- for(j=0; j<size; j++)
- {
- if((i+j)%2 ==0 )
- pDC->FillRect(CRect(i*cell, j*cell, (i+1)*cell, (j+1)*cell), &b_brush);
- else
- pDC->FillRect(CRect(i*cell, j*cell, (i+1)*cell, (j+1)*cell), &w_brush);
- }
- }
- CPen b_pen(PS_SOLID, 1, RGB(0, 0, 0));
- int board = cell*size;
- pDC->SelectObject(b_pen);
- pDC->MoveTo(0, 0);
- pDC->LineTo(0, board-1);
- pDC->LineTo(board-1, board-1);
- pDC->LineTo(board-1, 0);
- pDC->LineTo(0, 0);
- }
- // 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 CNQueenDlg::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
- {
- w = 400-400%m_N; //棋盘宽度(如果N不能整除400时,取宽度为最接近于400的、N的倍数)
- h = 400-400%m_N; //棋盘高度
- cell = w / m_N; //每格的宽度和高度
- LONG left = 20, top = 20; //N皇后棋盘左上角位置
- CPaintDC dc(this); // device context for painting
- CDC MemDC; //定义一个显示设备对象
- CBitmap MemBitmap;//定义一个位图对象
- CPen b_pen(PS_SOLID, 1, RGB(0, 0, 255)); //画外框的画笔
- CBrush w_brush, y_brush;
- w_brush.CreateSolidBrush(RGB(255, 255, 255)); //填充外框的画刷
- y_brush.CreateSolidBrush(RGB(255, 255, 0));
- MemDC.CreateCompatibleDC(&dc); //建立兼容的内存显示设备
- MemBitmap.CreateCompatibleBitmap(&dc,w, h);//建立一个兼容的位图
- CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
- MemDC.FillRect(CRect(20, 20, w, h), &w_brush);
- DrawBoard(&MemDC, m_N, cell); //画棋盘
- MemDC.SelectObject(b_pen);
- MemDC.SelectObject(y_brush);
- int i, j;
- int x1,y1,x2,y2; //绘制圆圈时的边界
- for( i=0; i<m_N; i++)
- {
- for( j=0; j<m_N; j++ )
- {
- if( queen[i][j] == 1 )
- {
- x1 = j*cell+cell/6;
- y1 = i*cell+cell/6;
- x2 = j*cell+cell/6+cell*2/3;
- y2 = i*cell+cell/6+cell*2/3;
- MemDC.Ellipse(x1,y1,x2,y2);
- }
- }
- }
- dc.BitBlt(20,20,w, h,&MemDC,0,0,SRCCOPY);
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CNQueenDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CNQueenDlg::OnAbout()
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- void CNQueenDlg::OnChangeN()
- {
- int N = m_N; //保存更新前的数值
- UpdateData(TRUE); //编辑框中读数据
- if( m_N<1 || m_N >20 ) m_N = N; //新的数值超出范围,强制改回为原来的值
- UpdateData(FALSE); //将成员变量m_N的值写入编辑框中
- if( N!=m_N )
- {
- for( int i=0; i<21; i++ )
- memset(queen[i],0,21*sizeof(int));
- Invalidate( ); //如果m_N的值更新了,强制刷新重绘
- }
- }
- void CNQueenDlg::OnJudge()
- {
- int i, j;
- bool right = true;
- int count = 0; //每行、列、每条对角线上皇后的个数
- for( i=0; i<m_N; i++ ) //检查每行,每行有且只有一个
- {
- count = 0;
- for( j=0; j<m_N; j++ )
- if( queen[i][j]==1 ) count++;
- if( count!=1 )
- { right = false; break; }
- }
- if( !right )
- { MessageBox( "请重新安排!","抱歉,解不对!", MB_OK ); return; }
- for( i=0; i<m_N; i++ ) //检查每列,每列有且只有一个
- {
- count = 0;
- for( j=0; j<m_N; j++ )
- if( queen[j][i]==1 ) count++;
- if( count!=1 )
- { right = false; break; }
- }
- if( !right )
- { MessageBox( "请重新安排!","抱歉,解不对!", MB_OK ); return; }
- for( i=0; i<m_N; i++ ) //检查每条主对角线,每条主对角线上最多只能有1个
- {
- count = 0;
- for( j=0; i+j<m_N; j++ ) //检查与queen[i][0]位于同一条主对角线的位置
- if( queen[i+j][j]==1 ) count++;
- if( count>1 )
- { right = false; break; }
- count = 0;
- for( j=0; i+j<m_N; j++ ) //检查与queen[0][i]位于同一条主对角线的位置
- if( queen[j][i+j]==1 ) count++;
- if( count>1 )
- { right = false; break; }
- }
- if( !right )
- { MessageBox( "请重新安排!","抱歉,解不对!", MB_OK ); return; }
- for( i=0; i<m_N; i++ ) //检查每条次对角线,每条次对角线上最多只能有1个
- {
- count = 0;
- for( j=0; i+j<m_N; j++ ) //检查与queen[i][7]位于同一条次对角线的位置
- if( queen[i+j][m_N-1-j]==1 ) count++;
- if( count>1 )
- { right = false; break; }
- count = 0;
- for( j=0; i-j>=0; j++ ) //检查与queen[0][i]位于同一条次对角线的位置
- if( queen[j][i-j]==1 ) count++;
- if( count>1 )
- { right = false; break; }
- }
- if( !right )
- { MessageBox( "请重新安排!","抱歉,解不对!", MB_OK ); return; }
- MessageBox( "您找到一个解!","恭喜您!", MB_OK );
- }
- void CNQueenDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- int c = (point.x-20)/cell, r = (point.y-20)/cell;
- queen[r][c] = 1 - queen[r][c];
- RECT rect;
- rect.left = 20+c*cell, rect.top = 20+r*cell;
- rect.right = 20+(c+1)*cell, rect.bottom = 20+(r+1)*cell;
- InvalidateRect( &rect );
- CDialog::OnLButtonDown(nFlags, point);
- }