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

📄 sudokudlg.cpp

📁 一个在VISUAL C++下编程的soduku系统 里面有界面 有出盘 也比较简单
💻 CPP
字号:
/*
   Sudoku Quick and Easy Solver
   Copyright (C) 2005  Suhaib Chishtie

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


#include "stdafx.h"
#include "Sudoku.h"
#include "SudokuDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CSudokuDlg dialog

CSudokuDlg::CSudokuDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSudokuDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSudokuDlg)
		// 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 CSudokuDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSudokuDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSudokuDlg, CDialog)
	//{{AFX_MSG_MAP(CSudokuDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_PUZ1, OnButtonPuz1)
	ON_BN_CLICKED(IDC_BUTTON_PUZ2, OnButtonPuz2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSudokuDlg message handlers

BOOL CSudokuDlg::OnInitDialog()
{
	unsigned j, k, startx, x, y, id;
	CString str;

	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
	
	id = 1;
	startx = y = 10;
	for (j=0; j<9; j++){
		x = startx;
		for (k=0; k<9; k++){
			pEdit[j][k] = new CEdit;
			pEdit[j][k]->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
				CRect(x, y, x+20, y+20), this, id);
			str.Format("%d,%d", j, k);
			//pEdit[j][k]->SetWindowText(str);
			x += 20+5;
			if ( !((k+1)%3) )
				x+= 5;
		}
		y += 20+5;
		if ( !((j+1)%3) )
			y += 5;
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSudokuDlg::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 CSudokuDlg::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 CSudokuDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSudokuDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	unsigned j, k, err=0;
	CString str;

	for (j=0; j<9; j++){
		for (k=0; k<9; k++){
			pEdit[j][k]->GetWindowText(str);
			if ( (str.GetLength() > 0) && (str.GetAt(0) < '1' || str.GetAt(0) > '9' || str.GetLength() > 1) ){
				CString errmsg;
				err=1;
				pEdit[j][k]->SetFocus();
				pEdit[j][k]->SetSel(0, str.GetLength());
				errmsg.Format("Invalid value of <%s> in cell(%d, %d)", str, j, k);
				AfxMessageBox(errmsg);
				break;
			}
			Puzzle[j][k] = (str.GetLength() > 0) ? str.GetAt(0) - '0' : 0;
		}
		if (err)
			break;
	}
	
	if (!err) {
		CDisplay disp;
		unsigned int j, k;

		for (j=0; j<9; j++){
			for (k=0; k<9; k++){
				if (Puzzle[j][k])
					disp.all[j][k] = 0x1 << (Puzzle[j][k] - 1);
				else
					disp.all[j][k] = 0x1FF;
			}
		}
		int nResponse = disp.DoModal();
		if (nResponse == IDCANCEL)
		{

			CDialog::OnOK();
		}
	}
}

void CSudokuDlg::OnButtonPuz1() 
{
	// TODO: Add your control notification handler code here
	unsigned j, k;
	CString str;
	unsigned Puz1[9][9] = {
				  {0, 0, 1, 0, 0, 5, 3, 0, 9} ,
				  {0, 6, 0, 0, 0, 2, 0, 0, 0} ,
				  {0, 0, 0, 0, 0, 0, 7, 2, 0} ,
				  {5, 1, 0, 6, 0, 9, 0, 0, 0} ,
				  {8, 0, 0, 0, 0, 0, 0, 0, 5} ,
				  {0, 0, 0, 5, 0, 3, 0, 7, 4} ,
				  {0, 4, 3, 0, 0, 0, 0, 0, 0} ,
				  {0, 0, 0, 9, 0, 0, 0, 5, 0} ,
				  {6, 0, 9, 2, 0, 0, 4, 0, 0} };
		
	for (j=0; j<9; j++){
		for (k=0; k<9; k++){
			if (Puz1[j][k])
				str.Format("%d", Puz1[j][k]);
			else
				str = _T("");

			pEdit[j][k]->SetWindowText(str);
		}
	}
}

void CSudokuDlg::OnButtonPuz2() 
{
	// TODO: Add your control notification handler code here
	unsigned j, k;
	CString str;
	unsigned Puz2[9][9] = {
				  {0, 0, 0, 7, 0, 0, 0, 2, 0} ,
				  {0, 0, 0, 6, 0, 0, 9, 0, 0} ,
				  {2, 0, 1, 9, 8, 0, 4, 0, 0} ,
				  {0, 9, 0, 0, 0, 0, 0, 0, 5} ,
				  {3, 0, 0, 0, 0, 0, 0, 0, 2} ,
				  {4, 0, 0, 0, 0, 0, 0, 6, 0} ,
				  {0, 0, 7, 0, 1, 3, 5, 0, 8} ,
				  {0, 0, 3, 0, 0, 2, 0, 0, 0} ,
				  {0, 8, 0, 0, 0, 7, 0, 0, 0} };
	
	//AfxMessageBox("hehehe   , I can't solve this puzzle without guessing\n and currently I don't support guessing yet!\nSo I just disabled this puzzle 2");
	//return ;
	
	for (j=0; j<9; j++){
		for (k=0; k<9; k++){
			if (Puz2[j][k])
				str.Format("%d", Puz2[j][k]);
			else
				str = _T("");

			pEdit[j][k]->SetWindowText(str);
		}
	}
}

CSudokuDlg::~CSudokuDlg()
{
	for (int j=0; j<9; j++)
		for (int k=0; k<9; k++)
			if (pEdit[j][k] != NULL)
				delete pEdit[j][k];
}

⌨️ 快捷键说明

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